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
|
@@ -0,0 +1,247 @@
|
|
|
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 Promise of the time when the module loads, for the rejections of the methods. */
|
|
17
|
+
const NativePromise : PromiseConstructor = Promise;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
|
|
21
|
+
* The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
|
|
22
|
+
*/
|
|
23
|
+
export class OverloadYield {
|
|
24
|
+
readonly v : unknown;
|
|
25
|
+
readonly k : 0 | 1;
|
|
26
|
+
constructor(value : unknown, kind : 0 | 1) {
|
|
27
|
+
this.v = value;
|
|
28
|
+
this.k = kind;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
|
|
34
|
+
* awaited object one time, in the context of the body. When the object has no `then` method,
|
|
35
|
+
* the driver awaits the box. Thus the engine does not read `then` a second time, and the
|
|
36
|
+
* `await` takes the same microtask turns.
|
|
37
|
+
*/
|
|
38
|
+
export class PlainValue {
|
|
39
|
+
readonly value : unknown;
|
|
40
|
+
constructor(value : unknown) {
|
|
41
|
+
this.value = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** This function gives the value in a box, or the value itself. */
|
|
46
|
+
export function unbox(value : unknown) : unknown {
|
|
47
|
+
return value instanceof PlainValue ? value.value : value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The methods of a generator that a request of the consumer starts. */
|
|
51
|
+
export type ResumeKey = "next" | "throw" | "return";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* This function does one step of the body: `generator[key](argument)`. The browser runtime sets
|
|
55
|
+
* the context of the generator around the step. `resumed` is `true` for a step after an
|
|
56
|
+
* `await` of the driver. The function gives the result of the generator without a change.
|
|
57
|
+
*/
|
|
58
|
+
export type AsyncGeneratorStep = (key : ResumeKey, argument : unknown, resumed : boolean) => IteratorResult<unknown, unknown>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* This function gives the value that the driver awaits in place of `value`. The browser runtime
|
|
62
|
+
* reads the `then` property in the context of the generator. The Node.js runtime gives the
|
|
63
|
+
* value without a change.
|
|
64
|
+
*/
|
|
65
|
+
export type Prepare = (value : unknown) => unknown;
|
|
66
|
+
|
|
67
|
+
interface Request {
|
|
68
|
+
readonly key : ResumeKey;
|
|
69
|
+
readonly argument : unknown;
|
|
70
|
+
readonly resolve : (result : IteratorResult<unknown, unknown>) => void;
|
|
71
|
+
readonly reject : (error : unknown) => void;
|
|
72
|
+
next : Request | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Invoke = (key : ResumeKey, argument : unknown) => Promise<IteratorResult<unknown, unknown>>;
|
|
76
|
+
|
|
77
|
+
/** The key of the request function of each async generator object. */
|
|
78
|
+
const INVOKE : unique symbol = Symbol("async-browser-context/invoke");
|
|
79
|
+
|
|
80
|
+
/** 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. */
|
|
81
|
+
function request(target : unknown, key : ResumeKey, argument : unknown) : Promise<IteratorResult<unknown, unknown>> {
|
|
82
|
+
const invoke = typeof target === "object" && target !== null ? (target as { [INVOKE]? : Invoke })[INVOKE] : undefined;
|
|
83
|
+
if (invoke === undefined) {
|
|
84
|
+
return NativePromise.reject(new TypeError(`AsyncGenerator.prototype.${key} started on an object that is not an async generator`));
|
|
85
|
+
}
|
|
86
|
+
return invoke(key, argument);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* This function gives %AsyncIteratorPrototype% of the engine, which gives `Symbol.asyncIterator`,
|
|
91
|
+
* `Symbol.asyncDispose` and the async iterator helpers. If a compiler changed the async
|
|
92
|
+
* generator below, the function gives `undefined`.
|
|
93
|
+
*/
|
|
94
|
+
function asyncIteratorPrototype() : object | undefined {
|
|
95
|
+
try {
|
|
96
|
+
const generatorFunction = async function* () {
|
|
97
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
98
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
99
|
+
"async-browser-context runtime";
|
|
100
|
+
};
|
|
101
|
+
const prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype) as object) as Record<symbol, unknown> | null;
|
|
102
|
+
return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
|
|
103
|
+
} catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The prototype of the async generator objects of transformed code. Its methods are not enumerable, as native methods. */
|
|
109
|
+
const AsyncGeneratorPrototype : object = (() => {
|
|
110
|
+
const parent = asyncIteratorPrototype();
|
|
111
|
+
const prototype = Object.create(parent ?? Object.prototype) as object;
|
|
112
|
+
const methods : Record<ResumeKey, (this : unknown, argument? : unknown) => Promise<IteratorResult<unknown, unknown>>> = {
|
|
113
|
+
next(value) { return request(this, "next", value); },
|
|
114
|
+
throw(error) { return request(this, "throw", error); },
|
|
115
|
+
return(value) { return request(this, "return", value); },
|
|
116
|
+
};
|
|
117
|
+
for (const key of ["next", "throw", "return"] as const) {
|
|
118
|
+
Object.defineProperty(prototype, key, { value : methods[key], writable : true, configurable : true });
|
|
119
|
+
}
|
|
120
|
+
if (parent === undefined) {
|
|
121
|
+
Object.defineProperty(prototype, Symbol.asyncIterator, { value : function asyncIterator(this : unknown) { return this; }, writable : true, configurable : true });
|
|
122
|
+
}
|
|
123
|
+
Object.defineProperty(prototype, Symbol.toStringTag, { value : "AsyncGenerator", configurable : true });
|
|
124
|
+
return prototype;
|
|
125
|
+
})();
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* This function makes an async generator object. The object queues the requests of the
|
|
129
|
+
* consumer, and starts one request at a time, as a native async generator does. `enter`
|
|
130
|
+
* starts the driver: the Node.js runtime starts it in the context of the generator.
|
|
131
|
+
*/
|
|
132
|
+
export function createAsyncGenerator(
|
|
133
|
+
step : AsyncGeneratorStep,
|
|
134
|
+
PromiseConstructor : PromiseConstructor,
|
|
135
|
+
enter : (start : () => Promise<void>) => Promise<void> = (start) => start(),
|
|
136
|
+
prepare : Prepare = (value) => value,
|
|
137
|
+
) : AsyncGenerator<unknown, unknown, unknown> {
|
|
138
|
+
let first : Request | undefined;
|
|
139
|
+
let last : Request | undefined;
|
|
140
|
+
/** The state of the body: not started, suspended at a `yield`, or complete. While a step operates, the requests wait in the queue. */
|
|
141
|
+
let state : "start" | "yield" | "done" = "start";
|
|
142
|
+
|
|
143
|
+
/** This function starts the requests in the queue, one after the other, until the queue is empty. */
|
|
144
|
+
const run = async (key : ResumeKey, argument : unknown) : Promise<void> => {
|
|
145
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
146
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
147
|
+
"async-browser-context runtime";
|
|
148
|
+
let resumed = false;
|
|
149
|
+
for (;;) {
|
|
150
|
+
let failed = false;
|
|
151
|
+
let settled : unknown;
|
|
152
|
+
let done = false;
|
|
153
|
+
try {
|
|
154
|
+
if (state === "done" || (state === "start" && key !== "next")) {
|
|
155
|
+
// The body does not operate: it is complete, or return() or throw() came before the start
|
|
156
|
+
state = "done";
|
|
157
|
+
done = true;
|
|
158
|
+
if (key === "throw") {
|
|
159
|
+
throw argument;
|
|
160
|
+
}
|
|
161
|
+
if (key === "return") {
|
|
162
|
+
// As AsyncGeneratorAwaitReturn, return() awaits its value
|
|
163
|
+
settled = await argument;
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
if (key === "return") {
|
|
167
|
+
// At a yield, return() awaits its value first. A rejection goes into the body at the yield.
|
|
168
|
+
try {
|
|
169
|
+
argument = unbox(await prepare(argument));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
key = "throw";
|
|
172
|
+
argument = error;
|
|
173
|
+
}
|
|
174
|
+
resumed = true;
|
|
175
|
+
}
|
|
176
|
+
let result = step(key, argument, resumed);
|
|
177
|
+
for (;;) {
|
|
178
|
+
const yielded : unknown = result.value;
|
|
179
|
+
const overloaded = yielded instanceof OverloadYield;
|
|
180
|
+
let value : unknown;
|
|
181
|
+
try {
|
|
182
|
+
value = unbox(await prepare(overloaded ? yielded.v : yielded));
|
|
183
|
+
} catch (error) {
|
|
184
|
+
resumed = true;
|
|
185
|
+
key = "throw";
|
|
186
|
+
result = step("throw", error, true);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
resumed = true;
|
|
190
|
+
if (overloaded) {
|
|
191
|
+
const nextKey : ResumeKey = key === "return" && yielded.k === 1 ? "return" : "next";
|
|
192
|
+
if (yielded.k === 0 || Boolean((value as { done? : unknown }).done)) {
|
|
193
|
+
key = nextKey;
|
|
194
|
+
result = step(nextKey, value, true);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
// A step of yield*: the body gives the result of the delegate back without a change.
|
|
198
|
+
// Its value is not awaited, as with a native yield*.
|
|
199
|
+
const passed = step(nextKey, value, true);
|
|
200
|
+
if (passed !== value) {
|
|
201
|
+
result = passed;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
value = passed.value;
|
|
205
|
+
}
|
|
206
|
+
settled = value;
|
|
207
|
+
done = result.done === true;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
state = done ? "done" : "yield";
|
|
211
|
+
}
|
|
212
|
+
} catch (error) {
|
|
213
|
+
state = "done";
|
|
214
|
+
failed = true;
|
|
215
|
+
settled = error;
|
|
216
|
+
}
|
|
217
|
+
// A request always waits in the queue while the driver runs
|
|
218
|
+
const current = first as Request;
|
|
219
|
+
first = current.next;
|
|
220
|
+
if (failed) {
|
|
221
|
+
current.reject(settled);
|
|
222
|
+
} else {
|
|
223
|
+
current.resolve({ value : settled, done });
|
|
224
|
+
}
|
|
225
|
+
if (first === undefined) {
|
|
226
|
+
last = undefined;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
key = first.key;
|
|
230
|
+
argument = first.argument;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const invoke : Invoke = (key, argument) => new PromiseConstructor((resolve, reject) => {
|
|
235
|
+
const added : Request = { key, argument, resolve, reject, next : undefined };
|
|
236
|
+
if (last === undefined) {
|
|
237
|
+
first = last = added;
|
|
238
|
+
void enter(() => run(key, argument));
|
|
239
|
+
} else {
|
|
240
|
+
last = last.next = added;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const generator = Object.create(AsyncGeneratorPrototype) as AsyncGenerator<unknown, unknown, unknown>;
|
|
245
|
+
Object.defineProperty(generator, INVOKE, { value : invoke });
|
|
246
|
+
return generator;
|
|
247
|
+
}
|
package/src/core/coroutine.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createAsyncGenerator, PlainValue, unbox } from "./async-generator.js";
|
|
1
2
|
import { applyWithHooks, store, type Frame } from "./store.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -23,6 +24,58 @@ function namedForHooks(generatorFunction : Function, name : string | undefined)
|
|
|
23
24
|
return generatorFunction;
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
29
|
+
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
30
|
+
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
31
|
+
*/
|
|
32
|
+
/** This function gives `true` for a value that can have a `then` property: an object or a function. */
|
|
33
|
+
function isObject(value : unknown) : value is object {
|
|
34
|
+
return (typeof value === "object" && value !== null) || typeof value === "function";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* This function gives the value that a driver awaits or gives as its result, in place of `value`. It
|
|
39
|
+
* operates in the context of the body, as `await` reads the `then` property there:
|
|
40
|
+
*
|
|
41
|
+
* - A primitive value and a native promise stay the same. `await` does not read a `then`
|
|
42
|
+
* property of them.
|
|
43
|
+
* - For another object, the function reads `then` one time. A `then` method gets the current
|
|
44
|
+
* frame for its later call. Thus the method operates in the context of the body, as with
|
|
45
|
+
* the native `AsyncLocalStorage`. With `box`, an object without a `then` method goes into a
|
|
46
|
+
* box, so that the engine does not read `then` a second time.
|
|
47
|
+
* - When the read throws, the function gives a rejected promise: the error goes into the body.
|
|
48
|
+
*/
|
|
49
|
+
function awaitable(value : unknown, NativePromise : PromiseConstructor, box : boolean) : unknown {
|
|
50
|
+
if (!isObject(value)) {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
let then : unknown;
|
|
54
|
+
try {
|
|
55
|
+
if (value instanceof NativePromise && value.constructor === NativePromise) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
then = (value as { then? : unknown }).then;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
return new NativePromise((_resolve, reject) => reject(error));
|
|
61
|
+
}
|
|
62
|
+
if (typeof then !== "function") {
|
|
63
|
+
return box ? new PlainValue(value) : value;
|
|
64
|
+
}
|
|
65
|
+
const frame = store.current;
|
|
66
|
+
return {
|
|
67
|
+
then(this : unknown, resolve : unknown, reject : unknown) : unknown {
|
|
68
|
+
const previous = store.current;
|
|
69
|
+
store.current = frame;
|
|
70
|
+
try {
|
|
71
|
+
return Reflect.apply(then, value, [resolve, reject]);
|
|
72
|
+
} finally {
|
|
73
|
+
store.current = previous;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
26
79
|
/**
|
|
27
80
|
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
28
81
|
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
@@ -32,7 +85,7 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
32
85
|
generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>,
|
|
33
86
|
name? : string,
|
|
34
87
|
) : (this : T, ...args : A) => Promise<R> {
|
|
35
|
-
const
|
|
88
|
+
const NativePromise = store.intrinsics.Promise;
|
|
36
89
|
return function asyncFunction(this : T, ...args : A) : Promise<R> {
|
|
37
90
|
// The context of the call. The body can change it with AsyncLocalStorage.enterWith.
|
|
38
91
|
let frame : Frame = store.current;
|
|
@@ -41,9 +94,11 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
41
94
|
const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
|
|
42
95
|
|
|
43
96
|
/**
|
|
44
|
-
* This function does one step of the body in the context of the async function
|
|
45
|
-
*
|
|
46
|
-
*
|
|
97
|
+
* This function does one step of the body in the context of the async function. `resumed`
|
|
98
|
+
* is `true` for a step after an `await`. Only these steps start the hook methods. After a
|
|
99
|
+
* resumed step, the function sets the previous context again. The first step operates in
|
|
100
|
+
* the code of the caller, so an `enterWith` of that step stays for the caller, as on
|
|
101
|
+
* Node.js. The function gives the value to await, or the result of the function.
|
|
47
102
|
*/
|
|
48
103
|
const step = (resume : (argument : unknown) => IteratorResult<unknown, R>, argument : unknown, resumed : boolean) : IteratorResult<unknown, R> => {
|
|
49
104
|
const previous = store.current;
|
|
@@ -53,16 +108,12 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
53
108
|
const result = hooks === undefined
|
|
54
109
|
? resume(argument)
|
|
55
110
|
: applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
|
|
59
|
-
// The result object is new for each step, so the change is not visible to other code.
|
|
60
|
-
(result as { value : unknown }).value = promiseResolve(value);
|
|
61
|
-
}
|
|
111
|
+
// The result object is new for each step, so the change is not visible to other code
|
|
112
|
+
(result as { value : unknown }).value = awaitable(result.value, NativePromise, result.done !== true);
|
|
62
113
|
return result;
|
|
63
114
|
} finally {
|
|
64
115
|
frame = store.current;
|
|
65
|
-
store.current = previous;
|
|
116
|
+
store.current = resumed ? previous : frame;
|
|
66
117
|
}
|
|
67
118
|
};
|
|
68
119
|
|
|
@@ -73,14 +124,15 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
73
124
|
*/
|
|
74
125
|
const drive = async () : Promise<R> => {
|
|
75
126
|
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
127
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
76
128
|
"async-browser-context runtime";
|
|
77
129
|
// In the driver, an error of the parameters rejects the promise, as with a native async function
|
|
78
|
-
generator =
|
|
130
|
+
generator = Reflect.apply(generatorFunction, this, args);
|
|
79
131
|
let result = step(next, undefined, false);
|
|
80
132
|
while (result.done !== true) {
|
|
81
133
|
let value : unknown;
|
|
82
134
|
try {
|
|
83
|
-
value = await result.value;
|
|
135
|
+
value = unbox(await result.value);
|
|
84
136
|
} catch (error) {
|
|
85
137
|
result = step(throwInto, error, true);
|
|
86
138
|
continue;
|
|
@@ -93,6 +145,54 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
93
145
|
};
|
|
94
146
|
}
|
|
95
147
|
|
|
148
|
+
/**
|
|
149
|
+
* This function gives a function that makes an async generator object, which operates
|
|
150
|
+
* `generatorFunction`. The preset gives this function in place of the helper
|
|
151
|
+
* `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
|
|
152
|
+
*
|
|
153
|
+
* Each step of the body operates in the context of the call that made the generator
|
|
154
|
+
* (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
|
|
155
|
+
*/
|
|
156
|
+
export function asyncGenerator<T, A extends unknown[]>(
|
|
157
|
+
generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
|
|
158
|
+
) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
|
|
159
|
+
const NativePromise = store.intrinsics.Promise;
|
|
160
|
+
return function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
|
|
161
|
+
let frame : Frame = store.current;
|
|
162
|
+
const generator = Reflect.apply(generatorFunction, this, args);
|
|
163
|
+
const methods : Record<ResumeMethod, (argument : unknown) => IteratorResult<unknown, unknown>> = {
|
|
164
|
+
next : (argument) => generator.next(argument),
|
|
165
|
+
throw : (argument) => generator.throw(argument),
|
|
166
|
+
return : (argument) => generator.return(argument),
|
|
167
|
+
};
|
|
168
|
+
/** One step of the body in the context of the generator. The result stays the same: `yield*` gives the result of the delegate back. */
|
|
169
|
+
const step = (key : ResumeMethod, argument : unknown, resumed : boolean) : IteratorResult<unknown, unknown> => {
|
|
170
|
+
const previous = store.current;
|
|
171
|
+
store.current = frame;
|
|
172
|
+
try {
|
|
173
|
+
const hooks = resumed ? store.hooks : undefined;
|
|
174
|
+
return hooks === undefined
|
|
175
|
+
? methods[key](argument)
|
|
176
|
+
: applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
|
|
177
|
+
} finally {
|
|
178
|
+
frame = store.current;
|
|
179
|
+
store.current = previous;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
/** The value to await, in the context of the generator: `await` and `yield` read the `then` property of an object there. */
|
|
183
|
+
const prepare = (value : unknown) : unknown => {
|
|
184
|
+
const previous = store.current;
|
|
185
|
+
store.current = frame;
|
|
186
|
+
try {
|
|
187
|
+
return awaitable(value, NativePromise, true);
|
|
188
|
+
} finally {
|
|
189
|
+
store.current = previous;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
return createAsyncGenerator(step, NativePromise, undefined, prepare);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
96
196
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
97
197
|
type ResumeMethod = "next" | "throw" | "return";
|
|
98
198
|
|
|
@@ -102,9 +202,10 @@ type ResumeMethod = "next" | "throw" | "return";
|
|
|
102
202
|
* proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
|
|
103
203
|
* the context of the caller is current again.
|
|
104
204
|
*
|
|
105
|
-
* The preset changes each generator function so that it gives
|
|
106
|
-
* `bindGenerator(generator)`.
|
|
107
|
-
*
|
|
205
|
+
* The preset changes each sync generator function so that it gives
|
|
206
|
+
* `bindGenerator(generator)`. An async generator does not need it, because
|
|
207
|
+
* `asyncGenerator` keeps the context itself. The function also works with
|
|
208
|
+
* async generator objects, for example in the output of older versions.
|
|
108
209
|
*/
|
|
109
210
|
export function bindGenerator<I extends AnyIterator>(generator : I) : I {
|
|
110
211
|
let frame : Frame = store.current;
|
package/src/node/runtime.ts
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
9
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
+
import { createAsyncGenerator, type ResumeKey } from "../core/async-generator.js";
|
|
11
|
+
|
|
12
|
+
export { OverloadYield } from "../core/async-generator.js";
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* This function gives an async function that operates `generatorFunction`. The function is
|
|
@@ -33,6 +36,30 @@ export function coroutine<T, A extends unknown[], R>(generatorFunction : (this :
|
|
|
33
36
|
};
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
/**
|
|
40
|
+
* This function gives a function that makes an async generator object, which operates
|
|
41
|
+
* `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
|
|
42
|
+
* Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
|
|
43
|
+
* native `AsyncLocalStorage` keeps that context through the `await` of the driver.
|
|
44
|
+
*/
|
|
45
|
+
export function asyncGenerator<T, A extends unknown[]>(
|
|
46
|
+
generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
|
|
47
|
+
) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
|
|
48
|
+
return function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
|
|
49
|
+
let snapshot = AsyncLocalStorage.snapshot();
|
|
50
|
+
const generator = generatorFunction.apply(this, args);
|
|
51
|
+
const step = (key : ResumeKey, argument : unknown) : IteratorResult<unknown, unknown> => {
|
|
52
|
+
try {
|
|
53
|
+
return generator[key](argument);
|
|
54
|
+
} finally {
|
|
55
|
+
// The body can change its context with enterWith before it stops
|
|
56
|
+
snapshot = AsyncLocalStorage.snapshot();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return createAsyncGenerator(step, Promise, (start) => snapshot(start));
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
36
63
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
37
64
|
type ResumeMethod = "next" | "throw" | "return";
|
|
38
65
|
|
package/src/runtime.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The runtime of the code that the Babel preset transforms. The transformed
|
|
3
|
-
* code imports `coroutine
|
|
3
|
+
* code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
|
|
4
|
+
* `OverloadYield` from this module. When the
|
|
4
5
|
* module loads, it installs the patches of the runtime.
|
|
5
6
|
*
|
|
6
7
|
* @packageDocumentation
|
|
7
8
|
*/
|
|
8
9
|
import "./install.js";
|
|
9
10
|
|
|
10
|
-
export {
|
|
11
|
+
export { OverloadYield } from "./core/async-generator.js";
|
|
12
|
+
export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
|