source: trip-planner-front/node_modules/eventemitter-asyncresource/src/index.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import { EventEmitter } from 'events';
2import { AsyncResource } from 'async_hooks';
3
4const kEventEmitter = Symbol('kEventEmitter');
5const kAsyncResource = Symbol('kAsyncResource');
6
7type EventEmitterOptions = typeof EventEmitter extends {
8 new (options?: infer T) : EventEmitter;
9} ? T : never;
10
11type AsyncResourceOptions = typeof AsyncResource extends {
12 new (name : string, options?: infer T) : AsyncResource;
13} ? T : never;
14
15type Options = EventEmitterOptions & AsyncResourceOptions & {
16 name?: string;
17};
18
19class EventEmitterReferencingAsyncResource extends AsyncResource {
20 [kEventEmitter] : EventEmitter;
21
22 constructor (ee: EventEmitter, type: string, options?: AsyncResourceOptions) {
23 super(type, options);
24 this[kEventEmitter] = ee;
25 }
26
27 get eventEmitter () : EventEmitter {
28 return this[kEventEmitter];
29 }
30}
31
32class EventEmitterAsyncResource extends EventEmitter {
33 [kAsyncResource] : EventEmitterReferencingAsyncResource;
34
35 constructor (options?: Options | string) {
36 let name;
37 if (typeof options === 'string') {
38 name = options;
39 options = undefined;
40 } else {
41 name = options?.name || new.target.name;
42 }
43 super(options);
44
45 this[kAsyncResource] =
46 new EventEmitterReferencingAsyncResource(this, name, options);
47 }
48
49 emit (event: string | symbol, ...args: any[]) : boolean {
50 return this.asyncResource.runInAsyncScope(
51 super.emit, this, event, ...args);
52 }
53
54 emitDestroy () : void {
55 this.asyncResource.emitDestroy();
56 }
57
58 asyncId () : number {
59 return this.asyncResource.asyncId();
60 }
61
62 triggerAsyncId () : number {
63 return this.asyncResource.triggerAsyncId();
64 }
65
66 get asyncResource () : EventEmitterReferencingAsyncResource {
67 return this[kAsyncResource];
68 }
69
70 static get EventEmitterAsyncResource () { return EventEmitterAsyncResource; }
71}
72
73export = EventEmitterAsyncResource;
Note: See TracBrowser for help on using the repository browser.