Skip to content

Commit

Permalink
await using normative updates (#16537)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Jul 22, 2024
1 parent ecc8e11 commit 2def809
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,10 @@ const helpers: Record<string, Helper> = {
},
},
),
// size: 907, gzip size: 473
// size: 1117, gzip size: 548
usingCtx: helper(
"7.23.9",
'function _usingCtx(){var r="function"==typeof SuppressedError?SuppressedError:function(r,n){var e=Error();return e.name="SuppressedError",e.error=r,e.suppressed=n,e},n={},e=[];function using(r,n){if(null!=n){if(Object(n)!==n)throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");if(r)var o=n[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==o&&(o=n[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof o)throw new TypeError("Property [Symbol.dispose] is not a function.");e.push({v:n,d:o,a:r})}else r&&e.push({d:n,a:r});return n}return{e:n,u:using.bind(null,!1),a:using.bind(null,!0),d:function(){var o=this.e;function next(){for(;r=e.pop();)try{var r,t=r.d&&r.d.call(r.v);if(r.a)return Promise.resolve(t).then(next,err)}catch(r){return err(r)}if(o!==n)throw o}function err(e){return o=o!==n?new r(e,o):e,next()}return next()}}}',
'function _usingCtx(){var r="function"==typeof SuppressedError?SuppressedError:function(r,e){var n=Error();return n.name="SuppressedError",n.error=r,n.suppressed=e,n},e={},n=[];function using(r,e){if(null!=e){if(Object(e)!==e)throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");if(r)var o=e[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(void 0===o&&(o=e[Symbol.dispose||Symbol.for("Symbol.dispose")],r))var t=o;if("function"!=typeof o)throw new TypeError("Object is not disposable.");t&&(o=function(){try{t.call(e)}catch(r){return Promise.reject(r)}}),n.push({v:e,d:o,a:r})}else r&&n.push({d:e,a:r});return e}return{e:e,u:using.bind(null,!1),a:using.bind(null,!0),d:function(){var o,t=this.e,s=0;function next(){for(;o=n.pop();)try{if(!o.a&&1===s)return s=0,n.push(o),Promise.resolve().then(next);if(o.d){var r=o.d.call(o.v);if(o.a)return s|=2,Promise.resolve(r).then(next,err)}else s|=1}catch(r){return err(r)}if(1===s)return t!==e?Promise.reject(t):Promise.resolve();if(t!==e)throw t}function err(n){return t=t!==e?new r(n,t):n,next()}return next()}}}',
{
globals: [
"SuppressedError",
Expand Down
54 changes: 46 additions & 8 deletions packages/babel-helpers/src/helpers/usingCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ interface UsingCtxReturn {
d: DisposeLike;
}

const enum StateFlag {
NONE = 0,
NEEDS_AWAIT = 1,
HAS_AWAITED = 2,
}

export default function _usingCtx(): UsingCtxReturn {
var _disposeSuppressedError =
typeof SuppressedError === "function"
Expand Down Expand Up @@ -61,13 +67,26 @@ export default function _usingCtx(): UsingCtxReturn {
value as AsyncDisposable
)[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
}
if (dispose == null) {
if (dispose === undefined) {
dispose = (value as Disposable)[
Symbol.dispose || Symbol.for("Symbol.dispose")
];
if (isAwait) {
var inner = dispose;
}
}
if (typeof dispose !== "function") {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
throw new TypeError("Object is not disposable.");
}
if (inner) {
dispose = function () {
try {
inner.call(value);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
return Promise.reject(e);
}
};
}
stack.push({ v: value, d: dispose, a: isAwait });
} else if (isAwait) {
Expand All @@ -91,21 +110,40 @@ export default function _usingCtx(): UsingCtxReturn {
>(null, true),
// dispose
d: function () {
var error = this.e;
var error = this.e,
state: StateFlag = StateFlag.NONE,
resource;

function next(): Promise<void> | void {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
while ((resource = stack.pop())) {
try {
var resource,
disposalResult = resource.d && resource.d.call(resource.v);
if (resource.a) {
return Promise.resolve(disposalResult).then(next, err);
if (!resource.a && state === StateFlag.NEEDS_AWAIT) {
state = StateFlag.NONE;
stack.push(resource);
return Promise.resolve().then(next);
}
if (resource.d) {
var disposalResult = resource.d.call(resource.v);
if (resource.a) {
state |= StateFlag.HAS_AWAITED;
return Promise.resolve(disposalResult).then(next, err);
}
} else {
state |= StateFlag.NEEDS_AWAIT;
}
} catch (e) {
return err(e as Error);
}
}
if (state === StateFlag.NEEDS_AWAIT) {
if (error !== empty) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
return Promise.reject(error);
} else {
return Promise.resolve();
}
}

if (error !== empty) throw error;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
"proposal-explicit-resource-management",
"transform-optional-catch-binding",
"transform-async-to-generator"
],
"parserOpts": { "allowReturnOutsideFunction": true },
"minNodeVersion": "10.0.0"
}
Loading

0 comments on commit 2def809

Please sign in to comment.