UNPKG

4.57 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10/**
11 * EventSubscription represents a subscription to a particular event. It can
12 * remove its own subscription.
13 */
14interface EventSubscription {
15 eventType: string;
16 key: number;
17 subscriber: EventSubscriptionVendor;
18
19 /**
20 * @param subscriber the subscriber that controls
21 * this subscription.
22 */
23 new (subscriber: EventSubscriptionVendor): EventSubscription;
24
25 /**
26 * Removes this subscription from the subscriber that controls it.
27 */
28 remove(): void;
29}
30
31/**
32 * EventSubscriptionVendor stores a set of EventSubscriptions that are
33 * subscribed to a particular event type.
34 */
35declare 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 */
76interface EmitterSubscription extends EventSubscription {
77 emitter: EventEmitter;
78 listener: () => any;
79 context: any;
80
81 /**
82 * @param emitter - The event emitter that registered this
83 * subscription
84 * @param subscriber - The subscriber that controls
85 * this subscription
86 * @param listener - Function to invoke when the specified event is
87 * emitted
88 * @param context - Optional context object to use when invoking the
89 * listener
90 */
91 new (
92 emitter: EventEmitter,
93 subscriber: EventSubscriptionVendor,
94 listener: () => any,
95 context: any,
96 ): EmitterSubscription;
97
98 /**
99 * Removes this subscription from the emitter that registered it.
100 * Note: we're overriding the `remove()` method of EventSubscription here
101 * but deliberately not calling `super.remove()` as the responsibility
102 * for removing the subscription lies with the EventEmitter.
103 */
104 remove(): void;
105}
106
107export default class EventEmitter {
108 /**
109 *
110 * @param subscriber - Optional subscriber instance
111 * to use. If omitted, a new subscriber will be created for the emitter.
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}