-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventemitter.js
More file actions
111 lines (98 loc) · 3.2 KB
/
eventemitter.js
File metadata and controls
111 lines (98 loc) · 3.2 KB
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
import { template } from "./utils/utils.js";
/**
* Internal function for validating event type
* @param {string} eventName
* @returns {boolean}
*/
const isValidEvent = (eventName) => typeof eventName === 'string';
const ERR_MSG_EVENT_TYPE = 'string';
const isFunction = (type) => typeof type === 'function';
const typeErrorClosure = template`Expected a ${0} but got ${1}!`;
class PubSub {
/* Not sure if it should be Set<Function> or Function[] */
/**
* @type {Map<string, Set<Function>}
*/
#handlers = new Map();
#makingPassOn = '';
#cache = [];
/**
*
* @param {string} eventName the name of the event to subscribe to.
* @param {Function} func the function to register
*/
subscribe(eventName, func) {
if (!isFunction(func)) {
throw new TypeError(typeErrorClosure('function', String(func)));
}
if (!isValidEvent(eventName)) {
throw new TypeError(typeErrorClosure(ERR_MSG_EVENT_TYPE, eventName));
}
if (this.#handlers.has(eventName)) {
if (this.#makingPassOn === eventName) {
this.#cache.push(func);
} else {
const eventHandlers = this.#handlers.get(eventName);
eventHandlers.add(func);
}
} else {
this.#handlers.set(eventName, new Set([func]));
}
}
/**
*
* @param {string} eventName the name of the event the function participates in.
* @param {*} func the original function to remove.
*/
unsubscribe(eventName, func) {
if (!isFunction(func)) {
throw new TypeError(typeErrorClosure('function', String(func)));
}
if (!isValidEvent(eventName)) {
throw new TypeError(typeErrorClosure(ERR_MSG_EVENT_TYPE, eventName));
}
if (this.#handlers.has(eventName)) {
const eventHandlers = this.#handlers.get(eventName);
eventHandlers.delete(func);
}
}
/**
*
* @param {string} eventName
*/
unsubscribeAll(eventName) {
if (!isValidEvent(eventName)) {
throw new TypeError(typeErrorClosure(ERR_MSG_EVENT_TYPE, eventName));
}
if (this.#handlers.has(eventName)) {
this.#handlers.delete(eventName);
}
}
emit(eventName, ...args) {
if (!isValidEvent(eventName)) {
throw new TypeError(typeErrorClosure(ERR_MSG_EVENT_TYPE, eventName));
}
this.#makingPassOn = eventName;
if (this.#handlers.has(eventName)) {
let funcs = this.#handlers.get(eventName);
funcs.forEach(fn => fn(...args));
}
this.#makingPassOn = '';
let funcs = this.#handlers.get(eventName)
const len = this.#cache.length;
for (let i = 0; i < len; i++) {
funcs.add(this.#cache[i]);
}
this.#cache.length = 0;
}
*eventNames() {
yield *this.#handlers.keys();
}
handlerCountOf(eventName) {
if (!isValidEvent(eventName)) {
throw new TypeError(typeErrorClosure(ERR_MSG_EVENT_TYPE, eventName));
}
return this.#handlers.get(eventName).size();
}
}
export {PubSub};