-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.test-d.ts
286 lines (248 loc) · 6.91 KB
/
index.test-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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* eslint-disable @typescript-eslint/no-empty-function, @typescript-eslint/no-floating-promises */
import {expectType, expectError, expectNotAssignable, expectAssignable} from 'tsd';
import {pEventIterator} from 'p-event';
import Emittery from './index.js';
type AnyListener = (eventData?: unknown) => void | Promise<void>;
// Emit
{
const ee = new Emittery();
ee.emit('anEvent');
ee.emit('anEvent', 'some data');
}
// On
{
const ee = new Emittery();
ee.on('anEvent', () => undefined);
ee.on('anEvent', async () => {});
ee.on('anEvent', data => undefined);
ee.on('anEvent', async data => {});
ee.on(['anEvent', 'anotherEvent'], async data => undefined);
ee.on(Emittery.listenerAdded, ({eventName, listener}) => {
expectType<PropertyKey | undefined>(eventName);
expectType<AnyListener>(listener);
});
ee.on(Emittery.listenerRemoved, ({eventName, listener}) => {
expectType<PropertyKey | undefined>(eventName);
expectType<AnyListener>(listener);
});
}
// Off
{
const ee = new Emittery();
ee.off('anEvent', () => undefined);
ee.off('anEvent', async () => {});
ee.off('anEvent', data => undefined);
ee.off('anEvent', async data => {});
ee.off(Emittery.listenerAdded, ({eventName, listener}) => {});
ee.off(Emittery.listenerRemoved, ({eventName, listener}) => {});
}
// Once
{
const ee = new Emittery();
const test = async () => {
await ee.once('anEvent');
await ee.once(Emittery.listenerAdded).then(({eventName, listener}) => {
expectType<PropertyKey | undefined>(eventName);
expectType<AnyListener>(listener);
});
await ee.once(Emittery.listenerRemoved).then(({eventName, listener}) => {
expectType<PropertyKey | undefined>(eventName);
expectType<AnyListener>(listener);
});
const oncePromise = ee.once('anotherEvent');
oncePromise.off();
await oncePromise;
};
}
{
const ee = new Emittery();
expectError(ee.emit('anEvent', 'some data', 'and more'));
}
{
const ee = new Emittery();
expectError(ee.on('anEvent', (data: any, more: any) => undefined));
}
// IsDebug
{
type MyEventData = {
value: string;
open: undefined;
close: boolean;
};
const ee = new Emittery<MyEventData>();
const myLogger = (type: string, debugName: string, eventName?: keyof MyEventData, eventData?: MyEventData[keyof MyEventData]): void => {
expectAssignable<string>(type);
expectAssignable<string>(debugName);
expectAssignable<string | undefined>(eventName);
expectAssignable<MyEventData[keyof MyEventData]>(eventData);
};
const debugOptions = {name: 'test', enabled: true, logger: myLogger};
// Global debug flag
expectAssignable<boolean>(Emittery.isDebugEnabled);
// General debug options
expectAssignable<typeof ee.debug>(debugOptions);
expectAssignable<string>(ee.debug.name);
expectAssignable<boolean | undefined>(ee.debug.enabled);
// Debug logger
expectNotAssignable<() => undefined>(ee.debug.logger);
expectNotAssignable<(data: unknown) => undefined>(ee.debug.logger);
expectNotAssignable<(type: string, debugName: string) => undefined>(ee.debug.logger);
expectNotAssignable<((type: string, debugName: string, eventName?: string, eventData?: Record<string, any>) => void) | undefined>(ee.debug.logger);
expectAssignable<typeof ee.debug.logger>(myLogger);
}
// Strict typing for emission
{
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
}>();
ee.emit('open');
ee.emit('close');
ee.emit('value', 'test');
expectError(ee.emit('value'));
expectError(ee.emit('open', 'test'));
}
// Strict typing for listeners
{
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
other: number;
}>();
ee.on('open', () => {});
ee.on('open', argument => {
expectType<undefined>(argument);
});
ee.on('value', () => {});
ee.on('value', argument => {
expectType<string>(argument);
});
ee.on(['value', 'other'], argument => {
expectType<string | number>(argument);
});
const listener = (value: string) => undefined;
ee.on('value', listener);
ee.off('value', listener);
const test = async () => {
const event = await ee.once('value');
expectType<string>(event);
const multiEvent = await ee.once(['value', 'other']);
expectType<string | number>(multiEvent);
};
expectError(ee.on('value', (value: number) => {}));
}
// Async listeners
{
const ee = new Emittery<{
open: undefined;
close: string;
}>();
ee.on('open', () => {});
ee.on('open', async () => {});
ee.on('open', async () => {});
ee.on('close', async value => {
expectType<string>(value);
});
}
// Strict typing for onAny, offAny listeners
{
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
other: number;
}>();
ee.onAny((name, data) => {
expectType<'value' | 'open' | 'close' | 'other'>(name);
expectType<string | number | undefined>(data);
});
const listener = (name: string) => {};
ee.onAny(listener);
ee.offAny(listener);
}
// Strict typing for onAny, offAny listeners for an Emittery that only has listeners with arguments
{
const ee = new Emittery<{
value: string;
other: number;
}>();
ee.onAny((name, data) => {
expectType<'value' | 'other'>(name);
expectType<string | number>(data);
});
}
// Strict typing for anyEvent iterator
{
const testAnyEvent = async () => {
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
}>();
for await (const event of ee.anyEvent()) {
expectType<'value' | 'open' | 'close'>(event[0]);
expectType<string | undefined>(event[1]);
}
const ee2 = new Emittery<{
value: string;
other: number;
}>();
for await (const event of ee2.anyEvent()) {
expectType<'value' | 'other'>(event[0]);
expectType<string | number>(event[1]);
}
};
}
// Strict typing for `.events` iterator
{
const testEventsIterator = async () => {
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
other: number;
}>();
for await (const event of ee.events('value')) {
expectType<string>(event);
}
for await (const event of ee.events(['value', 'other'])) {
expectType<string | number>(event);
}
for await (const event of ee.events(['value', 'open'])) {
expectType<string | undefined>(event);
}
const ee2 = new Emittery();
for await (const event of ee2.events('unknown')) {
expectType<any>(event);
}
};
}
// Compatibility with p-event, without explicit types
{
const ee = new Emittery();
pEventIterator(ee, 'data', {
resolutionEvents: ['finish'],
});
}
// Compatibility with p-event, with explicit types
{
type EventData = {
data: unknown;
error: unknown;
finish: undefined;
};
const ee = new Emittery<EventData>();
pEventIterator<keyof EventData, unknown>(ee, 'data', {
resolutionEvents: ['finish'],
});
}
// Mixin type
Emittery.mixin('emittery')(class {
test() {}
});
// Mixin type - arguments in constructor
Emittery.mixin('emittery')(class { // eslint-disable-line @typescript-eslint/no-extraneous-class
constructor(argument: string) {} // eslint-disable-line @typescript-eslint/no-useless-constructor
});