UNPKG

3.46 kBTypeScriptView Raw
1type Emitter = {
2 /**
3 Register an event handler that listens to a specified event.
4
5 @param event - The name of the event to listen to.
6 @param listener - The function to execute when the event is emitted.
7 @returns The Emitter instance for method chaining.
8 */
9 on(event: string, listener: (...arguments_: any[]) => void): Emitter;
10
11 /**
12 Register a one-time event handler for a specified event.
13
14 @param event - The name of the event to listen to.
15 @param listener - The function to execute once when the event is emitted.
16 @returns The Emitter instance for method chaining.
17 */
18 once(event: string, listener: (...arguments_: any[]) => void): Emitter;
19
20 /**
21 Remove a specific event handler for a specified event.
22
23 @param event - The name of the event.
24 @param listener - The specific handler function to remove.
25 @returns The Emitter instance for method chaining.
26 */
27 off(event: string, listener: (...arguments_: any[]) => void): Emitter; // eslint-disable-line @typescript-eslint/unified-signatures
28
29 /**
30 Remove all event handlers for a specified event.
31
32 @param event - The name of the event for which to remove all handlers.
33 @returns The Emitter instance for method chaining.
34 */
35 off(event: string): Emitter; // eslint-disable-line @typescript-eslint/unified-signatures
36
37 /**
38 Remove all event handlers for all events.
39
40 @returns The Emitter instance for method chaining.
41 */
42 off(): Emitter;
43
44 /**
45 Emit an event, invoking all handlers registered for it.
46
47 @param event - The name of the event to emit.
48 @param arguments_ - Arguments to pass to the event handlers.
49 @returns The Emitter instance for method chaining.
50 */
51 emit(event: string, ...arguments_: any[]): Emitter;
52
53 /**
54 Retrieve the event handlers registered for a specific event.
55
56 @param event - The name of the event.
57 @returns An array of functions registered as handlers for the event.
58 */
59 listeners(event: string): Array<(...arguments_: any[]) => void>;
60
61 /**
62 Get the count of listeners for a specific event.
63
64 @param event - The name of the event.
65 @returns The number of listeners for the event.
66 */
67 listenerCount(event: string): number; // eslint-disable-line @typescript-eslint/unified-signatures
68
69 /**
70 Get the count of all event handlers in total.
71
72 @returns The total number of event handlers.
73 */
74 listenerCount(): number;
75
76 /**
77 Check if there are any handlers registered for a specific event.
78
79 @param event - The name of the event.
80 @returns `true` if there are one or more handlers, `false` otherwise.
81 */
82 hasListeners(event: string): boolean; // eslint-disable-line @typescript-eslint/unified-signatures
83
84 /**
85 Check if there are any handlers registered for any event.
86
87 @returns `true` if there are one or more handlers for any event, `false` otherwise.
88 */
89 hasListeners(): boolean;
90};
91
92type EmitterConstructor = {
93 prototype: Emitter;
94 new (object?: object): Emitter; // eslint-disable-line @typescript-eslint/ban-types
95 <T extends object>(object: T): T & Emitter; // eslint-disable-line @typescript-eslint/ban-types
96};
97
98/**
99As an `Emitter` instance:
100
101```
102import Emitter from 'component-emitter';
103
104const emitter = new Emitter();
105
106emitter.emit('🦄');
107```
108
109As a mixin:
110
111```
112import Emitter from 'component-emitter';
113
114const user = {name: 'tobi'};
115Emitter(user);
116
117user.emit('I am a user');
118```
119
120As a prototype mixin:
121
122```
123import Emitter from 'component-emitter';
124
125Emitter(User.prototype);
126```
127*/
128declare const emitter: EmitterConstructor;
129
130export = emitter;