1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | interface EventSubscription {
|
15 | eventType: string;
|
16 | key: number;
|
17 | subscriber: EventSubscriptionVendor;
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 | new (subscriber: EventSubscriptionVendor): EventSubscription;
|
24 |
|
25 | |
26 |
|
27 |
|
28 | remove(): void;
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | declare class EventSubscriptionVendor {
|
36 | constructor();
|
37 |
|
38 | /**
|
39 | * Adds a subscription keyed by an event type.
|
40 | *
|
41 | */
|
42 | addSubscription(
|
43 | eventType: string,
|
44 | subscription: EventSubscription,
|
45 | ): EventSubscription;
|
46 |
|
47 | /**
|
48 | * Removes a bulk set of the subscriptions.
|
49 | *
|
50 | * @param eventType - Optional name of the event type whose
|
51 | * registered subscriptions to remove, if null remove all subscriptions.
|
52 | */
|
53 | removeAllSubscriptions(eventType?: string): void;
|
54 |
|
55 | /**
|
56 | * Removes a specific subscription. Instead of calling this function, call
|
57 | * `subscription.remove()` directly.
|
58 | *
|
59 | */
|
60 | removeSubscription(subscription: any): void;
|
61 |
|
62 | /**
|
63 | * Returns the array of subscriptions that are currently registered for the
|
64 | * given event type.
|
65 | *
|
66 | * Note: This array can be potentially sparse as subscriptions are deleted
|
67 | * from it when they are removed.
|
68 | *
|
69 | */
|
70 | getSubscriptionsForType(eventType: string): EventSubscription[];
|
71 | }
|
72 |
|
73 | /**
|
74 | * EmitterSubscription represents a subscription with listener and context data.
|
75 | */
|
76 | interface EmitterSubscription extends EventSubscription {
|
77 | emitter: EventEmitter;
|
78 | listener: () => any;
|
79 | context: any;
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | new (
|
92 | emitter: EventEmitter,
|
93 | subscriber: EventSubscriptionVendor,
|
94 | listener: () => any,
|
95 | context: any,
|
96 | ): EmitterSubscription;
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | remove(): void;
|
105 | }
|
106 |
|
107 | export default class EventEmitter {
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | constructor(subscriber?: EventSubscriptionVendor | null);
|
114 |
|
115 | /**
|
116 | * Adds a listener to be invoked when events of the specified type are
|
117 | * emitted. An optional calling context may be provided. The data arguments
|
118 | * emitted will be passed to the listener function.
|
119 | *
|
120 | * @param eventType - Name of the event to listen to
|
121 | * @param listener - Function to invoke when the specified event is
|
122 | * emitted
|
123 | * @param context - Optional context object to use when invoking the
|
124 | * listener
|
125 | */
|
126 | addListener(
|
127 | eventType: string,
|
128 | listener: (...args: any[]) => any,
|
129 | context?: any,
|
130 | ): EmitterSubscription;
|
131 |
|
132 | /**
|
133 | * Removes all of the registered listeners, including those registered as
|
134 | * listener maps.
|
135 | *
|
136 | * @param eventType - Optional name of the event whose registered
|
137 | * listeners to remove
|
138 | */
|
139 | removeAllListeners(eventType?: string): void;
|
140 |
|
141 | /**
|
142 | * Returns the number of listeners that are currently registered for the given
|
143 | * event.
|
144 | *
|
145 | * @param eventType - Name of the event to query
|
146 | */
|
147 | listenerCount(eventType: string): number;
|
148 |
|
149 | /**
|
150 | * Emits an event of the given type with the given data. All handlers of that
|
151 | * particular type will be notified.
|
152 | *
|
153 | * @param eventType - Name of the event to emit
|
154 | * @param Arbitrary arguments to be passed to each registered listener
|
155 | *
|
156 | * @example
|
157 | * emitter.addListener('someEvent', function(message) {
|
158 | * console.log(message);
|
159 | * });
|
160 | *
|
161 | * emitter.emit('someEvent', 'abc'); // logs 'abc'
|
162 | */
|
163 | emit(eventType: string, ...params: any[]): void;
|
164 | }
|