async-browser-context 0.1.1 → 0.2.1
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 +25 -0
- package/README.md +45 -3
- package/dist/babel/name-coroutines.d.ts +25 -0
- package/dist/babel/name-coroutines.d.ts.map +1 -0
- package/dist/babel/name-coroutines.js +116 -0
- package/dist/babel/name-coroutines.js.map +1 -0
- package/dist/babel/preset.d.ts +12 -2
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +8 -2
- package/dist/babel/preset.js.map +1 -1
- package/dist/core/callback-hook.d.ts +24 -0
- package/dist/core/callback-hook.d.ts.map +1 -0
- package/dist/core/callback-hook.js +13 -0
- package/dist/core/callback-hook.js.map +1 -0
- package/dist/core/coroutine.d.ts +2 -12
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +23 -8
- package/dist/core/coroutine.js.map +1 -1
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +28 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/store.d.ts +28 -3
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +81 -7
- package/dist/core/store.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +10 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +14 -0
- package/dist/node/index.js.map +1 -1
- package/dist/patches/callbacks.d.ts.map +1 -1
- package/dist/patches/callbacks.js +10 -8
- package/dist/patches/callbacks.js.map +1 -1
- package/dist/patches/events.d.ts.map +1 -1
- package/dist/patches/events.js +39 -22
- package/dist/patches/events.js.map +1 -1
- package/dist/patches/handler-interfaces.d.ts +16 -0
- package/dist/patches/handler-interfaces.d.ts.map +1 -0
- package/dist/patches/handler-interfaces.js +42 -0
- package/dist/patches/handler-interfaces.js.map +1 -0
- package/dist/patches/observers.js +1 -1
- package/dist/patches/observers.js.map +1 -1
- package/dist/patches/patch.d.ts +5 -2
- package/dist/patches/patch.d.ts.map +1 -1
- package/dist/patches/patch.js +6 -3
- package/dist/patches/patch.js.map +1 -1
- package/dist/patches/promise.js +1 -1
- package/dist/patches/promise.js.map +1 -1
- package/dist/patches/streams.d.ts.map +1 -1
- package/dist/patches/streams.js +12 -7
- package/dist/patches/streams.js.map +1 -1
- package/dist/patches/timers.d.ts.map +1 -1
- package/dist/patches/timers.js +6 -5
- package/dist/patches/timers.js.map +1 -1
- package/dist/vite/plugin.d.ts +5 -0
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/vite/plugin.js +1 -1
- package/dist/vite/plugin.js.map +1 -1
- package/package.json +8 -3
- package/src/babel/name-coroutines.ts +128 -0
- package/src/babel/preset.ts +16 -3
- package/src/core/callback-hook.ts +34 -0
- package/src/core/coroutine.ts +27 -8
- package/src/core/hooks.ts +30 -0
- package/src/core/store.ts +108 -7
- package/src/index.ts +1 -0
- package/src/node/index.ts +16 -0
- package/src/patches/callbacks.ts +10 -8
- package/src/patches/events.ts +38 -22
- package/src/patches/handler-interfaces.ts +41 -0
- package/src/patches/observers.ts +1 -1
- package/src/patches/patch.ts +6 -3
- package/src/patches/promise.ts +2 -2
- package/src/patches/streams.ts +12 -7
- package/src/patches/timers.ts +6 -5
- package/src/vite/plugin.ts +6 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# Change history
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### New functions
|
|
6
|
+
|
|
7
|
+
- The option `names` of the Babel preset and of the Vite plugin. With `names: false`, the preset does not give the names of the async functions to `coroutine`. The code is then smaller by 0.1 to 1.6 percent.
|
|
8
|
+
|
|
9
|
+
### Documentation and tests
|
|
10
|
+
|
|
11
|
+
- The guide "Build tools" gives the setup for webpack, Rspack, Rollup, esbuild, Next.js and SWC. The test `pnpm test:bundlers` builds a page with each setup and examines it in Chromium.
|
|
12
|
+
- SWC and esbuild with the target ES2016 keep all rules except rule C5. Two new test projects start all rule tests with these compilers in place of the preset.
|
|
13
|
+
- The command `pnpm bench:size` measures the size of the runtime and the size that the transform adds to real npm packages. The runtime is 5.9 kB with gzip. The transform adds 2.9 percent to an example application.
|
|
14
|
+
|
|
15
|
+
## 0.2.0
|
|
16
|
+
|
|
17
|
+
### New functions
|
|
18
|
+
|
|
19
|
+
- `addCallbackHook(hook)` adds a hook. The library starts `hook.before` and `hook.after` around each callback of a patched API and each step of an async function after an `await`. The hook methods get the callback and its source, for example `"setTimeout"` or `"addEventListener:click"`, and operate in the context of the callback. Thus, a tool can find the operation and the function of a long task in all browsers. On Node.js, the function does nothing.
|
|
20
|
+
- The Babel preset gives the name of each async function to `coroutine`. The callback hooks show this name for the steps after an `await`.
|
|
21
|
+
|
|
22
|
+
### Changes
|
|
23
|
+
|
|
24
|
+
- Each wrapper of the library has the name `asyncBrowserContextWrapper`. Before, the wrappers had the name `boundToFrame` or no name. Stack traces, profilers and the Long Animation Frames API show this name between the browser and the callback.
|
|
25
|
+
- The event patch reads only the 138 interfaces that have `on...` properties. Before, it read all globals of the page, approximately 1,000. For each global that code reads for the first time, the browser makes an interface object. Thus, the patches take less time when the page loads. The median changed from 18 to 8 ms in Chromium, from 22 to 8 ms in Firefox and from 16 to 7 ms in WebKit. A browser test makes sure that the list contains each interface with `on...` properties.
|
|
26
|
+
- The documents tell one more case of rule C13. When code in the root context dispatches an event, the listener gets the context of its registration.
|
|
27
|
+
|
|
3
28
|
## 0.1.1
|
|
4
29
|
|
|
5
30
|
This version changes only the documentation. The code is the same as in 0.1.0.
|
package/README.md
CHANGED
|
@@ -81,6 +81,7 @@ The plugin transforms the modules of the application and of its dependencies, al
|
|
|
81
81
|
| `include` | All JavaScript and TypeScript modules | A regular expression or a function that selects the modules to transform. |
|
|
82
82
|
| `exclude` | None | A regular expression or a function that selects the modules not to transform. |
|
|
83
83
|
| `runtime` | `async-browser-context/runtime` | The module that the transformed code imports. |
|
|
84
|
+
| `names` | `true` | Set `false` to remove the names of the async functions for the callback hooks. The code is then smaller by 0.1 to 1.6 percent. |
|
|
84
85
|
| `ssr` | `false` | Set `true` to also transform the modules of server-side rendering, for example for Vitest in Node.js. |
|
|
85
86
|
|
|
86
87
|
### Babel
|
|
@@ -95,7 +96,21 @@ The plugin transforms the modules of the application and of its dependencies, al
|
|
|
95
96
|
|
|
96
97
|
2. Make sure that Babel transforms the dependencies too. With webpack, do not exclude `node_modules` from `babel-loader`.
|
|
97
98
|
|
|
98
|
-
The preset operates with Babel 7.22 and later, and with Babel 8.
|
|
99
|
+
The preset operates with Babel 7.22 and later, and with Babel 8. CI examines Babel 7.22, the newest Babel 7 and the newest Babel 8 with the packed package (`pnpm test:babel`).
|
|
100
|
+
|
|
101
|
+
### Other build tools
|
|
102
|
+
|
|
103
|
+
A test in CI (`pnpm test:bundlers`) builds a page with each of these setups and examines it in Chromium. The [build tools guide](https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/build-tools) gives each configuration.
|
|
104
|
+
|
|
105
|
+
| Tool | Setup | Rules |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| webpack 5, Rspack 2 | `babel-loader` with the preset. Do not exclude `node_modules`. | All |
|
|
108
|
+
| Rollup 4 | `@rollup/plugin-babel` with the preset | All |
|
|
109
|
+
| esbuild | A small plugin that applies the preset | All |
|
|
110
|
+
| Next.js 16, Turbopack or webpack | `babel.config.js` with `next/babel` and the preset, and `transpilePackages` for the dependencies | All |
|
|
111
|
+
| SWC, esbuild, Next.js without Babel | The target ES2016: the compiler changes async functions into generators | All except rule C5 |
|
|
112
|
+
|
|
113
|
+
With the target ES2016, the patch of `then` keeps the context through `await`. A generator body then gets the context of each `next()` call, and the callback hooks get no names.
|
|
99
114
|
|
|
100
115
|
### Node.js
|
|
101
116
|
|
|
@@ -133,6 +148,24 @@ snapshot.run(() => userId.get()); // "u-1"
|
|
|
133
148
|
|
|
134
149
|
The package also exports `Variable`, `Snapshot`, and the other names `AsyncVariable` and `AsyncSnapshot`.
|
|
135
150
|
|
|
151
|
+
**`addCallbackHook`** adds a hook. The library starts its methods before and after each callback that it starts, in the context of that callback. Use it to measure callbacks, and to find the operation of a long task, also in Firefox and Safari:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { addCallbackHook, AsyncLocalStorage } from "async-browser-context";
|
|
155
|
+
|
|
156
|
+
const operation = new AsyncLocalStorage<string>(); // Start each operation in operation.run()
|
|
157
|
+
const starts : number[] = [];
|
|
158
|
+
addCallbackHook({
|
|
159
|
+
before() { starts.push(performance.now()); },
|
|
160
|
+
after(callback, source) {
|
|
161
|
+
const duration = performance.now() - (starts.pop() ?? 0);
|
|
162
|
+
if (duration >= 50) report(operation.getStore(), callback.name, source, duration);
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`source` names the API, for example `"setTimeout"`, `"addEventListener:click"` or `"await"`. For a step after an `await`, `callback.name` is the name of the async function. The guide [Find the operation of a long task](https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/long-tasks) tells more.
|
|
168
|
+
|
|
136
169
|
## Entry points
|
|
137
170
|
|
|
138
171
|
| Import | Function |
|
|
@@ -177,7 +210,7 @@ The tests examine each rule on the browser runtime, on the Node.js runtime and i
|
|
|
177
210
|
| C10 | Two copies of the library on one page use one context store. |
|
|
178
211
|
| C11 | The library does not replace the `Promise` constructor. `Promise` subclasses operate as without the library. |
|
|
179
212
|
| C12 | The library does not keep promises or values in memory after the program has no reference to them. |
|
|
180
|
-
| C13 | An event listener gets the context of the code that dispatches the event.
|
|
213
|
+
| C13 | An event listener gets the context of the code that dispatches the event. If that code has the root context, the listener gets the registration context. This is the context of the `addEventListener()` call or of the assignment to the `on...` property. An event that the browser dispatches, for example a click of the user, also gives the registration context. |
|
|
181
214
|
|
|
182
215
|
## Patched APIs
|
|
183
216
|
|
|
@@ -201,6 +234,7 @@ In transformed code, each step gets the context of its own operation. Code witho
|
|
|
201
234
|
| The other code | What to do |
|
|
202
235
|
| --- | --- |
|
|
203
236
|
| Gives a promise, and your code awaits it | Nothing. After the `await`, your code gets its context again. |
|
|
237
|
+
| Is async code that a compiler changed before the publish (`__awaiter` of TypeScript, `_asyncToGenerator` of Babel) | Nothing. The helpers continue in `then` callbacks, and the patch of `then` keeps the context. |
|
|
204
238
|
| Starts your callback synchronously, or from a timer, an observer or a promise that it registers before its first `await` | Nothing. The patched APIs keep the context. Event listeners follow rule C13. |
|
|
205
239
|
| Starts your callback after its own native `await` | `AsyncLocalStorage.bind(callback)` or `AsyncContext.Snapshot.wrap(callback)` |
|
|
206
240
|
| Keeps your callback in a list and starts it later from other code | `AsyncLocalStorage.bind(callback)` when you give the callback |
|
|
@@ -213,7 +247,7 @@ untransformedLibrary.onDone(AsyncLocalStorage.bind((result) => {
|
|
|
213
247
|
}));
|
|
214
248
|
```
|
|
215
249
|
|
|
216
|
-
The Vite plugin transforms the dependencies in `node_modules` by default. Thus, these boundaries are usually only scripts from other servers, browser extensions and code from `eval()` or `new Function()`. The tests in `test/rules/c07-boundaries.test.ts` examine each row. The [boundaries page](https://mark1russell7.github.io/AsyncBrowserContext/docs/concepts/boundaries) of the website gives the details.
|
|
250
|
+
The Vite plugin transforms the dependencies in `node_modules` by default. Thus, these boundaries are usually only scripts from other servers, browser extensions and code from `eval()` or `new Function()`. The tests in `test/rules/c07-boundaries.test.ts` and `test/rules/c07-compiled-dependencies.test.ts` examine each row. The [boundaries page](https://mark1russell7.github.io/AsyncBrowserContext/docs/concepts/boundaries) of the website gives the details.
|
|
217
251
|
|
|
218
252
|
Do not use the library together with zone.js or with another library that patches `Promise.prototype.then`. The two libraries patch the same functions with different rules. For OpenTelemetry, use `AsyncContextManager` in place of the `ZoneContextManager`.
|
|
219
253
|
|
|
@@ -221,6 +255,7 @@ Do not use the library together with zone.js or with another library that patche
|
|
|
221
255
|
|
|
222
256
|
- **Generator functions.** A transformed generator function is an ordinary function that gives a generator. `fn.prototype` and `instanceof fn` do not operate as for a native generator function. The generator objects operate as native generator objects.
|
|
223
257
|
- **Stack traces.** In transformed code, a stack trace shows the generator and the `coroutine` function of the runtime.
|
|
258
|
+
- **Profilers.** Between the browser and each callback of a patched API, stack traces and profilers show the wrapper `asyncBrowserContextWrapper`. The Long Animation Frames API of Chromium shows this name in place of your function. `addCallbackHook()` gives the function and the context of each callback.
|
|
224
259
|
|
|
225
260
|
## Performance
|
|
226
261
|
|
|
@@ -237,6 +272,10 @@ These results are from `pnpm bench`: the mean time of each scenario, on Node.js
|
|
|
237
272
|
|
|
238
273
|
One read with `get()` takes approximately 4 ns. The [documentation website](https://mark1russell7.github.io/AsyncBrowserContext/) shows the full results.
|
|
239
274
|
|
|
275
|
+
When the page loads, the library installs its patches in approximately 8 ms: the median of Chromium, Firefox and WebKit on the same computer.
|
|
276
|
+
|
|
277
|
+
The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 4 to 9 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`).
|
|
278
|
+
|
|
240
279
|
## Tests
|
|
241
280
|
|
|
242
281
|
| Command | Function |
|
|
@@ -245,8 +284,11 @@ One read with `get()` takes approximately 4 ns. The [documentation website](http
|
|
|
245
284
|
| `pnpm test:node` | The tests in Node.js only |
|
|
246
285
|
| `pnpm test:browser` | The tests in Chromium, Firefox and WebKit |
|
|
247
286
|
| `pnpm smoke` | Packs the package, installs it in a new Vite application, and examines the application in Chromium |
|
|
287
|
+
| `pnpm test:babel` | Packs the package, transforms a program with the preset in Babel 7.22, the newest Babel 7 and Babel 8, and starts the output |
|
|
288
|
+
| `pnpm test:bundlers` | Packs the package, builds a page with webpack, Rspack, Rollup, esbuild and Next.js, and examines each page in Chromium |
|
|
248
289
|
| `pnpm mutation` | Mutation testing with Stryker |
|
|
249
290
|
| `pnpm bench` | The benchmarks |
|
|
291
|
+
| `pnpm bench:size` | The size of the runtime, and the size that the transform adds to a set of npm packages |
|
|
250
292
|
| `pnpm lint:ste` | The STE linter of the documentation and the TSDoc comments |
|
|
251
293
|
|
|
252
294
|
Set `BROWSERS=chromium,firefox,webkit` to select the browsers. Playwright WebKit does not start on all Windows computers, so the default on Windows is Chromium and Firefox. `docs/testing.md` tells more.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { types, type NodePath, type PluginObject, type PluginPass } from "@babel/core";
|
|
2
|
+
/** The options of the plugin. */
|
|
3
|
+
export interface NameCoroutinesOptions {
|
|
4
|
+
/** The module that exports `coroutine`. */
|
|
5
|
+
readonly runtime: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* This function gives the `name` that JavaScript gives to the function, or
|
|
9
|
+
* `undefined` if the name is not known before run time.
|
|
10
|
+
*/
|
|
11
|
+
export declare function functionName(path: NodePath<types.Function>): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* The Babel plugin that gives the name of each async function to `coroutine`:
|
|
14
|
+
* `_coroutine(function* () {...}, "load")`. The generator function of
|
|
15
|
+
* `@babel/plugin-transform-async-to-generator` has no name, and the callback
|
|
16
|
+
* hooks of the runtime show the name of the async function.
|
|
17
|
+
*
|
|
18
|
+
* In `Program` enter, before the async transform, the plugin records the
|
|
19
|
+
* name of each async function. In `Program` exit, it adds the name to each
|
|
20
|
+
* `coroutine` call. For a plain function, the async transform keeps the
|
|
21
|
+
* function node as the argument of `coroutine`. For a method, the call is in
|
|
22
|
+
* the body of the method.
|
|
23
|
+
*/
|
|
24
|
+
export declare function nameCoroutinesPlugin(_api: unknown, options: object): PluginObject<PluginPass>;
|
|
25
|
+
//# sourceMappingURL=name-coroutines.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name-coroutines.d.ts","sourceRoot":"","sources":["../../src/babel/name-coroutines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,2CAA2C;IAC3C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAyBD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAI,MAAM,GAAG,SAAS,CAsBjF;AAcD;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CA4ChG"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { types } from "@babel/core";
|
|
2
|
+
/** The local names that Babel gives to the import of `coroutine`. */
|
|
3
|
+
const COROUTINE_NAME = /^_coroutine\d*$/;
|
|
4
|
+
/** The assignment operators that give a name to an anonymous function, as in the specification (NamedEvaluation). */
|
|
5
|
+
const NAMING_OPERATORS = new Set(["=", "&&=", "||=", "??="]);
|
|
6
|
+
/** This function gives the name of a property key, or `undefined` if the name is known only at run time. */
|
|
7
|
+
function keyName(key, computed) {
|
|
8
|
+
if (types.isStringLiteral(key)) {
|
|
9
|
+
return key.value;
|
|
10
|
+
}
|
|
11
|
+
if (types.isNumericLiteral(key)) {
|
|
12
|
+
return String(key.value);
|
|
13
|
+
}
|
|
14
|
+
if (computed) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
if (types.isIdentifier(key)) {
|
|
18
|
+
return key.name;
|
|
19
|
+
}
|
|
20
|
+
return types.isPrivateName(key) ? `#${key.id.name}` : undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This function gives the `name` that JavaScript gives to the function, or
|
|
24
|
+
* `undefined` if the name is not known before run time.
|
|
25
|
+
*/
|
|
26
|
+
export function functionName(path) {
|
|
27
|
+
const node = path.node;
|
|
28
|
+
if ((types.isFunctionDeclaration(node) || types.isFunctionExpression(node)) && node.id) {
|
|
29
|
+
return node.id.name;
|
|
30
|
+
}
|
|
31
|
+
if (types.isObjectMethod(node) || types.isClassMethod(node) || types.isClassPrivateMethod(node)) {
|
|
32
|
+
return keyName(node.key, node.computed === true);
|
|
33
|
+
}
|
|
34
|
+
const parent = path.parent;
|
|
35
|
+
if (types.isVariableDeclarator(parent) && parent.init === node && types.isIdentifier(parent.id)) {
|
|
36
|
+
return parent.id.name;
|
|
37
|
+
}
|
|
38
|
+
if (types.isAssignmentExpression(parent) && parent.right === node && NAMING_OPERATORS.has(parent.operator) && types.isIdentifier(parent.left)) {
|
|
39
|
+
return parent.left.name;
|
|
40
|
+
}
|
|
41
|
+
if (types.isAssignmentPattern(parent) && parent.right === node && types.isIdentifier(parent.left)) {
|
|
42
|
+
return parent.left.name;
|
|
43
|
+
}
|
|
44
|
+
if ((types.isObjectProperty(parent) || types.isClassProperty(parent) || types.isClassPrivateProperty(parent)) && parent.value === node) {
|
|
45
|
+
return keyName(parent.key, "computed" in parent && parent.computed === true);
|
|
46
|
+
}
|
|
47
|
+
return types.isExportDefaultDeclaration(parent) ? "default" : undefined;
|
|
48
|
+
}
|
|
49
|
+
/** This function gives `true` if `callee` is the `coroutine` function of the runtime, also in CommonJS output. */
|
|
50
|
+
function isCoroutineCallee(callee, path, runtime) {
|
|
51
|
+
if (types.isIdentifier(callee)) {
|
|
52
|
+
return COROUTINE_NAME.test(callee.name) || path.referencesImport(runtime, "coroutine");
|
|
53
|
+
}
|
|
54
|
+
const member = types.isSequenceExpression(callee) ? callee.expressions.at(-1) : callee;
|
|
55
|
+
return types.isMemberExpression(member) && types.isIdentifier(member.property, { name: "coroutine" });
|
|
56
|
+
}
|
|
57
|
+
/** The key of the names of one file in the plugin state. */
|
|
58
|
+
const NAMES_KEY = "async-browser-context/coroutine-names";
|
|
59
|
+
/**
|
|
60
|
+
* The Babel plugin that gives the name of each async function to `coroutine`:
|
|
61
|
+
* `_coroutine(function* () {...}, "load")`. The generator function of
|
|
62
|
+
* `@babel/plugin-transform-async-to-generator` has no name, and the callback
|
|
63
|
+
* hooks of the runtime show the name of the async function.
|
|
64
|
+
*
|
|
65
|
+
* In `Program` enter, before the async transform, the plugin records the
|
|
66
|
+
* name of each async function. In `Program` exit, it adds the name to each
|
|
67
|
+
* `coroutine` call. For a plain function, the async transform keeps the
|
|
68
|
+
* function node as the argument of `coroutine`. For a method, the call is in
|
|
69
|
+
* the body of the method.
|
|
70
|
+
*/
|
|
71
|
+
export function nameCoroutinesPlugin(_api, options) {
|
|
72
|
+
const { runtime } = options;
|
|
73
|
+
return {
|
|
74
|
+
name: "async-browser-context/name-coroutines",
|
|
75
|
+
visitor: {
|
|
76
|
+
Program: {
|
|
77
|
+
enter(programPath, state) {
|
|
78
|
+
const names = new WeakMap();
|
|
79
|
+
programPath.traverse({
|
|
80
|
+
Function(path) {
|
|
81
|
+
if (!path.node.async || path.node.generator) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const name = functionName(path);
|
|
85
|
+
if (name !== undefined && name !== "") {
|
|
86
|
+
names.set(path.node, name);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
state.set(NAMES_KEY, names);
|
|
91
|
+
},
|
|
92
|
+
exit(programPath, state) {
|
|
93
|
+
const names = state.get(NAMES_KEY);
|
|
94
|
+
programPath.traverse({
|
|
95
|
+
CallExpression(path) {
|
|
96
|
+
const { callee, arguments: args } = path.node;
|
|
97
|
+
const [body] = args;
|
|
98
|
+
if (args.length !== 1 || !types.isFunctionExpression(body) || !body.generator) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!isCoroutineCallee(callee, path.get("callee"), runtime)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const parent = path.getFunctionParent()?.node;
|
|
105
|
+
const name = names.get(body) ?? (types.isMethod(parent) ? names.get(parent) : undefined);
|
|
106
|
+
if (name !== undefined) {
|
|
107
|
+
args.push(types.stringLiteral(name));
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=name-coroutines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name-coroutines.js","sourceRoot":"","sources":["../../src/babel/name-coroutines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqD,MAAM,aAAa,CAAC;AAQvF,qEAAqE;AACrE,MAAM,cAAc,GAAG,iBAAiB,CAAC;AAEzC,qHAAqH;AACrH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAE7D,4GAA4G;AAC5G,SAAS,OAAO,CAAC,GAAgB,EAAE,QAAkB;IACjD,IAAI,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7B,OAAO,GAAG,CAAC,KAAK,CAAC;IACrB,CAAC;IACD,IAAI,KAAK,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IACD,IAAI,QAAQ,EAAE,CAAC;QACX,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,IAAI,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,OAAO,GAAG,CAAC,IAAI,CAAC;IACpB,CAAC;IACD,OAAO,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AACpE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,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C;gCAC1D,OAAO;4BACX,CAAC;4BACD,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,EAAE,EAAE,IAAI,CAAC;4BAC9C,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;4BACzF,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gCACrB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;4BACzC,CAAC;wBACL,CAAC;qBACJ,CAAC,CAAC;gBACP,CAAC;aACJ;SACJ;KACJ,CAAC;AACN,CAAC"}
|
package/dist/babel/preset.d.ts
CHANGED
|
@@ -7,6 +7,13 @@ export interface PresetOptions {
|
|
|
7
7
|
* `bindGenerator`. The default is `async-browser-context/runtime`.
|
|
8
8
|
*/
|
|
9
9
|
readonly runtime?: string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Give the name of each async function to `coroutine`, for the callback
|
|
12
|
+
* hooks. The default is `true`. With `false`, the hooks get no name for the
|
|
13
|
+
* steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
|
|
14
|
+
* of its gzip size (`pnpm bench:size`).
|
|
15
|
+
*/
|
|
16
|
+
readonly names?: boolean | undefined;
|
|
10
17
|
}
|
|
11
18
|
/** The default runtime module. */
|
|
12
19
|
export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
@@ -16,9 +23,12 @@ export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
16
23
|
*
|
|
17
24
|
* 1. A plugin of this library binds each generator to the context of its
|
|
18
25
|
* creation.
|
|
19
|
-
* 2.
|
|
26
|
+
* 2. A plugin of this library gives the name of each async function to
|
|
27
|
+
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
28
|
+
* this step.
|
|
29
|
+
* 3. `@babel/plugin-transform-async-generator-functions` changes async
|
|
20
30
|
* generators and `for await` loops.
|
|
21
|
-
*
|
|
31
|
+
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
22
32
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
23
33
|
*
|
|
24
34
|
* 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,
|
|
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;AAMvE,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;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAY9G"}
|
package/dist/babel/preset.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
|
|
2
2
|
import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
|
|
3
3
|
import { bindGeneratorsPlugin } from "./bind-generators.js";
|
|
4
|
+
import { nameCoroutinesPlugin } from "./name-coroutines.js";
|
|
4
5
|
export { METADATA_KEY } from "./bind-generators.js";
|
|
5
6
|
/** The default runtime module. */
|
|
6
7
|
export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
@@ -10,9 +11,12 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
10
11
|
*
|
|
11
12
|
* 1. A plugin of this library binds each generator to the context of its
|
|
12
13
|
* creation.
|
|
13
|
-
* 2.
|
|
14
|
+
* 2. A plugin of this library gives the name of each async function to
|
|
15
|
+
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
16
|
+
* this step.
|
|
17
|
+
* 3. `@babel/plugin-transform-async-generator-functions` changes async
|
|
14
18
|
* generators and `for await` loops.
|
|
15
|
-
*
|
|
19
|
+
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
16
20
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
17
21
|
*
|
|
18
22
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -21,9 +25,11 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
21
25
|
export default function asyncBrowserContextPreset(api, options = {}) {
|
|
22
26
|
api.assertVersion("^7.22.0 || ^8.0.0");
|
|
23
27
|
const runtime = options.runtime ?? DEFAULT_RUNTIME;
|
|
28
|
+
const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
|
|
24
29
|
return {
|
|
25
30
|
plugins: [
|
|
26
31
|
[bindGeneratorsPlugin, { runtime }],
|
|
32
|
+
...names,
|
|
27
33
|
asyncGeneratorFunctions,
|
|
28
34
|
[asyncToGenerator, { module: runtime, method: "coroutine" }],
|
|
29
35
|
],
|
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;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
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;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;GAgBG;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,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,uBAAuB;YACvB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A callback hook. The library starts `before` immediately before each
|
|
3
|
+
* callback that it starts, and `after` immediately after the callback, also
|
|
4
|
+
* after an error. Both methods operate in the context of the callback, so
|
|
5
|
+
* `variable.get()` and `storage.getStore()` give the values of the callback.
|
|
6
|
+
*/
|
|
7
|
+
export interface CallbackHook {
|
|
8
|
+
/**
|
|
9
|
+
* The library starts this method before the callback.
|
|
10
|
+
*
|
|
11
|
+
* @param callback - The function that the library starts. For the step
|
|
12
|
+
* of an async function after an `await`, it is the body of the async
|
|
13
|
+
* function, with the name of the async function.
|
|
14
|
+
* @param source - The API that got the callback, for example
|
|
15
|
+
* `"setTimeout"`, `"Promise.then"`, `"addEventListener:click"`,
|
|
16
|
+
* `"onclick"` or `"await"`.
|
|
17
|
+
*/
|
|
18
|
+
before?(callback: Function, source: string): void;
|
|
19
|
+
/** The library starts this method after the callback, also after an error. The arguments are the same as for `before`. */
|
|
20
|
+
after?(callback: Function, source: string): void;
|
|
21
|
+
}
|
|
22
|
+
/** This function makes sure that `hook` is an object, and that `before` and `after` are functions or `undefined`. */
|
|
23
|
+
export declare function assertCallbackHook(hook: CallbackHook): void;
|
|
24
|
+
//# sourceMappingURL=callback-hook.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-hook.d.ts","sourceRoot":"","sources":["../../src/core/callback-hook.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IACzB;;;;;;;;;OASG;IACH,MAAM,CAAC,CAAC,QAAQ,EAAG,QAAQ,EAAE,MAAM,EAAG,MAAM,GAAI,IAAI,CAAC;IACrD,0HAA0H;IAC1H,KAAK,CAAC,CAAC,QAAQ,EAAG,QAAQ,EAAE,MAAM,EAAG,MAAM,GAAI,IAAI,CAAC;CACvD;AAED,qHAAqH;AACrH,wBAAgB,kBAAkB,CAAC,IAAI,EAAG,YAAY,GAAI,IAAI,CAU7D"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** This function makes sure that `hook` is an object, and that `before` and `after` are functions or `undefined`. */
|
|
2
|
+
export function assertCallbackHook(hook) {
|
|
3
|
+
if (typeof hook !== "object" || hook === null) {
|
|
4
|
+
throw new TypeError("addCallbackHook: the hook is not an object");
|
|
5
|
+
}
|
|
6
|
+
for (const phase of ["before", "after"]) {
|
|
7
|
+
const method = hook[phase];
|
|
8
|
+
if (method !== undefined && typeof method !== "function") {
|
|
9
|
+
throw new TypeError(`addCallbackHook: hook.${phase} is not a function`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=callback-hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-hook.js","sourceRoot":"","sources":["../../src/core/callback-hook.ts"],"names":[],"mappings":"AAsBA,qHAAqH;AACrH,MAAM,UAAU,kBAAkB,CAAC,IAAmB;IAClD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAC5C,MAAM,IAAI,SAAS,CAAC,4CAA4C,CAAC,CAAC;IACtE,CAAC;IACD,KAAK,MAAM,KAAK,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAU,EAAE,CAAC;QAC/C,MAAM,MAAM,GAAa,IAAI,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,MAAM,KAAK,SAAS,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;YACvD,MAAM,IAAI,SAAS,CAAC,yBAAyB,KAAK,oBAAoB,CAAC,CAAC;QAC5E,CAAC;IACL,CAAC;AACL,CAAC"}
|
package/dist/core/coroutine.d.ts
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The code from the Babel preset uses these runtime functions. Do not use them
|
|
3
|
-
* directly.
|
|
4
|
-
*
|
|
5
|
-
* Each async function becomes a generator function, and `coroutine` operates it.
|
|
6
|
-
* Each `await` becomes a `yield`. Before each step of the generator,
|
|
7
|
-
* `coroutine` sets the context of the async function. After the step, it sets
|
|
8
|
-
* the previous context again. Thus, the context is correct after each `await`,
|
|
9
|
-
* also in the same expression, and no other code gets this context.
|
|
10
|
-
*/
|
|
11
1
|
/**
|
|
12
2
|
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
13
3
|
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
14
|
-
* `method` option.
|
|
4
|
+
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
15
5
|
*/
|
|
16
|
-
export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown
|
|
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>;
|
|
17
7
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
18
8
|
/**
|
|
19
9
|
* This function binds a generator object to the current context. The body of
|
|
@@ -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":"AAyBA;;;;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,CA0CxC;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CA2BtE"}
|
package/dist/core/coroutine.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { store } from "./store.js";
|
|
1
|
+
import { applyWithHooks, store } from "./store.js";
|
|
2
2
|
/**
|
|
3
3
|
* The code from the Babel preset uses these runtime functions. Do not use them
|
|
4
4
|
* directly.
|
|
@@ -9,25 +9,40 @@ import { store } from "./store.js";
|
|
|
9
9
|
* the previous context again. Thus, the context is correct after each `await`,
|
|
10
10
|
* also in the same expression, and no other code gets this context.
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* This function gives the generator function to the callback hooks, with the name of the
|
|
14
|
+
* async function. The preset gives the name, because the generator function
|
|
15
|
+
* of Babel has no name. The function sets the name only when a hook operates.
|
|
16
|
+
*/
|
|
17
|
+
function namedForHooks(generatorFunction, name) {
|
|
18
|
+
if (name !== undefined && generatorFunction.name !== name) {
|
|
19
|
+
Object.defineProperty(generatorFunction, "name", { value: name, configurable: true });
|
|
20
|
+
}
|
|
21
|
+
return generatorFunction;
|
|
22
|
+
}
|
|
12
23
|
/**
|
|
13
24
|
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
14
25
|
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
15
|
-
* `method` option.
|
|
26
|
+
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
16
27
|
*/
|
|
17
|
-
export function coroutine(generatorFunction) {
|
|
28
|
+
export function coroutine(generatorFunction, name) {
|
|
18
29
|
const { Promise: NativePromise, promiseThen, promiseResolve } = store.intrinsics;
|
|
19
30
|
return function asyncFunction(...args) {
|
|
20
31
|
// The context of the call. The body can change it with AsyncLocalStorage.enterWith.
|
|
21
32
|
let frame = store.current;
|
|
22
33
|
const generator = generatorFunction.apply(this, args);
|
|
23
34
|
return new NativePromise((resolve, reject) => {
|
|
24
|
-
|
|
35
|
+
/** `resumed` is `true` for a step after an `await`. Only these steps start the hook methods. */
|
|
36
|
+
const step = (resume, argument, resumed) => {
|
|
25
37
|
const previous = store.current;
|
|
26
38
|
store.current = frame;
|
|
27
39
|
let result;
|
|
28
40
|
let awaited;
|
|
29
41
|
try {
|
|
30
|
-
|
|
42
|
+
const hooks = resumed ? store.hooks : undefined;
|
|
43
|
+
result = hooks === undefined
|
|
44
|
+
? resume(argument)
|
|
45
|
+
: applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
|
|
31
46
|
if (result.done) {
|
|
32
47
|
frame = store.current;
|
|
33
48
|
store.current = previous;
|
|
@@ -48,9 +63,9 @@ export function coroutine(generatorFunction) {
|
|
|
48
63
|
};
|
|
49
64
|
const next = (value) => generator.next(value);
|
|
50
65
|
const throwInto = (error) => generator.throw(error);
|
|
51
|
-
const onFulfilled = (value) => step(next, value);
|
|
52
|
-
const onRejected = (error) => step(throwInto, error);
|
|
53
|
-
step(next, undefined);
|
|
66
|
+
const onFulfilled = (value) => step(next, value, true);
|
|
67
|
+
const onRejected = (error) => step(throwInto, error, true);
|
|
68
|
+
step(next, undefined, false);
|
|
54
69
|
});
|
|
55
70
|
};
|
|
56
71
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAc,MAAM,YAAY,CAAC;AAE/
|
|
1
|
+
{"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAc,MAAM,YAAY,CAAC;AAE/D;;;;;;;;;GASG;AAEH;;;;GAIG;AACH,SAAS,aAAa,CAAC,iBAA4B,EAAE,IAAyB;IAC1E,IAAI,IAAI,KAAK,SAAS,IAAI,iBAAiB,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACxD,MAAM,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,EAAE,EAAE,KAAK,EAAG,IAAI,EAAE,YAAY,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ook } from "./callback-hook.js";
|
|
3
|
+
/**
|
|
4
|
+
* This function adds a hook. The library starts its methods before and after each
|
|
5
|
+
* callback that it starts. These callbacks are the callbacks of the patched
|
|
6
|
+
* APIs and the step of each transformed async function after an `await`. The
|
|
7
|
+
* function gives a function that removes the hook.
|
|
8
|
+
*
|
|
9
|
+
* A tool can use the hook to measure the time of each callback, with the
|
|
10
|
+
* name of the callback and the values of its context. An error in a hook goes
|
|
11
|
+
* to `reportError()`, and the callback operates as usual. A callback that
|
|
12
|
+
* starts synchronously in a hook does not start the hook methods.
|
|
13
|
+
*/
|
|
14
|
+
export declare function addCallbackHook(hook: CallbackHook): () => void;
|
|
15
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/core/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAG3E,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAG,YAAY,GAAI,MAAM,IAAI,CAahE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { assertCallbackHook } from "./callback-hook.js";
|
|
2
|
+
import { store } from "./store.js";
|
|
3
|
+
/**
|
|
4
|
+
* This function adds a hook. The library starts its methods before and after each
|
|
5
|
+
* callback that it starts. These callbacks are the callbacks of the patched
|
|
6
|
+
* APIs and the step of each transformed async function after an `await`. The
|
|
7
|
+
* function gives a function that removes the hook.
|
|
8
|
+
*
|
|
9
|
+
* A tool can use the hook to measure the time of each callback, with the
|
|
10
|
+
* name of the callback and the values of its context. An error in a hook goes
|
|
11
|
+
* to `reportError()`, and the callback operates as usual. A callback that
|
|
12
|
+
* starts synchronously in a hook does not start the hook methods.
|
|
13
|
+
*/
|
|
14
|
+
export function addCallbackHook(hook) {
|
|
15
|
+
assertCallbackHook(hook);
|
|
16
|
+
const entry = { hook };
|
|
17
|
+
store.hooks = [...(store.hooks ?? []), entry];
|
|
18
|
+
let removed = false;
|
|
19
|
+
return function removeCallbackHook() {
|
|
20
|
+
if (removed) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
removed = true;
|
|
24
|
+
const remaining = (store.hooks ?? []).filter((other) => other !== entry);
|
|
25
|
+
store.hooks = remaining.length === 0 ? undefined : remaining;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/core/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAqB,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,KAAK,EAAkB,MAAM,YAAY,CAAC;AAInD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAAC,IAAmB;IAC/C,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACzB,MAAM,KAAK,GAAe,EAAE,IAAI,EAAE,CAAC;IACnC,KAAK,CAAC,KAAK,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9C,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,OAAO,SAAS,kBAAkB;QAC9B,IAAI,OAAO,EAAE,CAAC;YACV,OAAO;QACX,CAAC;QACD,OAAO,GAAG,IAAI,CAAC;QACf,MAAM,SAAS,GAAG,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC;QACzE,KAAK,CAAC,KAAK,GAAG,SAAS,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IACjE,CAAC,CAAC;AACN,CAAC"}
|