async-browser-context 0.2.3 → 0.3.0
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 +102 -0
- package/README.md +45 -30
- package/dist/babel/async-generator-helpers.d.ts +6 -6
- package/dist/babel/async-generator-helpers.d.ts.map +1 -1
- package/dist/babel/async-generator-helpers.js +33 -22
- package/dist/babel/async-generator-helpers.js.map +1 -1
- package/dist/babel/async-generator-resources.d.ts +14 -0
- package/dist/babel/async-generator-resources.d.ts.map +1 -0
- package/dist/babel/async-generator-resources.js +38 -0
- package/dist/babel/async-generator-resources.js.map +1 -0
- package/dist/babel/bind-generators.d.ts +1 -1
- package/dist/babel/bind-generators.d.ts.map +1 -1
- package/dist/babel/bind-generators.js +88 -44
- package/dist/babel/bind-generators.js.map +1 -1
- package/dist/babel/name-coroutines.d.ts +14 -8
- package/dist/babel/name-coroutines.d.ts.map +1 -1
- package/dist/babel/name-coroutines.js +160 -39
- package/dist/babel/name-coroutines.js.map +1 -1
- package/dist/babel/preset.d.ts +24 -13
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +37 -9
- package/dist/babel/preset.js.map +1 -1
- package/dist/babel/runtime-calls.d.ts +22 -0
- package/dist/babel/runtime-calls.d.ts.map +1 -0
- package/dist/babel/runtime-calls.js +69 -0
- package/dist/babel/runtime-calls.js.map +1 -0
- package/dist/babel/skip-runtime.d.ts +18 -11
- package/dist/babel/skip-runtime.d.ts.map +1 -1
- package/dist/babel/skip-runtime.js +52 -16
- package/dist/babel/skip-runtime.js.map +1 -1
- package/dist/babel/static-members.d.ts +4 -0
- package/dist/babel/static-members.d.ts.map +1 -0
- package/dist/babel/static-members.js +112 -0
- package/dist/babel/static-members.js.map +1 -0
- package/dist/core/async-generator.d.ts +51 -13
- package/dist/core/async-generator.d.ts.map +1 -1
- package/dist/core/async-generator.js +255 -58
- package/dist/core/async-generator.js.map +1 -1
- package/dist/core/async-local-storage.d.ts +3 -0
- package/dist/core/async-local-storage.d.ts.map +1 -1
- package/dist/core/async-local-storage.js +3 -0
- package/dist/core/async-local-storage.js.map +1 -1
- package/dist/core/callback-hook.d.ts +3 -3
- package/dist/core/coroutine.d.ts +6 -2
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +106 -44
- package/dist/core/coroutine.js.map +1 -1
- package/dist/core/function-name.d.ts +9 -0
- package/dist/core/function-name.d.ts.map +1 -0
- package/dist/core/function-name.js +12 -0
- package/dist/core/function-name.js.map +1 -0
- package/dist/core/hooks.d.ts +2 -1
- package/dist/core/hooks.d.ts.map +1 -1
- package/dist/core/hooks.js +21 -6
- package/dist/core/hooks.js.map +1 -1
- package/dist/core/snapshot.d.ts +2 -1
- package/dist/core/snapshot.d.ts.map +1 -1
- package/dist/core/snapshot.js +7 -3
- package/dist/core/snapshot.js.map +1 -1
- package/dist/core/store.d.ts +28 -0
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +90 -20
- package/dist/core/store.js.map +1 -1
- package/dist/core/variable.d.ts +2 -1
- package/dist/core/variable.d.ts.map +1 -1
- package/dist/core/variable.js +5 -2
- package/dist/core/variable.js.map +1 -1
- package/dist/node/index.d.ts +10 -4
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +51 -16
- package/dist/node/index.js.map +1 -1
- package/dist/node/runtime.d.ts +7 -6
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +29 -16
- package/dist/node/runtime.js.map +1 -1
- package/dist/opentelemetry/context-manager.d.ts.map +1 -1
- package/dist/opentelemetry/context-manager.js +172 -13
- package/dist/opentelemetry/context-manager.js.map +1 -1
- package/dist/patches/callbacks.d.ts.map +1 -1
- package/dist/patches/callbacks.js +84 -29
- package/dist/patches/callbacks.js.map +1 -1
- package/dist/patches/events.d.ts.map +1 -1
- package/dist/patches/events.js +203 -91
- package/dist/patches/events.js.map +1 -1
- package/dist/patches/handler-interfaces.d.ts +3 -2
- package/dist/patches/handler-interfaces.d.ts.map +1 -1
- package/dist/patches/handler-interfaces.js +3 -2
- package/dist/patches/handler-interfaces.js.map +1 -1
- package/dist/patches/promise.d.ts +3 -0
- package/dist/patches/promise.d.ts.map +1 -1
- package/dist/patches/promise.js +13 -5
- package/dist/patches/promise.js.map +1 -1
- package/dist/patches/streams.d.ts.map +1 -1
- package/dist/patches/streams.js +36 -16
- package/dist/patches/streams.js.map +1 -1
- package/dist/patches/timers.d.ts.map +1 -1
- package/dist/patches/timers.js +17 -5
- package/dist/patches/timers.js.map +1 -1
- package/dist/patches/to-string.d.ts +4 -0
- package/dist/patches/to-string.d.ts.map +1 -1
- package/dist/patches/to-string.js +12 -6
- package/dist/patches/to-string.js.map +1 -1
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +3 -3
- package/dist/runtime.js.map +1 -1
- package/dist/vite/plugin.d.ts +7 -2
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/vite/plugin.js +30 -6
- package/dist/vite/plugin.js.map +1 -1
- package/package.json +49 -25
- package/src/babel/async-generator-helpers.ts +34 -23
- package/src/babel/async-generator-resources.ts +40 -0
- package/src/babel/bind-generators.ts +91 -47
- package/src/babel/name-coroutines.ts +170 -41
- package/src/babel/preset.ts +47 -15
- package/src/babel/runtime-calls.ts +73 -0
- package/src/babel/skip-runtime.ts +58 -18
- package/src/babel/static-members.ts +117 -0
- package/src/core/async-generator.ts +273 -62
- package/src/core/async-local-storage.ts +3 -0
- package/src/core/callback-hook.ts +3 -3
- package/src/core/coroutine.ts +104 -43
- package/src/core/function-name.ts +11 -0
- package/src/core/hooks.ts +23 -6
- package/src/core/snapshot.ts +8 -3
- package/src/core/store.ts +107 -21
- package/src/core/variable.ts +6 -2
- package/src/node/index.ts +62 -19
- package/src/node/runtime.ts +33 -15
- package/src/opentelemetry/context-manager.ts +188 -13
- package/src/patches/callbacks.ts +87 -30
- package/src/patches/events.ts +249 -103
- package/src/patches/handler-interfaces.ts +3 -2
- package/src/patches/promise.ts +17 -13
- package/src/patches/streams.ts +36 -16
- package/src/patches/timers.ts +17 -5
- package/src/patches/to-string.ts +12 -6
- package/src/runtime.ts +3 -3
- package/src/vite/plugin.ts +38 -8
|
@@ -2,38 +2,155 @@
|
|
|
2
2
|
* The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
|
|
3
3
|
* changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
|
|
4
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
|
|
6
|
-
*
|
|
7
|
-
* continues each step with a native `await`.
|
|
5
|
+
* Firefox. The preset replaces the helper with `asyncGenerator` of the runtime. It also takes
|
|
6
|
+
* the helpers `OverloadYield`, `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this
|
|
7
|
+
* module. The driver in this module then continues each step with a native `await`.
|
|
8
8
|
*
|
|
9
9
|
* The protocol is the protocol of the Babel 8 helper: the body gives
|
|
10
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`.
|
|
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. The delegate of this
|
|
14
|
+
* module also gives kind 2 for a step of `yield*` after `return()`.
|
|
12
15
|
*/
|
|
13
16
|
|
|
17
|
+
/** The Promise of the time when the module loads, for the rejections of the methods. */
|
|
18
|
+
const NativePromise : PromiseConstructor = Promise;
|
|
19
|
+
|
|
14
20
|
/**
|
|
15
|
-
* The value that the body gives for an `await` (kind 0)
|
|
16
|
-
*
|
|
21
|
+
* The value that the body gives for an `await` (kind 0), for a step of `yield*` (kind 1), or
|
|
22
|
+
* for a step of `yield*` after `return()` (kind 2). The helpers `awaitAsyncGenerator` and
|
|
23
|
+
* `asyncGeneratorDelegate` make it with `new`.
|
|
17
24
|
*/
|
|
18
25
|
export class OverloadYield {
|
|
19
|
-
v : unknown;
|
|
20
|
-
readonly k : 0 | 1;
|
|
21
|
-
constructor(value : unknown, kind : 0 | 1) {
|
|
26
|
+
readonly v : unknown;
|
|
27
|
+
readonly k : 0 | 1 | 2;
|
|
28
|
+
constructor(value : unknown, kind : 0 | 1 | 2) {
|
|
22
29
|
this.v = value;
|
|
23
30
|
this.k = kind;
|
|
24
31
|
}
|
|
25
32
|
}
|
|
26
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The error of a step of `yield*` that was rejected. The driver gives it back to the delegate,
|
|
36
|
+
* and the delegate throws the error. Thus the error goes into the body at the `yield*`, also
|
|
37
|
+
* when the delegate has no `throw` method.
|
|
38
|
+
*/
|
|
39
|
+
class DelegateRejection {
|
|
40
|
+
readonly error : unknown;
|
|
41
|
+
constructor(error : unknown) {
|
|
42
|
+
this.error = error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The helper `awaitAsyncGenerator` of Babel: the body gives its value for an `await`. The preset
|
|
48
|
+
* gives this function in place of the helper of Babel. Thus the marker is always the class of this
|
|
49
|
+
* module, also when `@babel/plugin-transform-runtime` gives the other helpers.
|
|
50
|
+
*/
|
|
51
|
+
export function awaitAsyncGenerator(value : unknown) : OverloadYield {
|
|
52
|
+
return new OverloadYield(value, 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The helper `asyncGeneratorDelegate` of Babel 8: a sync iterator for `yield*` in an async
|
|
57
|
+
* generator. Each step of the inner async iterator becomes `new OverloadYield(promise, 1)`, or
|
|
58
|
+
* kind 2 after `return()`. The driver awaits the promise, and gives a copy of its result back to
|
|
59
|
+
* the iterator. After kind 2, the driver gives it back with `return()`. Thus a complete inner
|
|
60
|
+
* iterator ends the body, as with a native `yield*`.
|
|
61
|
+
*/
|
|
62
|
+
export function asyncGeneratorDelegate(inner : AsyncIterator<unknown, unknown, unknown>) : Iterator<unknown, unknown, unknown> {
|
|
63
|
+
const iterator = inner as unknown as Record<ResumeKey, ((value : unknown) => unknown) | undefined>;
|
|
64
|
+
let waiting = false;
|
|
65
|
+
const pump = (key : ResumeKey, value : unknown) : IteratorResult<unknown, unknown> => {
|
|
66
|
+
waiting = true;
|
|
67
|
+
const promise = new NativePromise((resolve) => {
|
|
68
|
+
resolve(Reflect.apply(iterator[key] as (value : unknown) => unknown, inner, [value]));
|
|
69
|
+
});
|
|
70
|
+
return { done : false, value : new OverloadYield(promise, key === "return" ? 2 : 1) };
|
|
71
|
+
};
|
|
72
|
+
const delegate : Record<PropertyKey, unknown> = {
|
|
73
|
+
[Symbol.iterator]() { return this; },
|
|
74
|
+
next(value : unknown) : unknown {
|
|
75
|
+
if (!waiting) {
|
|
76
|
+
return pump("next", value);
|
|
77
|
+
}
|
|
78
|
+
waiting = false;
|
|
79
|
+
if (value instanceof DelegateRejection) {
|
|
80
|
+
throw value.error;
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
delegate["throw"] = (value : unknown) : unknown => {
|
|
86
|
+
if (typeof iterator.throw === "function") {
|
|
87
|
+
return pump("throw", value);
|
|
88
|
+
}
|
|
89
|
+
// Without a throw method, a native yield* closes the inner iterator and awaits its return()
|
|
90
|
+
// (AsyncIteratorClose). An error of return() goes into the body, else a TypeError.
|
|
91
|
+
waiting = true;
|
|
92
|
+
const closed = new NativePromise<never>((resolve) => {
|
|
93
|
+
resolve(typeof iterator.return === "function" ? Reflect.apply(iterator.return, inner, []) : undefined);
|
|
94
|
+
}).then((result : unknown) : never => {
|
|
95
|
+
if (typeof iterator.return === "function" && !isObject(result)) {
|
|
96
|
+
throw new TypeError("The result of return() of the iterator of yield* is not an object");
|
|
97
|
+
}
|
|
98
|
+
throw new TypeError("The iterator of yield* has no throw method");
|
|
99
|
+
});
|
|
100
|
+
return { done : false, value : new OverloadYield(closed, 1) };
|
|
101
|
+
};
|
|
102
|
+
if (typeof iterator.return === "function") {
|
|
103
|
+
delegate["return"] = (value : unknown) : unknown => {
|
|
104
|
+
if (waiting) {
|
|
105
|
+
waiting = false;
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
return pump("return", value);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return delegate as unknown as Iterator<unknown, unknown, unknown>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
|
|
116
|
+
* awaited object one time, in the context of the body. When the object has no `then` method,
|
|
117
|
+
* the driver awaits the box. Thus the engine does not read `then` a second time, and the
|
|
118
|
+
* `await` takes the same microtask turns.
|
|
119
|
+
*/
|
|
120
|
+
export class PlainValue {
|
|
121
|
+
readonly value : unknown;
|
|
122
|
+
constructor(value : unknown) {
|
|
123
|
+
this.value = value;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** This function gives the value in a box, or the value itself. */
|
|
128
|
+
export function unbox(value : unknown) : unknown {
|
|
129
|
+
return value instanceof PlainValue ? value.value : value;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** This function gives `true` for a value that can have properties: an object or a function. */
|
|
133
|
+
export function isObject(value : unknown) : value is object {
|
|
134
|
+
return (typeof value === "object" && value !== null) || typeof value === "function";
|
|
135
|
+
}
|
|
136
|
+
|
|
27
137
|
/** The methods of a generator that a request of the consumer starts. */
|
|
28
138
|
export type ResumeKey = "next" | "throw" | "return";
|
|
29
139
|
|
|
30
140
|
/**
|
|
31
141
|
* This function does one step of the body: `generator[key](argument)`. The browser runtime sets
|
|
32
142
|
* the context of the generator around the step. `resumed` is `true` for a step after an
|
|
33
|
-
* `await` of the driver.
|
|
143
|
+
* `await` of the driver. The function gives the result of the generator without a change.
|
|
34
144
|
*/
|
|
35
145
|
export type AsyncGeneratorStep = (key : ResumeKey, argument : unknown, resumed : boolean) => IteratorResult<unknown, unknown>;
|
|
36
146
|
|
|
147
|
+
/**
|
|
148
|
+
* This function gives the value that the driver awaits in place of `value`. The browser runtime
|
|
149
|
+
* reads the `then` property in the context of the generator. The Node.js runtime gives the
|
|
150
|
+
* value without a change.
|
|
151
|
+
*/
|
|
152
|
+
export type Prepare = (value : unknown) => unknown;
|
|
153
|
+
|
|
37
154
|
interface Request {
|
|
38
155
|
readonly key : ResumeKey;
|
|
39
156
|
readonly argument : unknown;
|
|
@@ -47,92 +164,186 @@ type Invoke = (key : ResumeKey, argument : unknown) => Promise<IteratorResult<un
|
|
|
47
164
|
/** The key of the request function of each async generator object. */
|
|
48
165
|
const INVOKE : unique symbol = Symbol("async-browser-context/invoke");
|
|
49
166
|
|
|
50
|
-
function
|
|
51
|
-
|
|
167
|
+
/** 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. */
|
|
168
|
+
function request(target : unknown, key : ResumeKey, argument : unknown) : Promise<IteratorResult<unknown, unknown>> {
|
|
169
|
+
const invoke = typeof target === "object" && target !== null ? (target as { [INVOKE]? : Invoke })[INVOKE] : undefined;
|
|
52
170
|
if (invoke === undefined) {
|
|
53
|
-
|
|
171
|
+
return NativePromise.reject(new TypeError(`AsyncGenerator.prototype.${key} started on an object that is not an async generator`));
|
|
172
|
+
}
|
|
173
|
+
return invoke(key, argument);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* This function gives %AsyncIteratorPrototype% of the engine from an async generator function.
|
|
178
|
+
* The prototype gives `Symbol.asyncIterator`, `Symbol.asyncDispose` and the async iterator
|
|
179
|
+
* helpers. If a compiler changed the async generator function, the function gives `undefined`.
|
|
180
|
+
*
|
|
181
|
+
* @internal
|
|
182
|
+
*/
|
|
183
|
+
export function asyncIteratorPrototypeOf(generatorFunction : Function) : object | undefined {
|
|
184
|
+
let prototype : Record<symbol, unknown> | null;
|
|
185
|
+
try {
|
|
186
|
+
prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype) as object) as Record<symbol, unknown> | null;
|
|
187
|
+
} catch {
|
|
188
|
+
// A function without a prototype object, for example an arrow function
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* This function makes the prototype of the async generator objects of transformed code. Its
|
|
196
|
+
* methods are not enumerable, as native methods. Without `parent`, the prototype inherits from
|
|
197
|
+
* `Object.prototype` and gets its own `Symbol.asyncIterator` method.
|
|
198
|
+
*
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
export function makeAsyncGeneratorPrototype(parent : object | undefined) : object {
|
|
202
|
+
const prototype = Object.create(parent ?? Object.prototype) as object;
|
|
203
|
+
const methods : Record<ResumeKey, (this : unknown, argument? : unknown) => Promise<IteratorResult<unknown, unknown>>> = {
|
|
204
|
+
next(value) { return request(this, "next", value); },
|
|
205
|
+
throw(error) { return request(this, "throw", error); },
|
|
206
|
+
return(value) { return request(this, "return", value); },
|
|
207
|
+
};
|
|
208
|
+
for (const key of ["next", "throw", "return"] as const) {
|
|
209
|
+
Object.defineProperty(prototype, key, { value : methods[key], writable : true, configurable : true });
|
|
210
|
+
}
|
|
211
|
+
if (parent === undefined) {
|
|
212
|
+
Object.defineProperty(prototype, Symbol.asyncIterator, { value : function asyncIterator(this : unknown) { return this; }, writable : true, configurable : true });
|
|
213
|
+
}
|
|
214
|
+
Object.defineProperty(prototype, Symbol.toStringTag, { value : "AsyncGenerator", configurable : true });
|
|
215
|
+
return prototype;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* This function gives %AsyncIteratorPrototype% of the engine, or `undefined`. Another preset in
|
|
220
|
+
* the same Babel configuration, for example `next/babel` with its preset-env, can change the
|
|
221
|
+
* async generator below in the runtime itself. Its helper is then a module that loads at this
|
|
222
|
+
* time, and the call can throw. The catch keeps the runtime available, with a prototype
|
|
223
|
+
* without the parent of the engine.
|
|
224
|
+
*/
|
|
225
|
+
function engineAsyncIteratorPrototype() : object | undefined {
|
|
226
|
+
try {
|
|
227
|
+
return asyncIteratorPrototypeOf(async function* () {
|
|
228
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
229
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
230
|
+
"async-browser-context runtime";
|
|
231
|
+
});
|
|
232
|
+
} catch {
|
|
233
|
+
return undefined;
|
|
54
234
|
}
|
|
55
|
-
return invoke;
|
|
56
235
|
}
|
|
57
236
|
|
|
58
237
|
/** The prototype of the async generator objects of transformed code. */
|
|
59
|
-
const AsyncGeneratorPrototype =
|
|
60
|
-
next(this : object, value? : unknown) : Promise<IteratorResult<unknown, unknown>> {
|
|
61
|
-
return invokeOf(this)("next", value);
|
|
62
|
-
},
|
|
63
|
-
throw(this : object, error? : unknown) : Promise<IteratorResult<unknown, unknown>> {
|
|
64
|
-
return invokeOf(this)("throw", error);
|
|
65
|
-
},
|
|
66
|
-
return(this : object, value? : unknown) : Promise<IteratorResult<unknown, unknown>> {
|
|
67
|
-
return invokeOf(this)("return", value);
|
|
68
|
-
},
|
|
69
|
-
[Symbol.asyncIterator]<T>(this : T) : T {
|
|
70
|
-
return this;
|
|
71
|
-
},
|
|
72
|
-
[Symbol.toStringTag] : "AsyncGenerator",
|
|
73
|
-
};
|
|
238
|
+
const AsyncGeneratorPrototype : object = makeAsyncGeneratorPrototype(engineAsyncIteratorPrototype());
|
|
74
239
|
|
|
75
240
|
/**
|
|
76
241
|
* This function makes an async generator object. The object queues the requests of the
|
|
77
|
-
* consumer, and starts one request at a time, as
|
|
78
|
-
* driver: the Node.js runtime starts it in the context of the generator.
|
|
242
|
+
* consumer, and starts one request at a time, as a native async generator does. `enter`
|
|
243
|
+
* starts the driver: the Node.js runtime starts it in the context of the generator.
|
|
79
244
|
*/
|
|
80
245
|
export function createAsyncGenerator(
|
|
81
246
|
step : AsyncGeneratorStep,
|
|
82
|
-
|
|
247
|
+
PromiseConstructor : PromiseConstructor,
|
|
83
248
|
enter : (start : () => Promise<void>) => Promise<void> = (start) => start(),
|
|
249
|
+
prepare : Prepare = (value) => value,
|
|
84
250
|
) : AsyncGenerator<unknown, unknown, unknown> {
|
|
85
251
|
let first : Request | undefined;
|
|
86
252
|
let last : Request | undefined;
|
|
253
|
+
/** `true` after the first step of the body. While a step operates, the requests wait in the queue. */
|
|
254
|
+
let started = false;
|
|
255
|
+
/** `true` when the body is complete. */
|
|
256
|
+
let complete = false;
|
|
87
257
|
|
|
88
258
|
/** This function starts the requests in the queue, one after the other, until the queue is empty. */
|
|
89
259
|
const run = async (key : ResumeKey, argument : unknown) : Promise<void> => {
|
|
90
260
|
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
261
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
91
262
|
"async-browser-context runtime";
|
|
92
263
|
let resumed = false;
|
|
93
264
|
for (;;) {
|
|
94
265
|
let failed = false;
|
|
95
266
|
let settled : unknown;
|
|
96
|
-
let done = false;
|
|
97
267
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
268
|
+
if (complete || (!started && key !== "next")) {
|
|
269
|
+
// The body does not operate: it is complete, or return() or throw() came before the start
|
|
270
|
+
complete = true;
|
|
271
|
+
if (key === "throw") {
|
|
272
|
+
throw argument;
|
|
273
|
+
}
|
|
274
|
+
if (key === "return") {
|
|
275
|
+
// As AsyncGeneratorAwaitReturn, return() awaits its value
|
|
276
|
+
settled = await argument;
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
started = true;
|
|
280
|
+
if (key === "return") {
|
|
281
|
+
// At a yield, return() awaits its value first. A rejection goes into the body at the yield.
|
|
282
|
+
try {
|
|
283
|
+
argument = unbox(await prepare(argument));
|
|
284
|
+
} catch (error) {
|
|
285
|
+
key = "throw";
|
|
286
|
+
argument = error;
|
|
287
|
+
}
|
|
106
288
|
resumed = true;
|
|
107
|
-
key = "throw";
|
|
108
|
-
result = step("throw", error, true);
|
|
109
|
-
continue;
|
|
110
289
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
290
|
+
let result = step(key, argument, resumed);
|
|
291
|
+
// return() at a yield already awaited its value. If the body completes with that value, a native generator does not await it again.
|
|
292
|
+
if (key === "return" && result.done === true && result.value === argument) {
|
|
293
|
+
settled = argument;
|
|
294
|
+
complete = true;
|
|
295
|
+
result = { done : true, value : undefined };
|
|
296
|
+
}
|
|
297
|
+
for (;;) {
|
|
298
|
+
if (complete) {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
const yielded : unknown = result.value;
|
|
302
|
+
const overloaded = yielded instanceof OverloadYield;
|
|
303
|
+
let value : unknown;
|
|
304
|
+
try {
|
|
305
|
+
value = unbox(await prepare(overloaded ? yielded.v : yielded));
|
|
306
|
+
} catch (error) {
|
|
307
|
+
// The error goes into the body at the await or the yield. The delegate of a yield* throws it.
|
|
308
|
+
result = overloaded && yielded.k !== 0 ? step("next", new DelegateRejection(error), true) : step("throw", error, true);
|
|
117
309
|
continue;
|
|
118
310
|
}
|
|
119
|
-
|
|
311
|
+
resumed = true;
|
|
312
|
+
if (overloaded) {
|
|
313
|
+
const resumeKey : ResumeKey = yielded.k === 2 ? "return" : "next";
|
|
314
|
+
if (yielded.k === 0 || !isObject(value)) {
|
|
315
|
+
// The value of an await. For a result of yield* that is not an object, the body throws a TypeError.
|
|
316
|
+
result = step(resumeKey, value, true);
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
// A step of yield*. As a native yield*, the driver reads done and value one time, and the body gets a copy.
|
|
320
|
+
// The driver does not compare the result of the body with the copy, because the yield* of WebKit makes a new object.
|
|
321
|
+
const done = Boolean((value as { done? : unknown }).done);
|
|
322
|
+
const item = (value as { value? : unknown }).value;
|
|
323
|
+
result = step(resumeKey, { value : item, done }, true);
|
|
324
|
+
if (done) {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
// The body gives the result back. Its value is not awaited, as with a native yield*.
|
|
328
|
+
value = item;
|
|
329
|
+
}
|
|
330
|
+
settled = value;
|
|
331
|
+
complete = result.done === true;
|
|
332
|
+
break;
|
|
120
333
|
}
|
|
121
|
-
settled = value;
|
|
122
|
-
done = result.done === true;
|
|
123
|
-
break;
|
|
124
334
|
}
|
|
125
335
|
} catch (error) {
|
|
336
|
+
complete = true;
|
|
126
337
|
failed = true;
|
|
127
338
|
settled = error;
|
|
128
339
|
}
|
|
129
340
|
// A request always waits in the queue while the driver runs
|
|
130
|
-
const
|
|
131
|
-
first =
|
|
341
|
+
const current = first as Request;
|
|
342
|
+
first = current.next;
|
|
132
343
|
if (failed) {
|
|
133
|
-
|
|
344
|
+
current.reject(settled);
|
|
134
345
|
} else {
|
|
135
|
-
|
|
346
|
+
current.resolve({ value : settled, done : complete });
|
|
136
347
|
}
|
|
137
348
|
if (first === undefined) {
|
|
138
349
|
last = undefined;
|
|
@@ -143,13 +354,13 @@ export function createAsyncGenerator(
|
|
|
143
354
|
}
|
|
144
355
|
};
|
|
145
356
|
|
|
146
|
-
const invoke : Invoke = (key, argument) => new
|
|
147
|
-
const
|
|
357
|
+
const invoke : Invoke = (key, argument) => new PromiseConstructor((resolve, reject) => {
|
|
358
|
+
const added : Request = { key, argument, resolve, reject, next : undefined };
|
|
148
359
|
if (last === undefined) {
|
|
149
|
-
first = last =
|
|
360
|
+
first = last = added;
|
|
150
361
|
void enter(() => run(key, argument));
|
|
151
362
|
} else {
|
|
152
|
-
last = last.next =
|
|
363
|
+
last = last.next = added;
|
|
153
364
|
}
|
|
154
365
|
});
|
|
155
366
|
|
|
@@ -63,6 +63,9 @@ export class AsyncLocalStorage<T> {
|
|
|
63
63
|
* This method sets `store` for the rest of the current synchronous step, and for the
|
|
64
64
|
* code that this step starts later. Prefer `run()`: `enterWith()` can also
|
|
65
65
|
* change the store of the code that called the current function.
|
|
66
|
+
*
|
|
67
|
+
* Outside all steps, for example at the top level of a module, the store stays after the
|
|
68
|
+
* task as a default store, as on Node.js. No code sets the root context again.
|
|
66
69
|
*/
|
|
67
70
|
enterWith(store : T) : void {
|
|
68
71
|
this.#enabled = true;
|
|
@@ -8,9 +8,9 @@ export interface CallbackHook {
|
|
|
8
8
|
/**
|
|
9
9
|
* The library starts this method before the callback.
|
|
10
10
|
*
|
|
11
|
-
* @param callback - The function that the library starts. For
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* @param callback - The function that the library starts. For a step
|
|
12
|
+
* after an `await`, it is the body of the async function or the async
|
|
13
|
+
* generator. It has the name of that function.
|
|
14
14
|
* @param source - The API that got the callback, for example
|
|
15
15
|
* `"setTimeout"`, `"Promise.then"`, `"addEventListener:click"`,
|
|
16
16
|
* `"onclick"` or `"await"`.
|