forked from nxtedition/emitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent-e3.js
151 lines (132 loc) · 3.41 KB
/
event-e3.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Use it as a constructor
* or as a decorator for an existing object
* or as a base class for extend
* cannot be used as a mixin for a constructor's prototype
* without calling the constructor
*/
function EventEmitter3(obj) {
(obj || this)._callbacks = Object.create(null);
if (obj) {return Object.assign(obj, EventEmitter3.prototype);}
}
/**
* Listen on the given `eventName` with `fn`
*
* @param {String | Symbol} eventName
* @param {Function} fn
* @api public
*/
EventEmitter3.prototype.on = function (eventName, fn) {
(this._callbacks[eventName] = this._callbacks[eventName] || [])
.push(fn);
};
/**
* Adds an `eventName` listener that will be invoked once then removed
*
* @param {String | Symbol} eventName
* @param {Function} fn
* @api public
*/
EventEmitter3.prototype.once = function (eventName, fn) {
const once = (data) => {
this.off(eventName, once);
fn(data);
};
once.fn = fn; // makes it possible to remove with off
this.on(eventName, once);
};
/**
* Remove a callback for `eventName` or
* all callbacks for `eventName` or
* all callbacks for all events
*
* @param {String | Symbol} eventName
* @param {Function} fn
* @api public
*/
EventEmitter3.prototype.off = function (eventName, fn) {
// all
if (!eventName) {
this._callbacks = Object.create(null);
return;
}
// specific event
const callbacks = this._callbacks[eventName];
if (!callbacks) {
return;
}
// remove all handlers
if (!fn) {
delete this._callbacks[eventName];
return;
}
// remove specific handler
const index = callbacks.findIndex(function (cb) {
return (cb === fn || cb.fn === fn);
});
if (index > -1) {
// Remove event specific arrays for the eventName type that no
// one is subscribed for, to avoid memory leak.
if (callbacks.length === 1) {
delete this._callbacks[eventName];
} else {
callbacks.splice(index, 1);
}
}
};
/**
* Emit `eventName` with data
*
* @param {String | Symbol} eventName
* @param {any} data
*/
EventEmitter3.prototype.emit = function (eventName, data) {
const callbacks = this._callbacks[eventName];
if (!callbacks) {
return;
}
const frozenCallbacks = Array.from(callbacks);
frozenCallbacks.forEach(callback => {
callback(data);
});
};
/**
* Return array of callbacks for `eventName`
*
* @param {String | Symbol} eventName
* @return {Array} listeners
* @api public
*/
EventEmitter3.prototype.listeners = function (eventName) {
return this._callbacks[eventName] || [];
};
/**
* True if this emitter has `eventName` handlers
*
* @param {String | Symbol} eventName
* @return {Boolean}
* @api public
*/
EventEmitter3.prototype.hasListeners = function (eventName) {
return Boolean(this.listeners(eventName).length);
};
/**
* Returns an array of event names for which the emitter has registered listeners
*
* @return {Array <String || Symbol>}
* @api public
*/
EventEmitter3.prototype.eventNames = function () {
return Reflect.ownKeys(this._callbacks);
};
/**
* Returns an array of event anmes of type string
* for which the emitter has registered listeners
*
* @return {Array <String>}
* @api public
*/
EventEmitter3.prototype.eventNamesStrings = function () {
return Object.keys(this._callbacks);
};
export default EventEmitter3;