-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventEmitter.test.ts
395 lines (266 loc) · 8.53 KB
/
EventEmitter.test.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { EventEmitter } from "./EventEmitter.ts";
import { assert, assertRejects, assertStrictEquals, fail } from "@std/assert";
const waitForTimeout = (
fn: (args?: unknown[]) => void | Promise<void>,
timeout: number,
// deno-lint-ignore no-explicit-any
...args: any[]
): Promise<void> => {
return new Promise((resolve) => {
const timeoutId = setTimeout(
async (args) => {
await fn(args);
clearTimeout(timeoutId);
resolve();
},
timeout,
...args,
);
});
};
Deno.test("EventEmitter", async (ctx) => {
type ReservedEvents = {
reserved: string;
reserved2: [string];
};
type UserEvents = {
ping: string;
pong: { data: string };
};
class Implementing extends EventEmitter<UserEvents, ReservedEvents> {
constructor() {
super();
this.run();
}
protected async run() {
await waitForTimeout(() => {
this.emitReserved("reserved", "reserved");
this.emitReserved("reserved2", ["reserved2"]);
}, 10);
}
}
await ctx.step("reserved events", async (ctx) => {
await ctx.step("listening is possible", async () => {
const instance = new Implementing();
await instance.pull("reserved").then((reserved) => {
assertStrictEquals(reserved, "reserved");
});
});
});
});
type Events = {
foo: "bar";
bar: "bar";
baz: undefined;
pong: "bar";
adjustCount: "increment" | "decrement";
};
Deno.test("EventEmitter base functions", async (ctx) => {
/**
* @see https://deno.land/x/[email protected]/test.ts
*/
await ctx.step("on", async (ctx) => {
await ctx.step("one event", () => {
const target = new EventEmitter<Events>();
target.on("foo", (detail) => {
assertStrictEquals(detail, "bar");
});
target.emit("foo", "bar");
});
await ctx.step("multiple events", () => {
const target = new EventEmitter<Events>();
target.on(["foo", "bar"], (detail) => {
assertStrictEquals(detail, "bar");
});
target.emit("foo", "bar");
target.emit("bar", "bar");
});
});
await ctx.step("once", () => {
const target = new EventEmitter<Events>();
target.once("pong", (detail) => {
assertStrictEquals(detail, "bar");
});
target.emit("pong", "bar");
});
await ctx.step("off", async (ctx) => {
await ctx.step("exact off", () => {
const target = new EventEmitter<Events>();
function foo() {
fail();
}
target.on("foo", foo).on("bar", foo).on("baz", foo);
target.off("foo", foo);
target.off(["bar", "baz"], foo);
target.emit("foo", "bar");
});
await ctx.step("offEvent", () => {
const target = new EventEmitter<Events>();
let i = 0;
target.on("foo", () => i--);
target.on("foo", () => i--);
target.on("baz", () => (i = i + 2));
target.on("bar", () => i++);
target.off("foo");
target.emit("foo", "bar");
assertStrictEquals(i, 0);
target.emit("bar", "bar");
assertStrictEquals(i, 1);
});
await ctx.step("offEvent", () => {
const target = new EventEmitter<Events>();
let i = 0;
target.on("foo", () => i--);
target.on("bar", () => i++);
target.emit("foo", "bar");
assertStrictEquals(i, -1);
target.off(["foo", "bar"]);
target.emit("bar", "bar");
assertStrictEquals(i, -1);
});
await ctx.step("offAll", () => {
const target = new EventEmitter<Events>();
let i = 0;
target.on("foo", () => i++);
target.on("bar", () => i++);
target.off();
target.emit("foo", "bar").emit("bar", "bar");
assertStrictEquals(i, 0);
});
});
await ctx.step("chainable", () => {
const target = new EventEmitter<Events>();
function foo() {
fail();
}
function bar(detail: string) {
assertStrictEquals(detail, "bar");
}
function barEvent(detail: CustomEvent) {
assertStrictEquals(detail.detail, "bar");
}
target.on("foo", foo).off("foo", foo);
target.emit("foo", "bar");
target
.once("foo", bar)
.addEventListener("foo", barEvent)
.emit("foo", "bar");
});
await ctx.step("pull", async (ctx) => {
await ctx.step("without timeout", async () => {
const target = new EventEmitter<Events>();
const promise = target.pull("foo");
await waitForTimeout(async () => {
target.emit("foo", "bar");
const detail = await promise;
assertStrictEquals(detail, "bar");
}, 10);
});
await ctx.step("with timeout", async (ctx) => {
await ctx.step("should resolve", async () => {
const target = new EventEmitter<Events>();
const promise = target.pull("foo", 20);
await waitForTimeout(async () => {
target.emit("foo", "bar");
const detail = await promise;
assertStrictEquals(detail, "bar");
}, 10);
});
await ctx.step("should reject", async () => {
const target = new EventEmitter<Events>();
await assertRejects(() => target.pull("foo", 10));
});
});
});
await ctx.step("getListeners", async (ctx) => {
await ctx.step("all listeners", () => {
const target = new EventEmitter<Events>();
const callback = () => {};
const callback2 = () => {};
target.once(["foo", "bar"], callback);
target.addEventListener("foo", callback2);
const listeners = target.getListeners();
assertStrictEquals(listeners.size, 2);
assert(listeners.get("foo")?.has(callback));
assert(listeners.get("foo")?.has(callback2));
assert(listeners.get("bar")?.has(callback));
});
await ctx.step("listeners of type", () => {
const target = new EventEmitter<Events>();
const callback = () => {};
const callback2 = () => {};
target.on("foo", callback);
target.addEventListener("foo", callback2, { once: true });
assertStrictEquals(target.getListeners("foo").size, 2);
assert(target.getListeners("foo").has(callback));
});
await ctx.step("listeners are removed", () => {
const target = new EventEmitter<Events>();
const callback = () => {},
callback2 = () => {},
callback3 = () => {},
callback4 = () => {};
target
.once("foo", callback)
.on("foo", callback2)
.addEventListener("foo", callback3, {
once: true,
})
.addEventListener("foo", callback4);
assertStrictEquals(target.getListeners("foo").size, 4);
target.emit("foo", "bar");
assertStrictEquals(target.getListeners("foo").size, 2);
target.off("foo", callback2);
assertStrictEquals(target.getListeners("foo").size, 1);
target.removeEventListener("foo", callback4);
assertStrictEquals(target.getListeners("foo").size, 0);
});
});
await ctx.step("dispatches events", () => {
const target = new EventEmitter<Events>();
let count = 0;
target.addEventListener("adjustCount", ({ detail }) => {
count += detail === "increment" ? 1 : -1;
});
const incrementEvent = EventEmitter.createEvent(
"adjustCount",
"increment" as const,
);
target.dispatchEvent(incrementEvent);
target.dispatchEvent(incrementEvent);
target.dispatch("adjustCount", "decrement");
target.dispatch("adjustCount", "decrement");
target.dispatch("adjustCount", "increment");
assertStrictEquals(count, 1);
});
await ctx.step('correctly implements "once" option', () => {
const target = new EventEmitter<Events>();
let count = 0;
const cb = (ev: CustomEvent) => {
count += ev.detail === "increment" ? 1 : -1;
};
target.addEventListener("adjustCount", cb, { once: true });
const incrementEvent = EventEmitter.createEvent(
"adjustCount",
"increment" as const,
);
assertStrictEquals(count, 0);
target.dispatchEvent(incrementEvent);
assertStrictEquals(count, 1);
target.dispatchEvent(incrementEvent);
assertStrictEquals(count, 1);
});
await ctx.step('"subscribe" method returns "unsubscribe" function', () => {
const target = new EventEmitter<Events>();
let count = 0;
const unsubscribe = target.subscribe("adjustCount", (payload) => {
count += payload === "increment" ? 1 : -1;
unsubscribe();
});
assertStrictEquals(count, 0);
target.publish("adjustCount", "increment");
assertStrictEquals(count, 1);
target.publish("adjustCount", "increment");
assertStrictEquals(count, 1);
});
});