-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEvents.ts
60 lines (46 loc) · 1.52 KB
/
Events.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
type Callback = (event: Event) => unknown;
class Event extends CustomEvent<unknown> {
public data?: unknown;
}
class EventEmitter
{
private readonly target = new EventTarget();
private readonly events: Map<string, Event> = new Map();
private readonly callbacks: Map<string, Callback[]> = new Map();
public add (name: string, callback: Callback): void {
const callbacks = this.callbacks.get(name);
callbacks
? callbacks.push(callback)
: this.callbacks.set(name, [callback]);
this.events.set(name, new Event(name));
this.target.addEventListener(name, callback as EventListener, false);
}
public dispatch (name: string, data?: unknown): void {
const event = this.events.get(name);
if (!event) return;
event.data = data;
this.target.dispatchEvent(event);
}
public remove (name: string, callback?: Callback): void {
const callbacks = this.callbacks.get(name);
if (callbacks && callback) {
const index = callbacks.indexOf(callback);
const listener = callback as EventListener;
index !== -1 && callbacks.splice(index, 1.0);
this.target.removeEventListener(name, listener, false);
}
if (!callback) {
callbacks?.forEach(callback =>
this.target.removeEventListener(name, callback as EventListener, false)
);
this.callbacks.delete(name);
this.events.delete(name);
}
}
public dispose (): void {
this.callbacks.clear();
this.events.clear();
}
}
const Emitter = new EventEmitter();
export { Event, Emitter };