-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
async_hooks.ts
77 lines (68 loc) · 1.93 KB
/
async_hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
AsyncResource,
createHook,
triggerAsyncId,
executionAsyncId,
executionAsyncResource,
HookCallbacks,
AsyncLocalStorage,
} from 'node:async_hooks';
{
const hooks: HookCallbacks = {
init() {},
before() {},
after() {},
destroy() {},
promiseResolve() {},
};
const asyncHook = createHook(hooks);
asyncHook.enable().disable().enable();
const tId: number = triggerAsyncId();
const eId: number = executionAsyncId();
const curRes: object = executionAsyncResource();
class TestResource extends AsyncResource {
constructor() {
super('TEST_RESOURCE');
}
}
class AnotherTestResource extends AsyncResource {
constructor() {
super('TEST_RESOURCE', 42);
const aId: number = this.asyncId();
const tId: number = this.triggerAsyncId();
}
run() {
this.runInAsyncScope(() => {});
this.runInAsyncScope(Array.prototype.find, [], () => true);
}
destroy() {
this.emitDestroy();
}
}
// check AsyncResource constructor options.
new AsyncResource('');
new AsyncResource('', 0);
new AsyncResource('', {});
new AsyncResource('', { triggerAsyncId: 0 });
new AsyncResource('', {
triggerAsyncId: 0,
requireManualDestroy: true
});
let res = AsyncResource.bind((x: number) => x)(42);
const asyncResource = new AsyncResource('');
res = asyncResource.bind((x: number) => x)(42);
// $ExpectType AsyncResource
asyncResource.emitDestroy();
}
{
const ctx = new AsyncLocalStorage<string>();
ctx.disable();
const exitResult: number = ctx.exit((a: number) => {
return 42;
}, 1);
const runResult: number = ctx.run('test', (a: number) => {
const store: string | undefined = ctx.getStore();
return 42;
}, 1);
ctx.enterWith('test');
}