generated from sapphiredev/sapphire-template
/
MockEmitter.ts
43 lines (34 loc) · 1.08 KB
/
MockEmitter.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
import { EventEmitter } from 'node:events';
import { EventIterator, EventIteratorOptions } from '../../src';
import { Person } from './Person';
export class PeopleIterator extends EventIterator<[Person]> {}
export const people = [new Person('Anna'), new Person('Bob'), new Person('Joe')];
export class PeopleEmitter extends EventEmitter {
#people = people;
#emitted = 0;
#timeout: NodeJS.Timeout | null = null;
#iterator: PeopleIterator | null = null;
public init(): void {
this.#timeout = setInterval((): void => {
if (this.#emitted === this.#people.length) {
clearInterval(this.#timeout!);
this.#timeout = null;
this.#iterator?.end();
this.#iterator = null;
} else {
this.emit('testEvent', this.#people[this.#emitted++]);
}
}, 1000);
}
public createPeopleIterator(options?: EventIteratorOptions<[Person]>): PeopleIterator {
this.#iterator = new PeopleIterator(this, 'testEvent', options);
this.init();
return this.#iterator;
}
public destroy(): void {
if (this.#timeout) {
this.#timeout.unref();
clearTimeout(this.#timeout);
}
}
}