-
Notifications
You must be signed in to change notification settings - Fork 1
/
KiwiEmitter.js
43 lines (36 loc) · 1002 Bytes
/
KiwiEmitter.js
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
class KiwiEmitter {
constructor() {
/**
* @protected
*/
this.events = {};
};
/**
* @param {string} name
* @param {Function} callback
*/
on(name, callback) {
if (!this.events[ name ]) this.events[ name ] = [];
this.events[ name ].push(callback);
};
emit(name, ...args) {
const listeners = this.events[ name ];
if (!listeners) return;
for (let index = 0; index < listeners.length; index++) {
const listener = listeners[ index ];
if (!listener) break;
listener(...args);
};
};
off(name, listener) {
const listeners = this.events[ name ];
if (!listeners) return;
for (let index = 0; index < listeners.length; index++) {
const index = listeners.indexOf(listener);
if (index !== -1) listeners.splice(index, 1);
};
};
};
const emitter = new KiwiEmitter();
emitter.on("ready", (a) => console.log(a));
emitter.emit("ready", 0, 1, 2, 3, 4, 5, 6);