-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathevents.js
30 lines (25 loc) · 1.02 KB
/
events.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
let EventEmitter = {};
EventEmitter.on = function (name, callback) {
this._eventEmitterCallbacks = this._eventEmitterCallbacks || {};
this._eventEmitterCallbacks[name] = this._eventEmitterCallbacks[name] || [];
this._eventEmitterCallbacks[name].push(callback);
};
EventEmitter.addListener = EventEmitter.on;
EventEmitter.off = function (name, callback) {
this._eventEmitterCallbacks = this._eventEmitterCallbacks || {};
if (!(name in this._eventEmitterCallbacks)) return;
const i = this._eventEmitterCallbacks[name].indexOf(callback);
if (i < 0) return;
this._eventEmitterCallbacks[name].splice(i, 1);
};
EventEmitter.removeListener = EventEmitter.off;
EventEmitter.emit = function (name, evt) {
this._eventEmitterCallbacks = this._eventEmitterCallbacks || {};
if (!(name in this._eventEmitterCallbacks)) return;
for (let callback of this._eventEmitterCallbacks[name]) {
if (typeof callback !== "function") return;
callback(evt);
}
};
EventEmitter.trigger = EventEmitter.emit;
export default EventEmitter;