/
EventEmitter.ts
60 lines (53 loc) · 1.62 KB
/
EventEmitter.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
import { IEventEmitter } from "./interfaces/IEventEmitter";
export class EventEmitter implements IEventEmitter {
private _events: { [type: string]: ((event: any) => void)[]; };
constructor() { this._events = {}; }
/**
* @ignore
*/
public fire<T>(
type: string,
event: T): void {
if (!this._listens(type)) { return; }
for (const handler of this._events[type]) {
handler(event);
}
}
/**
* Unsubscribe from an event by its name.
* @param {string} type - The name of the event
* to unsubscribe from.
* @param {(event: T) => void} handler - The
* handler to remove.
*/
public off<T>(
type: string,
handler: (event: T) => void): void {
if (!type) { this._events = {}; return; }
if (this._listens(type)) {
const index = this._events[type].indexOf(handler);
if (index >= 0) {
this._events[type].splice(index, 1);
}
if (!this._events[type].length) {
delete this._events[type];
}
}
}
/**
* Subscribe to an event by its name.
* @param {string} type - The name of the event
* to subscribe to.
* @param {(event: T) => void} handler - The
* handler called when the event occurs.
*/
public on<T>(
type: string,
handler: (event: T) => void): void {
this._events[type] = this._events[type] || [];
this._events[type].push(handler);
}
private _listens(eventType: string): boolean {
return eventType in this._events;
}
}