Commit | Line | Data |
---|---|---|
b5604034 JB |
1 | import type { AsyncResource, AsyncResourceOptions } from 'node:async_hooks' |
2 | import { EventEmitter } from 'node:events' | |
3 | ||
4 | declare module 'node:events' { | |
5 | interface EventEmitterOptions { | |
6 | /** | |
7 | * Enables automatic capturing of promise rejection. | |
8 | */ | |
9 | captureRejections?: boolean | undefined | |
10 | } | |
11 | ||
12 | interface EventEmitterAsyncResourceOptions | |
13 | extends AsyncResourceOptions, | |
14 | EventEmitterOptions { | |
15 | /** | |
16 | * The type of async event. | |
17 | * @default new.target.name | |
18 | */ | |
19 | name?: string | |
20 | } | |
21 | ||
22 | /** | |
23 | * Integrates `EventEmitter` with `AsyncResource` for `EventEmitters` that require | |
24 | * manual async tracking. Specifically, all events emitted by instances of | |
25 | * `EventEmitterAsyncResource` will run within its async context. | |
26 | * | |
27 | * The EventEmitterAsyncResource class has the same methods and takes the | |
28 | * same options as EventEmitter and AsyncResource themselves. | |
29 | */ | |
30 | export class EventEmitterAsyncResource extends EventEmitter { | |
31 | constructor (options?: EventEmitterAsyncResourceOptions) | |
32 | /** | |
33 | * Call all `destroy` hooks. This should only ever be called once. An error will | |
34 | * be thrown if it is called more than once. This **must** be manually called. If | |
35 | * the resource is left to be collected by the GC then the `destroy` hooks will | |
36 | * never be called. | |
37 | * @return A reference to `asyncResource`. | |
38 | */ | |
39 | emitDestroy (): AsyncResource | |
40 | /** The unique asyncId assigned to the resource. */ | |
41 | get asyncId (): number | |
42 | /** The same triggerAsyncId that is passed to the AsyncResource constructor. */ | |
43 | get triggerAsyncId (): number | |
44 | /** The underlying AsyncResource */ | |
45 | get asyncResource (): AsyncResource & { | |
46 | readonly eventEmitter: EventEmitterAsyncResource | |
47 | } | |
48 | } | |
49 | } |