-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Emittery.d.ts
155 lines (133 loc) · 5.28 KB
/
Emittery.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
export = Emittery;
declare class Emittery {
/**
* Subscribe to an event.
*
* Returns an unsubscribe method.
*
* Using the same listener multiple times for the same event will result
* in only one method call per emitted event.
*/
on(eventName: string, listener: (eventData?: any) => any): Emittery.UnsubscribeFn;
/**
* Remove an event subscription.
*/
off(eventName: string, listener: (eventData?: any) => any): void;
/**
* Subscribe to an event only once. It will be unsubscribed after the first
* event.
*
* Returns a promise for the event data when `eventName` is emitted.
*/
once(eventName: string): Promise<any>;
/**
* Get an asynchronous iterator which buffers data each time an event is
* emitted.
*
* Call `return()` on the iterator to remove the subscription.
*/
events(eventName: string): AsyncIterableIterator<any>;
/**
* Trigger an event asynchronously, optionally with some data. Listeners
* are called in the order they were added, but execute concurrently.
*
* Returns a promise for when all the event listeners are done. *Done*
* meaning executed if synchronous or resolved when an
* async/promise-returning function. You usually wouldn't want to wait for
* this, but you could for example catch possible errors. If any of the
* listeners throw/reject, the returned promise will be rejected with the
* error, but the other listeners will not be affected.
*
* Returns a promise for when all the event listeners are done.
*/
emit(eventName: string, eventData?: any): Promise<void>;
/**
* Same as `emit()`, but it waits for each listener to resolve before
* triggering the next one. This can be useful if your events depend on each
* other. Although ideally they should not. Prefer `emit()` whenever
* possible.
*
* If any of the listeners throw/reject, the returned promise will be
* rejected with the error and the remaining listeners will *not* be called.
*
* Returns a promise for when all the event listeners are done.
*/
emitSerial(eventName: string, eventData?: any): Promise<void>;
/**
* Subscribe to be notified about any event.
*
* Returns a method to unsubscribe.
*/
onAny(listener: (eventName: string, eventData?: any) => any): Emittery.UnsubscribeFn;
/**
* Remove an `onAny` subscription.
*/
offAny(listener: (eventName: string, eventData?: any) => any): void;
/**
* Get an asynchronous iterator which buffers a tuple of an event name and
* data each time an event is emitted.
*
* Call `return()` on the iterator to remove the subscription.
*/
anyEvent(): AsyncIterableIterator<[string, any]>;
/**
* Clear all iterators and event listeners on the instance.
*
* If `eventName` is given, only the iterators and listeners for that event
* are cleared.
*/
clearListeners(eventName?: string): void;
/**
* The number of iterators and listeners for the `eventName` or all events
* if not specified.
*/
listenerCount(eventName?: string): number;
}
declare namespace Emittery {
/**
* Removes an event subscription.
*/
type UnsubscribeFn = () => void;
/**
* Maps event names to their emitted data type.
*/
interface Events {
[eventName: string]: any;
}
/**
* Async event emitter.
*
* Must list supported events and the data type they emit, if any.
*
* For example:
*
* ```ts
* import Emittery = require('emittery');
*
* const ee = new Emittery.Typed<{value: string}, 'open' | 'close'>();
*
* ee.emit('open');
* ee.emit('value', 'foo\n');
* ee.emit('value', 1); // TS compilation error
* ee.emit('end'); // TS compilation error
* ```
*/
class Typed<EventDataMap extends Events, EmptyEvents extends string = never> extends Emittery {
on<Name extends keyof EventDataMap>(eventName: Name, listener: (eventData: EventDataMap[Name]) => any): Emittery.UnsubscribeFn;
on<Name extends EmptyEvents>(eventName: Name, listener: () => any): Emittery.UnsubscribeFn;
once<Name extends keyof EventDataMap>(eventName: Name): Promise<EventDataMap[Name]>;
once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
off<Name extends keyof EventDataMap>(eventName: Name, listener?: (eventData: EventDataMap[Name]) => any): void;
off<Name extends EmptyEvents>(eventName: Name, listener?: () => any): void;
events<Name extends keyof EventDataMap>(eventName: Name): AsyncIterableIterator<EventDataMap[Name]>;
events<Name extends EmptyEvents>(eventName: Name): AsyncIterableIterator<void>;
onAny(listener: (eventName: keyof EventDataMap | EmptyEvents, eventData?: EventDataMap[keyof EventDataMap]) => any): Emittery.UnsubscribeFn;
offAny<Name extends keyof EventDataMap>(listener?: (eventName: Name, eventData: EventDataMap[Name]) => any): void;
offAny<Name extends EmptyEvents>(listener?: (eventName: Name) => any): void;
anyEvent(): AsyncIterableIterator<[keyof EventDataMap | EmptyEvents, EventDataMap[keyof EventDataMap] | void]>;
emit<Name extends keyof EventDataMap>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
emitSerial<Name extends keyof EventDataMap>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
}
}