@travetto/test 5.0.0-rc.9 → 5.0.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.
@@ -1,49 +0,0 @@
1
- import { createHook, executionAsyncId } from 'node:async_hooks';
2
- import { isPromise } from 'node:util/types';
3
-
4
- import { ExecutionError } from '@travetto/worker';
5
- import { Util } from '@travetto/runtime';
6
-
7
- /**
8
- * Promise watcher, to catch any unfinished promises
9
- */
10
- export class PromiseCapturer {
11
- #pending = new Map<number, Promise<unknown>>();
12
- #id: number = 0;
13
-
14
- #init(id: number, type: string, triggerId: number, resource: unknown): void {
15
- if (this.#id && type === 'PROMISE' && triggerId === this.#id && isPromise(resource)) {
16
- this.#pending.set(id, resource);
17
- }
18
- }
19
-
20
- #promiseResolve(asyncId: number): void {
21
- this.#pending.delete(asyncId);
22
- }
23
-
24
- async run(op: () => Promise<unknown> | unknown): Promise<unknown> {
25
- const hook = createHook({
26
- init: (...args) => this.#init(...args),
27
- promiseResolve: (id) => this.#promiseResolve(id)
28
- });
29
-
30
- hook.enable();
31
-
32
- await Util.queueMacroTask();
33
- this.#id = executionAsyncId();
34
- try {
35
- const res = await op();
36
- let i = 5; // Wait upto 5 macro tasks before continuing
37
- while (this.#pending.size) {
38
- await Util.queueMacroTask();
39
- i -= 1;
40
- if (i === 0) {
41
- throw new ExecutionError(`Pending promises: ${this.#pending.size}`);
42
- }
43
- }
44
- return res;
45
- } finally {
46
- hook.disable();
47
- }
48
- }
49
- }