forked from sindresorhus/emittery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
238 lines (207 loc) · 5.13 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
/* eslint-disable @typescript-eslint/no-empty-function */
import {expectType, expectError} from 'tsd';
import pEvent = require('p-event');
import Emittery = require('.');
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 () => Promise.resolve());
ee.on('anEvent', data => undefined);
ee.on('anEvent', async data => Promise.resolve());
ee.on(Emittery.listenerAdded, ({eventName, listener}) => {
expectType<string | symbol | undefined>(eventName);
expectType<AnyListener>(listener);
});
ee.on(Emittery.listenerRemoved, ({eventName, listener}) => {
expectType<string | symbol | undefined>(eventName);
expectType<AnyListener>(listener);
});
}
// Off
{
const ee = new Emittery();
ee.off('anEvent', () => undefined);
ee.off('anEvent', async () => Promise.resolve());
ee.off('anEvent', data => undefined);
ee.off('anEvent', async data => Promise.resolve());
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<string | symbol | undefined>(eventName);
expectType<AnyListener>(listener);
});
await ee.once(Emittery.listenerRemoved).then(({eventName, listener}) => {
expectType<string | symbol | undefined>(eventName);
expectType<AnyListener>(listener);
});
};
}
{
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));
}
// Userland can't emit the meta events
{
const ee = new Emittery();
expectError(ee.emit(Emittery.listenerRemoved));
expectError(ee.emit(Emittery.listenerAdded));
}
// 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;
}>();
ee.on('open', () => {});
ee.on('open', argument => {
expectType<undefined>(argument);
});
ee.on('value', () => {});
ee.on('value', argument => {
expectType<string>(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);
};
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 () => Promise.resolve());
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;
}>();
for await (const event of ee.events('value')) {
expectType<string>(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();
pEvent.iterator(ee, 'data', {
resolutionEvents: ['finish']
});
}
// Compatibility with p-event, with explicit types
{
const ee = new Emittery<{
data: unknown;
error: unknown;
finish: undefined;
}>();
pEvent.iterator(ee, 'data', {
resolutionEvents: ['finish']
});
}
// Mixin type
Emittery.mixin('emittery')(class {
test() {}
});