@supercat1337/event-emitter 1.0.12 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/test.js DELETED
@@ -1,508 +0,0 @@
1
- //@ts-check
2
-
3
- //import test from "ava";
4
- import { EventEmitter } from "./../src/index.js";
5
- import test from "./../node_modules/ava/entrypoints/main.mjs";
6
-
7
- test("on(), emit()", (t) => {
8
- /** @type {EventEmitter<"foo"|"bar">} */
9
- let ev = new EventEmitter();
10
- ev.on("foo", () => {
11
- t.pass();
12
- });
13
-
14
- ev.emit("bar");
15
- ev.emit("foo");
16
- });
17
-
18
- test("once(), emit()", (t) => {
19
- /** @type {EventEmitter<"foo">} */
20
- let ev = new EventEmitter();
21
- let foo = 0;
22
- ev.once("foo", () => {
23
- foo++;
24
- });
25
-
26
- ev.emit("foo");
27
- ev.emit("foo");
28
- ev.emit("foo");
29
-
30
- if (foo == 1) {
31
- t.pass();
32
- } else {
33
- t.fail();
34
- }
35
- });
36
-
37
- test("off(), removeListener()", (t) => {
38
- /** @type {EventEmitter<"foo">} */
39
- let ev = new EventEmitter();
40
- let foo = 0;
41
- let action = () => {
42
- foo++;
43
- };
44
-
45
- ev.on("foo", action);
46
- ev.off("foo", action);
47
-
48
- ev.emit("foo");
49
- ev.emit("foo");
50
-
51
- t.notThrows(() => {
52
- // This should not throw
53
- // @ts-ignore
54
- ev.removeListener("foo1", action);
55
- });
56
- ev.destroy();
57
- t.notThrows(() => {
58
- ev.off("foo", action);
59
- });
60
-
61
- if (foo == 0) {
62
- t.pass();
63
- } else {
64
- t.fail();
65
- }
66
- });
67
-
68
- test("Call unsubscriber", (t) => {
69
- /** @type {EventEmitter<"foo">} */
70
- let ev = new EventEmitter();
71
- let foo = 0;
72
- let action = () => {
73
- foo++;
74
- };
75
-
76
- let unsubscriber = ev.on("foo", action);
77
-
78
- ev.emit("foo");
79
- unsubscriber();
80
-
81
- ev.emit("foo");
82
-
83
- if (foo == 1) {
84
- t.pass();
85
- } else {
86
- t.fail();
87
- }
88
- });
89
-
90
- test("on(), emit() with error", (t) => {
91
- /** @type {EventEmitter<"foo">} */
92
- let ev = new EventEmitter();
93
- let foo = 0;
94
-
95
- /**
96
- *
97
- * @param {number} bar
98
- */
99
- function func(bar) {
100
- if (bar % 2) {
101
- throw new Error("Custom error");
102
- } else {
103
- foo++;
104
- }
105
- }
106
-
107
- ev.on("foo", () => {
108
- func(foo);
109
- });
110
-
111
- ev.emit("foo");
112
- ev.emit("foo");
113
- ev.emit("foo");
114
-
115
- if (foo == 1) {
116
- t.pass();
117
- } else {
118
- t.fail();
119
- }
120
- });
121
-
122
- test("waitForEvent()", async (t) => {
123
- /** @type {EventEmitter<"foo">} */
124
- let ev = new EventEmitter();
125
- let foo = 0;
126
- let action = () => {
127
- foo++;
128
- };
129
-
130
- ev.on("foo", action);
131
-
132
- setTimeout(() => {
133
- ev.emit("foo");
134
- }, 10);
135
-
136
- await ev.waitForEvent("foo");
137
- if (foo == 1) {
138
- t.pass();
139
- } else {
140
- t.fail();
141
- }
142
- });
143
-
144
- test("waitForEvent() with timeout and no event", async (t) => {
145
- /** @type {EventEmitter<"foo">} */
146
- let ev = new EventEmitter();
147
- let foo = 0;
148
- let action = () => {
149
- foo++;
150
- };
151
-
152
- ev.on("foo", action);
153
-
154
- setTimeout(() => {
155
- ev.emit("foo");
156
- }, 200);
157
-
158
- await ev.waitForEvent("foo", 50);
159
- if (foo == 0) {
160
- t.pass();
161
- } else {
162
- t.fail();
163
- }
164
- });
165
-
166
- test("waitForEvent() with timeout", async (t) => {
167
- /** @type {EventEmitter<"foo">} */
168
- let ev = new EventEmitter();
169
- let foo = 0;
170
- let action = () => {
171
- foo++;
172
- };
173
-
174
- ev.on("foo", action);
175
-
176
- setTimeout(() => {
177
- ev.emit("foo");
178
- }, 50);
179
-
180
- await ev.waitForEvent("foo", 200);
181
- if (foo == 1) {
182
- t.pass();
183
- } else {
184
- t.fail();
185
- }
186
- });
187
-
188
- test("waitForAnyEvent()", async (t) => {
189
- /** @type {EventEmitter<"foo"|"bar">} */
190
- let ev = new EventEmitter();
191
- let foo = 0;
192
- let bar = 0;
193
-
194
- ev.on("foo", () => {
195
- foo++;
196
- });
197
-
198
- ev.on("bar", () => {
199
- bar++;
200
- });
201
-
202
- setTimeout(() => {
203
- ev.emit("foo");
204
- }, 10);
205
-
206
- await ev.waitForAnyEvent(["foo", "bar"]);
207
-
208
- setTimeout(() => {
209
- ev.emit("bar");
210
- }, 10);
211
-
212
- await ev.waitForAnyEvent(["foo", "bar"]);
213
-
214
- if (foo == 1 && bar == 1) {
215
- t.pass();
216
- } else {
217
- t.fail();
218
- }
219
- });
220
-
221
- test("waitForAnyEvent() with timeout and no event", async (t) => {
222
- /** @type {EventEmitter<"foo"|"bar">} */
223
- let ev = new EventEmitter();
224
- let foo = 0;
225
- let bar = 0;
226
-
227
- ev.on("foo", () => {
228
- foo++;
229
- });
230
-
231
- ev.on("bar", () => {
232
- bar++;
233
- });
234
-
235
- setTimeout(() => {
236
- ev.emit("foo");
237
- }, 50);
238
-
239
- setTimeout(() => {
240
- ev.emit("bar");
241
- }, 300);
242
-
243
- await ev.waitForAnyEvent(["foo", "bar"], 100);
244
-
245
- if (foo == 1 && bar == 0) {
246
- t.pass();
247
- } else {
248
- t.fail();
249
- }
250
- });
251
-
252
- test("waitForAnyEvent() with timeout", async (t) => {
253
- /** @type {EventEmitter<"foo"|"bar">} */
254
- let ev = new EventEmitter();
255
- let foo = 0;
256
- let bar = 0;
257
-
258
- ev.on("foo", () => {
259
- foo++;
260
- });
261
-
262
- ev.on("bar", () => {
263
- bar++;
264
- });
265
-
266
- setTimeout(() => {
267
- ev.emit("foo");
268
- }, 200);
269
-
270
- await ev.waitForAnyEvent(["foo", "bar"], 50);
271
-
272
- if (foo == 0 && bar == 0) {
273
- t.pass();
274
- } else {
275
- t.fail();
276
- }
277
- });
278
-
279
- test("clearEventListeners()", (t) => {
280
- /** @type {EventEmitter<"foo"|"bar">} */
281
- let ev = new EventEmitter();
282
- let foo = 0;
283
-
284
- ev.on("foo", () => {
285
- foo++;
286
- });
287
-
288
- ev.emit("foo");
289
-
290
- ev.clearEventListeners("foo");
291
- ev.clearEventListeners("bar");
292
-
293
- ev.emit("foo");
294
- ev.emit("foo");
295
- ev.emit("foo");
296
-
297
- t.is(foo, 1);
298
- });
299
-
300
- test("destroy()", (t) => {
301
- /** @type {EventEmitter<"foo">} */
302
- let ev = new EventEmitter();
303
- let foo = 0;
304
-
305
- ev.on("foo", () => {
306
- foo++;
307
- });
308
-
309
- ev.onHasEventListeners(() => {
310
- foo++;
311
- });
312
-
313
- ev.onNoEventListeners(() => {
314
- foo++;
315
- });
316
-
317
- ev.destroy();
318
-
319
- t.throws(() => {
320
- ev.on("foo", () => {});
321
- });
322
-
323
- ev.emit("foo");
324
- ev.emit("foo");
325
- ev.emit("foo");
326
-
327
- t.is(foo, 0);
328
- });
329
-
330
- test("isDestroyed", (t) => {
331
- /** @type {EventEmitter<"foo">} */
332
- let ev = new EventEmitter();
333
- t.is(ev.isDestroyed, false);
334
- ev.destroy();
335
- t.is(ev.isDestroyed, true);
336
- });
337
-
338
- test("onHasEventListeners(), onNoEventListeners()", (t) => {
339
- /** @type {EventEmitter<"foo">} */
340
- let ev = new EventEmitter();
341
- let foo = 0;
342
- let bar = 0;
343
-
344
- // Test that onHasEventListeners() and onNoEventListeners() are called when appropriate
345
- let unsubscriberOnHasEventListeners = ev.onHasEventListeners(() => {
346
- // Increment when onHasEventListeners() is called
347
- bar++;
348
- });
349
- let unsubscriberOnNoEventListeners = ev.onNoEventListeners(() => {
350
- // Decrement when onNoEventListeners() is called
351
- bar--;
352
- });
353
-
354
- t.is(bar, 0);
355
- t.is(foo, 0);
356
-
357
- // Subscribe to the event and emit it
358
- let unsubscriber = ev.on("foo", () => {
359
- foo++;
360
- });
361
- ev.emit("foo");
362
- t.is(bar, 1);
363
- t.is(foo, 1);
364
-
365
- // Unsubscribe and emit the event again
366
- unsubscriber();
367
- t.is(bar, 0);
368
- t.is(foo, 1);
369
-
370
- // Subscribe again and emit the event again
371
- let unsubscriber2 = ev.on("foo", () => {
372
- foo++;
373
- });
374
- ev.emit("foo");
375
- t.is(bar, 1);
376
- t.is(foo, 2);
377
-
378
- // Unsubscribe and emit the event again
379
- unsubscriberOnNoEventListeners();
380
- unsubscriber2();
381
- t.is(bar, 1);
382
- t.is(foo, 2);
383
- });
384
-
385
- test("onListenerError()", (t) => {
386
- /** @type {EventEmitter<"foo">} */
387
- let ev = new EventEmitter();
388
- let foo = 0;
389
- let errorCaught = false;
390
-
391
- // Subscribe to the event and emit it
392
- let unsubscriber = ev.on("foo", () => {
393
- foo++;
394
- throw new Error("test");
395
- });
396
- ev.onListenerError(() => {
397
- errorCaught = true;
398
- });
399
- ev.emit("foo");
400
- t.is(errorCaught, true);
401
- t.is(foo, 1);
402
- });
403
-
404
- test("onListenerError() with error in onHasEventListeners(), onNoEventListeners() callbacks", (t) => {
405
- /** @type {EventEmitter<"foo">} */
406
- let ev = new EventEmitter();
407
- let foo = 0;
408
- let onHasEventListenersErrorCaught = false;
409
- let onNoEventListenersErrorCaught = false;
410
-
411
- ev.onHasEventListeners(() => {
412
- throw new Error("onHasEventListeners");
413
- });
414
- ev.onNoEventListeners(() => {
415
- throw new Error("onNoEventListenersErrorCaught");
416
- });
417
- ev.onListenerError((e) => {
418
- if (e.message === "onHasEventListeners") {
419
- onHasEventListenersErrorCaught = true;
420
- }
421
- if (e.message === "onNoEventListenersErrorCaught") {
422
- onNoEventListenersErrorCaught = true;
423
- }
424
- });
425
-
426
- ev.onListenerError(() => {
427
- throw new Error("ListenerErrorInOnListenerError");
428
- });
429
-
430
- // Subscribe to the event and emit it
431
- let unsubscriber = ev.on("foo", () => {
432
- foo++;
433
- throw new Error("test");
434
- });
435
- ev.emit("foo");
436
- t.is(foo, 1);
437
- t.is(onHasEventListenersErrorCaught, true);
438
- t.is(onNoEventListenersErrorCaught, false);
439
- unsubscriber();
440
- t.is(onNoEventListenersErrorCaught, true);
441
- });
442
-
443
- test("when destroyed", (t) => {
444
- /** @type {EventEmitter<"foo">} */
445
- let ev = new EventEmitter();
446
- ev.destroy();
447
-
448
- t.notThrows(() => {
449
- ev.emit("foo");
450
- });
451
-
452
- t.notThrows(() => {
453
- ev.destroy();
454
- });
455
-
456
- t.notThrows(() => {
457
- ev.removeListener("foo", () => {});
458
- });
459
-
460
- t.notThrows(() => {
461
- ev.clearEventListeners("foo");
462
- });
463
-
464
- t.notThrows(() => {
465
- ev.off("foo", () => {});
466
- });
467
-
468
- t.throws(() => {
469
- ev.on("foo", () => {});
470
- });
471
-
472
- t.throws(() => {
473
- ev.once("foo", () => {});
474
- });
475
-
476
- t.throws(() => {
477
- ev.onHasEventListeners(() => {});
478
- });
479
-
480
- t.throws(() => {
481
- ev.onNoEventListeners(() => {});
482
- });
483
-
484
- t.throws(() => {
485
- ev.onListenerError(() => {});
486
- });
487
-
488
- t.throws(() => {
489
- ev.waitForEvent("foo");
490
- });
491
-
492
- t.throws(() => {
493
- ev.waitForAnyEvent(["foo"]);
494
- });
495
- });
496
-
497
- test("clear", (t) => {
498
- /** @type {EventEmitter<"foo">} */
499
- let ev = new EventEmitter();
500
- let foo = 0;
501
- ev.on("foo", () => {
502
- foo++;
503
- });
504
- ev.clear();
505
- ev.emit("foo");
506
- t.is(foo, 0);
507
- t.deepEqual(ev.events, {});
508
- });