async-browser-context 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +1 -1
- package/dist/babel/async-generator-helpers.d.ts +17 -0
- package/dist/babel/async-generator-helpers.d.ts.map +1 -0
- package/dist/babel/async-generator-helpers.js +40 -0
- package/dist/babel/async-generator-helpers.js.map +1 -0
- package/dist/babel/bind-generators.d.ts.map +1 -1
- package/dist/babel/bind-generators.js +17 -5
- package/dist/babel/bind-generators.js.map +1 -1
- package/dist/babel/preset.d.ts +6 -4
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +8 -4
- package/dist/babel/preset.js.map +1 -1
- package/dist/core/async-generator.d.ts +56 -0
- package/dist/core/async-generator.d.ts.map +1 -0
- package/dist/core/async-generator.js +211 -0
- package/dist/core/async-generator.js.map +1 -0
- package/dist/core/coroutine.d.ts +13 -3
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +116 -16
- package/dist/core/coroutine.js.map +1 -1
- package/dist/node/runtime.d.ts +8 -1
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +24 -0
- package/dist/node/runtime.js.map +1 -1
- package/dist/runtime.d.ts +4 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +4 -2
- package/dist/runtime.js.map +1 -1
- package/package.json +1 -1
- package/src/babel/async-generator-helpers.ts +49 -0
- package/src/babel/bind-generators.ts +17 -5
- package/src/babel/preset.ts +8 -4
- package/src/core/async-generator.ts +247 -0
- package/src/core/coroutine.ts +117 -16
- package/src/node/runtime.ts +27 -0
- package/src/runtime.ts +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Change history
|
|
2
2
|
|
|
3
|
+
## 0.2.4
|
|
4
|
+
|
|
5
|
+
### Corrections
|
|
6
|
+
|
|
7
|
+
- `yield*` in a transformed async generator gave a promise in place of each object of the delegate. Version 0.2.3 had this error. The driver does not change the result of a `yield*` step at this time.
|
|
8
|
+
- Transformed code can await a thenable that is not a native promise, or give it as the result of a function. Its `then` method operates in the context of the body at this time. Before, only the read of `then` had the context, and the call had the root context. A query builder that starts its request in `then`, for example of supabase-js, gets the context, as on Node.js.
|
|
9
|
+
- An `enterWith()` in the synchronous part of a transformed async function stays for the caller, as on Node.js.
|
|
10
|
+
- An async generator of transformed code is closer to a native async generator:
|
|
11
|
+
- `return(value)` at a `yield` awaits the value before the `finally` blocks. A rejection goes into the body at the `yield`.
|
|
12
|
+
- `next()` of a complete generator settles before a later microtask.
|
|
13
|
+
- Its methods give a rejected promise for an object that is not an async generator.
|
|
14
|
+
- The object inherits from `%AsyncIteratorPrototype%`, so `Symbol.asyncDispose` and the async iterator helpers are available, and its methods are not enumerable.
|
|
15
|
+
- An error of the `then` getter of an awaited object goes into the body at the `await`.
|
|
16
|
+
|
|
17
|
+
## 0.2.3
|
|
18
|
+
|
|
19
|
+
### Corrections
|
|
20
|
+
|
|
21
|
+
- Async generators are faster. The runtime has a driver for async generators (`asyncGenerator`), which continues each step with a native `await`. Before, the helper of Babel was in each module, and each step was a `then` callback. A `for await` loop over an async generator with 50,000 steps took 11.2 ms in Chromium, 560 ms in Firefox and 16 ms in WebKit. It takes 9.0 ms, 370 ms and 13 ms at this time (`pnpm bench:browsers`).
|
|
22
|
+
- The modules with async generators are smaller, because they do not contain the async generator helper of Babel. In the size measurement, idb changed from +31.9 to +8.1 percent and openai from +9.1 to +7.5 percent.
|
|
23
|
+
- The preset does not wrap async generators with `bindGenerator`. The driver keeps the context of the creation itself (rule C5).
|
|
24
|
+
|
|
25
|
+
### Tests
|
|
26
|
+
|
|
27
|
+
- The tests examine the behavior of async generators: the queue of requests, `throw()`, `return()` with `await` and `yield` in a `finally` block, and `yield*`. The project without a transform starts the same tests on native async generators.
|
|
28
|
+
|
|
3
29
|
## 0.2.2
|
|
4
30
|
|
|
5
31
|
### Corrections
|
package/README.md
CHANGED
|
@@ -276,7 +276,7 @@ When the page loads, the library installs its patches in approximately 8 ms: the
|
|
|
276
276
|
|
|
277
277
|
In Chromium, Firefox and WebKit, an `await` in transformed code takes approximately the time of a native `await` (`pnpm bench:browsers`). In Firefox, the patch of `then` adds approximately 0.8 µs to each `await` of a promise. The performance page of the website tells more.
|
|
278
278
|
|
|
279
|
-
The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds
|
|
279
|
+
The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 3 to 8 percent to packages with much async code, and nothing to React DOM. In an example application with React DOM, TanStack Query, supabase-js and ky, it adds 2.9 percent (`pnpm bench:size`).
|
|
280
280
|
|
|
281
281
|
## Tests
|
|
282
282
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type PluginObject, type PluginPass } from "@babel/core";
|
|
2
|
+
/** The options of the plugin. */
|
|
3
|
+
export interface AsyncGeneratorHelpersOptions {
|
|
4
|
+
/** The module that exports `asyncGenerator` and `OverloadYield`. */
|
|
5
|
+
readonly runtime: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The Babel plugin that gives the async generators to the driver of the runtime. It sets the
|
|
9
|
+
* `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
|
|
10
|
+
* `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
|
|
11
|
+
* become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
|
|
12
|
+
* file, and they make the `OverloadYield` values of the runtime.
|
|
13
|
+
*
|
|
14
|
+
* The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
|
|
15
|
+
*/
|
|
16
|
+
export declare function asyncGeneratorHelpersPlugin(_api: unknown, options: object): PluginObject<PluginPass>;
|
|
17
|
+
//# sourceMappingURL=async-generator-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-generator-helpers.d.ts","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGxE,iCAAiC;AACjC,MAAM,WAAW,4BAA4B;IACzC,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAUD;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAsBvG"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { types } from "@babel/core";
|
|
2
|
+
import { addNamed } from "@babel/helper-module-imports";
|
|
3
|
+
/** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
|
|
4
|
+
const REPLACED_HELPERS = {
|
|
5
|
+
wrapAsyncGenerator: "asyncGenerator",
|
|
6
|
+
OverloadYield: "OverloadYield",
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The Babel plugin that gives the async generators to the driver of the runtime. It sets the
|
|
10
|
+
* `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
|
|
11
|
+
* `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
|
|
12
|
+
* become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
|
|
13
|
+
* file, and they make the `OverloadYield` values of the runtime.
|
|
14
|
+
*
|
|
15
|
+
* The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
|
|
16
|
+
*/
|
|
17
|
+
export function asyncGeneratorHelpersPlugin(_api, options) {
|
|
18
|
+
const { runtime } = options;
|
|
19
|
+
return {
|
|
20
|
+
name: "async-browser-context/async-generator-helpers",
|
|
21
|
+
pre(file) {
|
|
22
|
+
const previous = file.get("helperGenerator");
|
|
23
|
+
const imports = new Map();
|
|
24
|
+
const generator = (name) => {
|
|
25
|
+
const exported = REPLACED_HELPERS[name];
|
|
26
|
+
if (exported === undefined) {
|
|
27
|
+
return previous?.(name);
|
|
28
|
+
}
|
|
29
|
+
let local = imports.get(exported);
|
|
30
|
+
if (local === undefined) {
|
|
31
|
+
local = addNamed(file.path, exported, runtime, { nameHint: exported });
|
|
32
|
+
imports.set(exported, local);
|
|
33
|
+
}
|
|
34
|
+
return types.cloneNode(local);
|
|
35
|
+
};
|
|
36
|
+
file.set("helperGenerator", generator);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=async-generator-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-generator-helpers.js","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAsC,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,+FAA+F;AAC/F,MAAM,gBAAgB,GAAsC;IACxD,kBAAkB,EAAG,gBAAgB;IACrC,aAAa,EAAG,eAAe;CAClC,CAAC;AAIF;;;;;;;;GAQG;AACH,MAAM,UAAU,2BAA2B,CAAC,IAAc,EAAE,OAAgB;IACxE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAuC,CAAC;IAC5D,OAAO;QACH,IAAI,EAAG,+CAA+C;QACtD,GAAG,CAAC,IAAyB;YACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAgC,CAAC;YAC5E,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;YACpD,MAAM,SAAS,GAAqB,CAAC,IAAI,EAAE,EAAE;gBACzC,MAAM,QAAQ,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;gBACxC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;oBACzB,OAAO,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;gBAC5B,CAAC;gBACD,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACtB,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAG,QAAQ,EAAE,CAAqB,CAAC;oBAC5F,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;gBACjC,CAAC;gBACD,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC,CAAC;YACF,IAAI,CAAC,GAAG,CAAC,iBAAiB,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAiFlD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAqChG"}
|
|
@@ -2,15 +2,26 @@ import { types } from "@babel/core";
|
|
|
2
2
|
import { addNamed } from "@babel/helper-module-imports";
|
|
3
3
|
/** The key of the file metadata that tells if the preset changed the file. */
|
|
4
4
|
export const METADATA_KEY = "asyncBrowserContext";
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The local names of the async generator helper: `wrapAsyncGenerator` of
|
|
7
|
+
* `@babel/plugin-transform-async-generator-functions`, or `asyncGenerator` of the runtime,
|
|
8
|
+
* which the preset gives in its place. Babel gives them an underscore.
|
|
9
|
+
*/
|
|
10
|
+
const ASYNC_GENERATOR_HELPER = /^_(wrapAsyncGenerator|asyncGenerator)\d*$/;
|
|
7
11
|
/** The local names that `addNamed` gives to `bindGenerator`. */
|
|
8
12
|
const BIND_GENERATOR_NAME = /^_bindGenerator\d*$/;
|
|
9
13
|
/** This function gives `true` if `path` is the generator that Babel gives to its async generator helper. */
|
|
10
14
|
function isAsyncGeneratorHelperArgument(path) {
|
|
11
15
|
const parent = path.parent;
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
if (!types.isCallExpression(parent)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (types.isIdentifier(parent.callee)) {
|
|
20
|
+
return ASYNC_GENERATOR_HELPER.test(parent.callee.name);
|
|
21
|
+
}
|
|
22
|
+
// CommonJS output of the preset: _runtime.asyncGenerator or (0, _runtime.asyncGenerator)
|
|
23
|
+
const member = types.isSequenceExpression(parent.callee) ? parent.callee.expressions.at(-1) : parent.callee;
|
|
24
|
+
return types.isMemberExpression(member) && types.isIdentifier(member.property, { name: "asyncGenerator" });
|
|
14
25
|
}
|
|
15
26
|
/**
|
|
16
27
|
* This function gives `true` if `callee` is the `bindGenerator` function of
|
|
@@ -83,7 +94,8 @@ export function bindGeneratorsPlugin(_api, options) {
|
|
|
83
94
|
if (node.async) {
|
|
84
95
|
changed = true;
|
|
85
96
|
}
|
|
86
|
-
|
|
97
|
+
// The driver of the runtime keeps the context of an async generator (asyncGenerator)
|
|
98
|
+
if (!node.generator || node.async || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
|
|
87
99
|
return;
|
|
88
100
|
}
|
|
89
101
|
changed = true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bind-generators.js","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqD,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAC;AAElD
|
|
1
|
+
{"version":3,"file":"bind-generators.js","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqD,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAC;AAElD;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,2CAA2C,CAAC;AAC3E,gEAAgE;AAChE,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAIlD,4GAA4G;AAC5G,SAAS,8BAA8B,CAAC,IAAmB;IACvD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC3B,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC;QAClC,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;QACpC,OAAO,sBAAsB,CAAC,IAAI,CAAC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
25
25
|
* 1. A plugin of this library skips the native async functions of the
|
|
26
26
|
* runtime, which a directive marks.
|
|
27
|
-
* 2. A plugin of this library binds each generator to the context of
|
|
28
|
-
* creation.
|
|
27
|
+
* 2. A plugin of this library binds each sync generator to the context of
|
|
28
|
+
* its creation.
|
|
29
29
|
* 3. A plugin of this library gives the name of each async function to
|
|
30
30
|
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
31
31
|
* this step.
|
|
32
|
-
* 4.
|
|
32
|
+
* 4. A plugin of this library gives the async generators to `asyncGenerator`
|
|
33
|
+
* of the runtime, in place of the Babel helper.
|
|
34
|
+
* 5. `@babel/plugin-transform-async-generator-functions` changes async
|
|
33
35
|
* generators and `for await` loops.
|
|
34
|
-
*
|
|
36
|
+
* 6. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
35
37
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
36
38
|
*
|
|
37
39
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAQvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAc9G"}
|
package/dist/babel/preset.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
|
|
2
2
|
import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
|
|
3
|
+
import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
|
|
3
4
|
import { bindGeneratorsPlugin } from "./bind-generators.js";
|
|
4
5
|
import { nameCoroutinesPlugin } from "./name-coroutines.js";
|
|
5
6
|
import { skipRuntimePlugin } from "./skip-runtime.js";
|
|
@@ -13,14 +14,16 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
13
14
|
*
|
|
14
15
|
* 1. A plugin of this library skips the native async functions of the
|
|
15
16
|
* runtime, which a directive marks.
|
|
16
|
-
* 2. A plugin of this library binds each generator to the context of
|
|
17
|
-
* creation.
|
|
17
|
+
* 2. A plugin of this library binds each sync generator to the context of
|
|
18
|
+
* its creation.
|
|
18
19
|
* 3. A plugin of this library gives the name of each async function to
|
|
19
20
|
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
20
21
|
* this step.
|
|
21
|
-
* 4.
|
|
22
|
+
* 4. A plugin of this library gives the async generators to `asyncGenerator`
|
|
23
|
+
* of the runtime, in place of the Babel helper.
|
|
24
|
+
* 5. `@babel/plugin-transform-async-generator-functions` changes async
|
|
22
25
|
* generators and `for await` loops.
|
|
23
|
-
*
|
|
26
|
+
* 6. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
24
27
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
25
28
|
*
|
|
26
29
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -35,6 +38,7 @@ export default function asyncBrowserContextPreset(api, options = {}) {
|
|
|
35
38
|
skipRuntimePlugin,
|
|
36
39
|
[bindGeneratorsPlugin, { runtime }],
|
|
37
40
|
...names,
|
|
41
|
+
[asyncGeneratorHelpersPlugin, { runtime }],
|
|
38
42
|
asyncGeneratorFunctions,
|
|
39
43
|
[asyncToGenerator, { module: runtime, method: "coroutine" }],
|
|
40
44
|
],
|
package/dist/babel/preset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D
|
|
1
|
+
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC;IACnD,MAAM,KAAK,GAAkB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,OAAO;QACH,OAAO,EAAG;YACN,iBAAiB;YACjB,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,CAAC,2BAA2B,EAAE,EAAE,OAAO,EAAE,CAAC;YAC1C,uBAAuB;YACvB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
|
|
3
|
+
* changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
|
|
4
|
+
* The helper continued each step in a `then` callback, and a `then` callback is slow in
|
|
5
|
+
* Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
|
|
6
|
+
* helper `OverloadYield` with the class of this module. The driver in this module then
|
|
7
|
+
* continues each step with a native `await`.
|
|
8
|
+
*
|
|
9
|
+
* The protocol is the protocol of the Babel 8 helper: the body gives
|
|
10
|
+
* `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
|
|
11
|
+
* step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
|
|
12
|
+
* generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
|
|
13
|
+
* value first, and a request on a complete generator settles at once.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
|
|
17
|
+
* The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class OverloadYield {
|
|
20
|
+
readonly v: unknown;
|
|
21
|
+
readonly k: 0 | 1;
|
|
22
|
+
constructor(value: unknown, kind: 0 | 1);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
|
|
26
|
+
* awaited object one time, in the context of the body. When the object has no `then` method,
|
|
27
|
+
* the driver awaits the box. Thus the engine does not read `then` a second time, and the
|
|
28
|
+
* `await` takes the same microtask turns.
|
|
29
|
+
*/
|
|
30
|
+
export declare class PlainValue {
|
|
31
|
+
readonly value: unknown;
|
|
32
|
+
constructor(value: unknown);
|
|
33
|
+
}
|
|
34
|
+
/** This function gives the value in a box, or the value itself. */
|
|
35
|
+
export declare function unbox(value: unknown): unknown;
|
|
36
|
+
/** The methods of a generator that a request of the consumer starts. */
|
|
37
|
+
export type ResumeKey = "next" | "throw" | "return";
|
|
38
|
+
/**
|
|
39
|
+
* This function does one step of the body: `generator[key](argument)`. The browser runtime sets
|
|
40
|
+
* the context of the generator around the step. `resumed` is `true` for a step after an
|
|
41
|
+
* `await` of the driver. The function gives the result of the generator without a change.
|
|
42
|
+
*/
|
|
43
|
+
export type AsyncGeneratorStep = (key: ResumeKey, argument: unknown, resumed: boolean) => IteratorResult<unknown, unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* This function gives the value that the driver awaits in place of `value`. The browser runtime
|
|
46
|
+
* reads the `then` property in the context of the generator. The Node.js runtime gives the
|
|
47
|
+
* value without a change.
|
|
48
|
+
*/
|
|
49
|
+
export type Prepare = (value: unknown) => unknown;
|
|
50
|
+
/**
|
|
51
|
+
* This function makes an async generator object. The object queues the requests of the
|
|
52
|
+
* consumer, and starts one request at a time, as a native async generator does. `enter`
|
|
53
|
+
* starts the driver: the Node.js runtime starts it in the context of the generator.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createAsyncGenerator(step: AsyncGeneratorStep, PromiseConstructor: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>, prepare?: Prepare): AsyncGenerator<unknown, unknown, unknown>;
|
|
56
|
+
//# sourceMappingURL=async-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAKH;;;GAGG;AACH,qBAAa,aAAa;IACtB,QAAQ,CAAC,CAAC,EAAG,OAAO,CAAC;IACrB,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,CAAC;gBACP,KAAK,EAAG,OAAO,EAAE,IAAI,EAAG,CAAC,GAAG,CAAC;CAI5C;AAED;;;;;GAKG;AACH,qBAAa,UAAU;IACnB,QAAQ,CAAC,KAAK,EAAG,OAAO,CAAC;gBACb,KAAK,EAAG,OAAO;CAG9B;AAED,mEAAmE;AACnE,wBAAgB,KAAK,CAAC,KAAK,EAAG,OAAO,GAAI,OAAO,CAE/C;AAED,wEAAwE;AACxE,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEpD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAG,SAAS,EAAE,QAAQ,EAAG,OAAO,EAAE,OAAO,EAAG,OAAO,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAE9H;;;;GAIG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,KAAK,EAAG,OAAO,KAAK,OAAO,CAAC;AA8DnD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAG,kBAAkB,EACzB,kBAAkB,EAAG,kBAAkB,EACvC,KAAK,GAAG,CAAC,KAAK,EAAG,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAsB,EAC3E,OAAO,GAAG,OAA0B,GACpC,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CA8G5C"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
|
|
3
|
+
* changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
|
|
4
|
+
* The helper continued each step in a `then` callback, and a `then` callback is slow in
|
|
5
|
+
* Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
|
|
6
|
+
* helper `OverloadYield` with the class of this module. The driver in this module then
|
|
7
|
+
* continues each step with a native `await`.
|
|
8
|
+
*
|
|
9
|
+
* The protocol is the protocol of the Babel 8 helper: the body gives
|
|
10
|
+
* `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
|
|
11
|
+
* step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
|
|
12
|
+
* generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
|
|
13
|
+
* value first, and a request on a complete generator settles at once.
|
|
14
|
+
*/
|
|
15
|
+
/** The Promise of the time when the module loads, for the rejections of the methods. */
|
|
16
|
+
const NativePromise = Promise;
|
|
17
|
+
/**
|
|
18
|
+
* The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
|
|
19
|
+
* The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
|
|
20
|
+
*/
|
|
21
|
+
export class OverloadYield {
|
|
22
|
+
v;
|
|
23
|
+
k;
|
|
24
|
+
constructor(value, kind) {
|
|
25
|
+
this.v = value;
|
|
26
|
+
this.k = kind;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
|
|
31
|
+
* awaited object one time, in the context of the body. When the object has no `then` method,
|
|
32
|
+
* the driver awaits the box. Thus the engine does not read `then` a second time, and the
|
|
33
|
+
* `await` takes the same microtask turns.
|
|
34
|
+
*/
|
|
35
|
+
export class PlainValue {
|
|
36
|
+
value;
|
|
37
|
+
constructor(value) {
|
|
38
|
+
this.value = value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** This function gives the value in a box, or the value itself. */
|
|
42
|
+
export function unbox(value) {
|
|
43
|
+
return value instanceof PlainValue ? value.value : value;
|
|
44
|
+
}
|
|
45
|
+
/** The key of the request function of each async generator object. */
|
|
46
|
+
const INVOKE = Symbol("async-browser-context/invoke");
|
|
47
|
+
/** This function starts a request. For an object that is not an async generator of the runtime, it gives a rejected promise, as a native method does. */
|
|
48
|
+
function request(target, key, argument) {
|
|
49
|
+
const invoke = typeof target === "object" && target !== null ? target[INVOKE] : undefined;
|
|
50
|
+
if (invoke === undefined) {
|
|
51
|
+
return NativePromise.reject(new TypeError(`AsyncGenerator.prototype.${key} started on an object that is not an async generator`));
|
|
52
|
+
}
|
|
53
|
+
return invoke(key, argument);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This function gives %AsyncIteratorPrototype% of the engine, which gives `Symbol.asyncIterator`,
|
|
57
|
+
* `Symbol.asyncDispose` and the async iterator helpers. If a compiler changed the async
|
|
58
|
+
* generator below, the function gives `undefined`.
|
|
59
|
+
*/
|
|
60
|
+
function asyncIteratorPrototype() {
|
|
61
|
+
try {
|
|
62
|
+
const generatorFunction = async function* () {
|
|
63
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
64
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
65
|
+
"async-browser-context runtime";
|
|
66
|
+
};
|
|
67
|
+
const prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype));
|
|
68
|
+
return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The prototype of the async generator objects of transformed code. Its methods are not enumerable, as native methods. */
|
|
75
|
+
const AsyncGeneratorPrototype = (() => {
|
|
76
|
+
const parent = asyncIteratorPrototype();
|
|
77
|
+
const prototype = Object.create(parent ?? Object.prototype);
|
|
78
|
+
const methods = {
|
|
79
|
+
next(value) { return request(this, "next", value); },
|
|
80
|
+
throw(error) { return request(this, "throw", error); },
|
|
81
|
+
return(value) { return request(this, "return", value); },
|
|
82
|
+
};
|
|
83
|
+
for (const key of ["next", "throw", "return"]) {
|
|
84
|
+
Object.defineProperty(prototype, key, { value: methods[key], writable: true, configurable: true });
|
|
85
|
+
}
|
|
86
|
+
if (parent === undefined) {
|
|
87
|
+
Object.defineProperty(prototype, Symbol.asyncIterator, { value: function asyncIterator() { return this; }, writable: true, configurable: true });
|
|
88
|
+
}
|
|
89
|
+
Object.defineProperty(prototype, Symbol.toStringTag, { value: "AsyncGenerator", configurable: true });
|
|
90
|
+
return prototype;
|
|
91
|
+
})();
|
|
92
|
+
/**
|
|
93
|
+
* This function makes an async generator object. The object queues the requests of the
|
|
94
|
+
* consumer, and starts one request at a time, as a native async generator does. `enter`
|
|
95
|
+
* starts the driver: the Node.js runtime starts it in the context of the generator.
|
|
96
|
+
*/
|
|
97
|
+
export function createAsyncGenerator(step, PromiseConstructor, enter = (start) => start(), prepare = (value) => value) {
|
|
98
|
+
let first;
|
|
99
|
+
let last;
|
|
100
|
+
/** The state of the body: not started, suspended at a `yield`, or complete. While a step operates, the requests wait in the queue. */
|
|
101
|
+
let state = "start";
|
|
102
|
+
/** This function starts the requests in the queue, one after the other, until the queue is empty. */
|
|
103
|
+
const run = async (key, argument) => {
|
|
104
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
105
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
106
|
+
"async-browser-context runtime";
|
|
107
|
+
let resumed = false;
|
|
108
|
+
for (;;) {
|
|
109
|
+
let failed = false;
|
|
110
|
+
let settled;
|
|
111
|
+
let done = false;
|
|
112
|
+
try {
|
|
113
|
+
if (state === "done" || (state === "start" && key !== "next")) {
|
|
114
|
+
// The body does not operate: it is complete, or return() or throw() came before the start
|
|
115
|
+
state = "done";
|
|
116
|
+
done = true;
|
|
117
|
+
if (key === "throw") {
|
|
118
|
+
throw argument;
|
|
119
|
+
}
|
|
120
|
+
if (key === "return") {
|
|
121
|
+
// As AsyncGeneratorAwaitReturn, return() awaits its value
|
|
122
|
+
settled = await argument;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
if (key === "return") {
|
|
127
|
+
// At a yield, return() awaits its value first. A rejection goes into the body at the yield.
|
|
128
|
+
try {
|
|
129
|
+
argument = unbox(await prepare(argument));
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
key = "throw";
|
|
133
|
+
argument = error;
|
|
134
|
+
}
|
|
135
|
+
resumed = true;
|
|
136
|
+
}
|
|
137
|
+
let result = step(key, argument, resumed);
|
|
138
|
+
for (;;) {
|
|
139
|
+
const yielded = result.value;
|
|
140
|
+
const overloaded = yielded instanceof OverloadYield;
|
|
141
|
+
let value;
|
|
142
|
+
try {
|
|
143
|
+
value = unbox(await prepare(overloaded ? yielded.v : yielded));
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
resumed = true;
|
|
147
|
+
key = "throw";
|
|
148
|
+
result = step("throw", error, true);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
resumed = true;
|
|
152
|
+
if (overloaded) {
|
|
153
|
+
const nextKey = key === "return" && yielded.k === 1 ? "return" : "next";
|
|
154
|
+
if (yielded.k === 0 || Boolean(value.done)) {
|
|
155
|
+
key = nextKey;
|
|
156
|
+
result = step(nextKey, value, true);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
// A step of yield*: the body gives the result of the delegate back without a change.
|
|
160
|
+
// Its value is not awaited, as with a native yield*.
|
|
161
|
+
const passed = step(nextKey, value, true);
|
|
162
|
+
if (passed !== value) {
|
|
163
|
+
result = passed;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
value = passed.value;
|
|
167
|
+
}
|
|
168
|
+
settled = value;
|
|
169
|
+
done = result.done === true;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
state = done ? "done" : "yield";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
state = "done";
|
|
177
|
+
failed = true;
|
|
178
|
+
settled = error;
|
|
179
|
+
}
|
|
180
|
+
// A request always waits in the queue while the driver runs
|
|
181
|
+
const current = first;
|
|
182
|
+
first = current.next;
|
|
183
|
+
if (failed) {
|
|
184
|
+
current.reject(settled);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
current.resolve({ value: settled, done });
|
|
188
|
+
}
|
|
189
|
+
if (first === undefined) {
|
|
190
|
+
last = undefined;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
key = first.key;
|
|
194
|
+
argument = first.argument;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const invoke = (key, argument) => new PromiseConstructor((resolve, reject) => {
|
|
198
|
+
const added = { key, argument, resolve, reject, next: undefined };
|
|
199
|
+
if (last === undefined) {
|
|
200
|
+
first = last = added;
|
|
201
|
+
void enter(() => run(key, argument));
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
last = last.next = added;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
const generator = Object.create(AsyncGeneratorPrototype);
|
|
208
|
+
Object.defineProperty(generator, INVOKE, { value: invoke });
|
|
209
|
+
return generator;
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=async-generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-generator.js","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;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AQ,GAAG,KAAK,CAAC,QAAQ,CAAC;QAC9B,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,MAAM,GAAY,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI,kBAAkB,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAClF,MAAM,KAAK,GAAa,EAAE,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAG,SAAS,EAAE,CAAC;QAC7E,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;YACrB,KAAK,KAAK,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;QACzC,CAAC;aAAM,CAAC;YACJ,IAAI,GAAG,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAC7B,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAA8C,CAAC;IACtG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,KAAK,EAAG,MAAM,EAAE,CAAC,CAAC;IAC7D,OAAO,SAAS,CAAC;AACrB,CAAC"}
|
package/dist/core/coroutine.d.ts
CHANGED
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
5
5
|
*/
|
|
6
6
|
export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>, name?: string): (this: T, ...args: A) => Promise<R>;
|
|
7
|
+
/**
|
|
8
|
+
* This function gives a function that makes an async generator object, which operates
|
|
9
|
+
* `generatorFunction`. The preset gives this function in place of the helper
|
|
10
|
+
* `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
|
|
11
|
+
*
|
|
12
|
+
* Each step of the body operates in the context of the call that made the generator
|
|
13
|
+
* (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
|
|
7
16
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
8
17
|
/**
|
|
9
18
|
* This function binds a generator object to the current context. The body of
|
|
@@ -11,9 +20,10 @@ type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown,
|
|
|
11
20
|
* proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
|
|
12
21
|
* the context of the caller is current again.
|
|
13
22
|
*
|
|
14
|
-
* The preset changes each generator function so that it gives
|
|
15
|
-
* `bindGenerator(generator)`.
|
|
16
|
-
*
|
|
23
|
+
* The preset changes each sync generator function so that it gives
|
|
24
|
+
* `bindGenerator(generator)`. An async generator does not need it, because
|
|
25
|
+
* `asyncGenerator` keeps the context itself. The function also works with
|
|
26
|
+
* async generator objects, for example in the output of older versions.
|
|
17
27
|
*/
|
|
18
28
|
export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
|
|
19
29
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coroutine.d.ts","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"coroutine.d.ts","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AA8EA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC/C,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAC7E,IAAI,CAAC,EAAG,MAAM,GACd,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CA2DxC;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EACjD,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GACnF,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAoCvE;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CA2BtE"}
|