-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.test-d.ts
143 lines (125 loc) · 4 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
import process from 'node:process';
import {EventEmitter} from 'node:events';
import fs from 'node:fs';
import {expectType} from 'tsd';
import {pEvent, pEventMultiple, pEventIterator, type CancelablePromise} from './index.js';
class NodeEmitter extends EventEmitter {
on(_event: 'finish', _listener: (number: number, string: string) => void) {
return this;
}
addListener(
_event: 'finish',
_listener: (number: number, string: string) => void,
) {
return this;
}
addEventListener(
_event: 'finish',
_listener: (number: number, string: string) => void,
) {
return this;
}
off(_event: 'finish', _listener: (number: number, string: string) => void) {
return this;
}
removeListener(
_event: 'finish',
_listener: (number: number, string: string) => void,
) {
return this;
}
removeEventListener(
_event: 'finish',
_listener: (number: number, string: string) => void,
) {
return this;
}
}
class DomEmitter implements EventTarget {
addEventListener(
_type: 'foo',
_listener: EventListenerOrEventListenerObject,
_options?: boolean | AddEventListenerOptions,
): void {} // eslint-disable-line @typescript-eslint/no-empty-function
dispatchEvent(_event: Event): boolean {
return false;
}
removeEventListener(
_type: 'foo',
_listener: EventListenerOrEventListenerObject,
_options?: boolean | AddEventListenerOptions,
): void {} // eslint-disable-line @typescript-eslint/no-empty-function
}
expectType<CancelablePromise<number>>(pEvent(new NodeEmitter(), 'finish'));
expectType<CancelablePromise<number>>(
pEvent(new NodeEmitter(), '🦄', value => value > 3),
);
expectType<CancelablePromise<Event>>(pEvent(new DomEmitter(), 'finish'));
expectType<CancelablePromise<Event>>(pEvent(document, 'DOMContentLoaded'));
expectType<CancelablePromise<number>>(
pEvent(new NodeEmitter(), 'finish', {rejectionEvents: ['error']}),
);
expectType<CancelablePromise<number>>(
pEvent(new NodeEmitter(), 'finish', {timeout: 1}),
);
expectType<CancelablePromise<number>>(
pEvent(new NodeEmitter(), 'finish', {filter: value => value > 3}),
);
expectType<CancelablePromise<[number, string]>>(
pEvent(new NodeEmitter(), 'finish', {multiArgs: true}),
);
void pEvent(new NodeEmitter(), 'finish', {
multiArgs: true,
filter: ([_, string]: [number, string]) => string === '🦄',
});
pEvent(new NodeEmitter(), 'finish').cancel();
expectType<CancelablePromise<number[]>>(
pEventMultiple(new NodeEmitter(), 'hello', {count: Number.POSITIVE_INFINITY}),
);
expectType<CancelablePromise<number[]>>(
pEventMultiple(new NodeEmitter(), 'hello', {
resolveImmediately: true,
count: Number.POSITIVE_INFINITY,
}),
);
expectType<CancelablePromise<Array<[number, string]>>>(
pEventMultiple(new NodeEmitter(), 'hello', {
count: Number.POSITIVE_INFINITY,
multiArgs: true,
}),
);
void pEventMultiple(new NodeEmitter(), 'finish', {
count: Number.POSITIVE_INFINITY,
multiArgs: true,
filter: ([_, string]: [number, string]) => string === '🦄',
});
expectType<AsyncIterableIterator<number>>(
pEventIterator(new NodeEmitter(), 'finish'),
);
expectType<AsyncIterableIterator<number>>(
pEventIterator(new NodeEmitter(), '🦄', value => value > 3),
);
expectType<AsyncIterableIterator<number>>(
pEventIterator(new NodeEmitter(), 'finish', {limit: 1}),
);
expectType<AsyncIterableIterator<number>>(
pEventIterator(new NodeEmitter(), 'finish', {resolutionEvents: ['finish']}),
);
expectType<AsyncIterableIterator<[number, string]>>(
pEventIterator(new NodeEmitter(), 'finish', {multiArgs: true}),
);
void pEventIterator(new NodeEmitter(), 'finish', {
multiArgs: true,
filter: ([_, string]: [number, string]) => string === '🦄',
});
async function getOpenReadStream(file: string): Promise<NodeJS.ReadableStream> {
const stream = fs.createReadStream(file) as NodeJS.ReadableStream;
await pEvent(stream, 'open');
return stream;
}
const stream = await getOpenReadStream('unicorn.txt');
stream.pipe(process.stdout);
const result = await pEvent(new NodeEmitter(), 'finish');
if (result === 1) {
throw new Error('Emitter finished with an error');
}