@irrelon/emitter 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -1
- package/.eslintrc.json +8 -2
- package/dist/Emitter.d.ts +205 -0
- package/dist/Emitter.js +295 -657
- package/package.json +54 -52
- package/readme.md +44 -11
- package/src/{Emitter.js → Emitter.ts} +138 -141
- package/tests/Emitter.test.js +26 -6
- package/tsconfig.json +29 -0
package/dist/Emitter.js
CHANGED
|
@@ -1,276 +1,54 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The MIT License (MIT)
|
|
3
|
-
|
|
4
|
-
Copyright (c) 2014 Irrelon Software Limited
|
|
5
|
-
http://www.irrelon.com
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
in the Software without restriction, including without limitation the rights
|
|
10
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
furnished to do so, subject to the following conditions:
|
|
13
|
-
|
|
14
|
-
The above copyright notice, url and this permission notice shall be included in
|
|
15
|
-
all copies or substantial portions of the Software.
|
|
16
|
-
|
|
17
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
-
THE SOFTWARE.
|
|
24
|
-
|
|
25
|
-
Source: https://github.com/irrelon/emitter
|
|
26
|
-
|
|
27
|
-
Changelog:
|
|
28
|
-
Version 4.0.1:
|
|
29
|
-
Updated library to use new ES6 functionality making Overload()
|
|
30
|
-
less useful so it can be removed as a dependency.
|
|
31
|
-
Version 4.0.0:
|
|
32
|
-
Breaking change. Library now has named exports `Emitter` and
|
|
33
|
-
`makeEmitter`. `Emitter` is an ES6 class that can be extended
|
|
34
|
-
and `makeEmitter` is a function that takes an object or class
|
|
35
|
-
and does what `Emitter(someObject)` used to do. This change is
|
|
36
|
-
to support being able to extend `Emitter` as a base class.
|
|
37
|
-
Version 3.1.0:
|
|
38
|
-
Changed order of execution so that listeners that are listening
|
|
39
|
-
against a specific ID get called before the general catch-all
|
|
40
|
-
listeners. Renamed package to @irrelon/emitter.
|
|
41
|
-
Version 2.0.11:
|
|
42
|
-
Added cancelStatic method to allow cancelling a static event
|
|
43
|
-
Version 2.0.7:
|
|
44
|
-
Fixed UMD module support
|
|
45
|
-
Version 2.0.6:
|
|
46
|
-
Added UMD module support
|
|
47
|
-
Version 2.0.5:
|
|
48
|
-
Added bower version number
|
|
49
|
-
Version 2.0.4:
|
|
50
|
-
Allow instantiation as independent instance, updated unit tests
|
|
51
|
-
Version 2.0.3:
|
|
52
|
-
Documentation updates, published to bower
|
|
53
|
-
Version 2.0.2:
|
|
54
|
-
Documentation updates
|
|
55
|
-
Version 2.0.1:
|
|
56
|
-
Bug fix in this._emitters usage
|
|
57
|
-
Version 2.0.0:
|
|
58
|
-
Big update to bring in line with latest developments in other projects. Event emitter can
|
|
59
|
-
now use deferEmit(), emitId(), emitStatic(), emitStaticId(), willEmit(), willEmitId().
|
|
60
|
-
Version 1.1.9:
|
|
61
|
-
Updated changelog correctly
|
|
62
|
-
Version 1.1.8:
|
|
63
|
-
Removed tons of dependencies wrongly included in main dependencies, have moved to devDependencies section of package.json
|
|
64
|
-
Version 1.1.0:
|
|
65
|
-
Added support for overloaded methods
|
|
66
|
-
Added support for events with ids
|
|
67
|
-
Version 1.0.2:
|
|
68
|
-
Removed AMD support, added browserify support
|
|
69
|
-
Added package.json
|
|
70
|
-
Added once() method
|
|
71
|
-
Added hasListener() method
|
|
72
|
-
Published to NPM as irrelon-emitter
|
|
73
|
-
Version 1.0.1:
|
|
74
|
-
Added ability to extend any object with eventing capability
|
|
75
|
-
Added AMD / Require.js support
|
|
76
|
-
Added Node.js support
|
|
77
|
-
Version 1.0.0:
|
|
78
|
-
First commit
|
|
79
|
-
*/
|
|
80
1
|
"use strict";
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
85
|
-
|
|
86
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
87
|
-
|
|
88
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
89
|
-
|
|
90
|
-
var EventMainMethods = {
|
|
91
|
-
/**
|
|
92
|
-
* Attach an event listener to the passed event only if the passed
|
|
93
|
-
* id matches the document id for the event being fired.
|
|
94
|
-
* @memberOf Emitter
|
|
95
|
-
* @method on
|
|
96
|
-
* @param {String} event The name of the event to listen for.
|
|
97
|
-
* @param {*} id The document id to match against.
|
|
98
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
99
|
-
* @returns {Emitter} The emitter instance.
|
|
100
|
-
*/
|
|
101
|
-
"_on": function _on(event, id, listener) {
|
|
102
|
-
var _this = this;
|
|
103
|
-
|
|
104
|
-
var self = this;
|
|
105
|
-
|
|
106
|
-
var generateTimeout = function generateTimeout(emitter) {
|
|
107
|
-
setTimeout(function () {
|
|
108
|
-
listener.apply(_this, emitter.args);
|
|
109
|
-
}, 1);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
this._listeners = this._listeners || {};
|
|
113
|
-
this._listeners[event] = this._listeners[event] || {};
|
|
114
|
-
this._listeners[event][id] = this._listeners[event][id] || [];
|
|
115
|
-
|
|
116
|
-
this._listeners[event][id].push(listener); // Check for any static emitters, and fire the event if any exist
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (!this._emitters || !this._emitters[event] || !this._emitters[event].length) return this; // Emit events for each emitter
|
|
120
|
-
|
|
121
|
-
for (var i = 0; i < this._emitters[event].length; i++) {
|
|
122
|
-
var emitter = this._emitters[event];
|
|
123
|
-
|
|
124
|
-
if (id === "*" || emitter.id === id) {
|
|
125
|
-
// Call the listener out of process so that any code that expects a listener
|
|
126
|
-
// to be called at some point in the future rather than immediately on registration
|
|
127
|
-
// will not fail
|
|
128
|
-
generateTimeout(emitter);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return this;
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Attach an event listener to the passed event only if the passed
|
|
137
|
-
* id matches the document id for the event being fired.
|
|
138
|
-
* @memberOf Emitter
|
|
139
|
-
* @method once
|
|
140
|
-
* @param {String} event The name of the event to listen for.
|
|
141
|
-
* @param {*} id The document id to match against.
|
|
142
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
143
|
-
* @returns {Emitter} The emitter instance.
|
|
144
|
-
*/
|
|
145
|
-
"_once": function _once(event, id, listener) {
|
|
146
|
-
var _this2 = this,
|
|
147
|
-
_arguments = arguments;
|
|
148
|
-
|
|
149
|
-
var fired = false;
|
|
150
|
-
|
|
151
|
-
var internalCallback = function internalCallback() {
|
|
152
|
-
if (fired) return;
|
|
153
|
-
fired = true;
|
|
154
|
-
|
|
155
|
-
_this2.off(event, id, internalCallback);
|
|
156
|
-
|
|
157
|
-
listener.apply(_this2, _arguments);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
return this.on(event, id, internalCallback);
|
|
161
|
-
},
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Cancels an event listener based on an event name, id and listener function.
|
|
165
|
-
* @memberOf Emitter
|
|
166
|
-
* @method off
|
|
167
|
-
* @param {String} event The event to cancel listener for.
|
|
168
|
-
* @param {String} id The ID of the event to cancel listening for.
|
|
169
|
-
* @param {Function} listener The event listener function used in the on()
|
|
170
|
-
* or once() call to cancel.
|
|
171
|
-
* @returns {Emitter} The emitter instance.
|
|
172
|
-
*/
|
|
173
|
-
"_off": function _off(event, id, listener) {
|
|
174
|
-
var _this3 = this;
|
|
175
|
-
|
|
176
|
-
if (this._emitting) {
|
|
177
|
-
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
178
|
-
|
|
179
|
-
this._eventRemovalQueue.push(function () {
|
|
180
|
-
_this3.off(event, id, listener);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
return this;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (!this._listeners || !this._listeners[event] || !this._listeners[event][id]) return this;
|
|
187
|
-
|
|
188
|
-
if (id && !listener) {
|
|
189
|
-
if (id === "*") {
|
|
190
|
-
delete this._listeners[event];
|
|
191
|
-
return this;
|
|
192
|
-
} // No listener provided, delete all listeners
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
delete this._listeners[event][id];
|
|
196
|
-
return this;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
var arr = this._listeners[event][id] || [],
|
|
200
|
-
index = arr.indexOf(listener);
|
|
201
|
-
|
|
202
|
-
if (index > -1) {
|
|
203
|
-
arr.splice(index, 1);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
};
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeEmitter = exports.Emitter = void 0;
|
|
207
4
|
/**
|
|
208
5
|
* @class Emitter
|
|
209
6
|
* @constructor
|
|
210
7
|
*/
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Attach an event listener to the passed event only if the passed
|
|
224
|
-
* id matches the document id for the event being fired.
|
|
225
|
-
* @memberOf Emitter
|
|
226
|
-
* @method on
|
|
227
|
-
* @param {String} event The name of the event to listen for.
|
|
228
|
-
* @param {*} id The document id to match against.
|
|
229
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
230
|
-
* @returns {Emitter} The emitter instance.
|
|
231
|
-
*/
|
|
232
|
-
value: function on(event) {
|
|
233
|
-
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
234
|
-
rest[_key - 1] = arguments[_key];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
var restTypes = rest.map(function (arg) {
|
|
238
|
-
return (0, _typeof2.default)(arg);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
if (restTypes[0] === "function") {
|
|
242
|
-
return EventMainMethods._on.call(this, event, "*", rest[0]);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return EventMainMethods._on.call(this, event, rest[0], rest[1]);
|
|
8
|
+
class Emitter {
|
|
9
|
+
constructor(obj) {
|
|
10
|
+
this._noEmitDefer = false;
|
|
11
|
+
this._emitting = false;
|
|
12
|
+
this._listeners = {};
|
|
13
|
+
this._emitters = {};
|
|
14
|
+
this._eventRemovalQueue = [];
|
|
15
|
+
if (obj !== undefined) {
|
|
16
|
+
return (0, exports.makeEmitter)(obj);
|
|
17
|
+
}
|
|
246
18
|
}
|
|
247
19
|
/**
|
|
248
20
|
* Attach an event listener to the passed event only if the passed
|
|
249
21
|
* id matches the document id for the event being fired.
|
|
250
22
|
* @memberOf Emitter
|
|
251
|
-
* @method
|
|
23
|
+
* @method on
|
|
252
24
|
* @param {String} event The name of the event to listen for.
|
|
253
25
|
* @param {*} id The document id to match against.
|
|
254
26
|
* @param {Function} listener The method to call when the event is fired.
|
|
255
27
|
* @returns {Emitter} The emitter instance.
|
|
256
28
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
29
|
+
_on(event, id, listener) {
|
|
30
|
+
const generateEmitterTimeout = (emitter) => {
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
listener.apply(this, emitter.args);
|
|
33
|
+
}, 1);
|
|
34
|
+
};
|
|
35
|
+
this._listeners = this._listeners || {};
|
|
36
|
+
this._listeners[event] = this._listeners[event] || {};
|
|
37
|
+
this._listeners[event][id] = this._listeners[event][id] || [];
|
|
38
|
+
this._listeners[event][id].push(listener);
|
|
39
|
+
// Check for any static emitters, and fire the event if any exist
|
|
40
|
+
if (!this._emitters || !this._emitters[event] || !this._emitters[event].length)
|
|
41
|
+
return this;
|
|
42
|
+
// Emit events for each emitter
|
|
43
|
+
this._emitters[event].forEach((emitter) => {
|
|
44
|
+
if (id === "*" || emitter.id === id) {
|
|
45
|
+
// Call the listener out of process so that any code that expects a listener
|
|
46
|
+
// to be called at some point in the future rather than immediately on registration
|
|
47
|
+
// will not fail
|
|
48
|
+
generateEmitterTimeout(emitter);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return this;
|
|
274
52
|
}
|
|
275
53
|
/**
|
|
276
54
|
* Attach an event listener to the passed event only if the passed
|
|
@@ -282,25 +60,16 @@ function () {
|
|
|
282
60
|
* @param {Function} listener The method to call when the event is fired.
|
|
283
61
|
* @returns {Emitter} The emitter instance.
|
|
284
62
|
*/
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return (
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
if (restTypes[0] === "function") {
|
|
298
|
-
this.off(event);
|
|
299
|
-
return EventMainMethods._on.call(this, event, "*", rest[0]);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
this.off(event, rest[0]);
|
|
303
|
-
return EventMainMethods._on.call(this, event, rest[0], rest[1]);
|
|
63
|
+
_once(event, id, listener) {
|
|
64
|
+
let fired = false;
|
|
65
|
+
const internalCallback = (...rest) => {
|
|
66
|
+
if (fired)
|
|
67
|
+
return;
|
|
68
|
+
fired = true;
|
|
69
|
+
this.off(event, id, internalCallback);
|
|
70
|
+
listener(...rest);
|
|
71
|
+
};
|
|
72
|
+
return this.on(event, id, internalCallback);
|
|
304
73
|
}
|
|
305
74
|
/**
|
|
306
75
|
* Cancels an event listener based on an event name, id and listener function.
|
|
@@ -312,28 +81,66 @@ function () {
|
|
|
312
81
|
* or once() call to cancel.
|
|
313
82
|
* @returns {Emitter} The emitter instance.
|
|
314
83
|
*/
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
84
|
+
_off(event, id, listener) {
|
|
85
|
+
if (this._emitting) {
|
|
86
|
+
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
87
|
+
this._eventRemovalQueue.push(() => {
|
|
88
|
+
this.off(event, id, listener);
|
|
89
|
+
});
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
if (!this._listeners || !this._listeners[event] || !this._listeners[event][id])
|
|
93
|
+
return this;
|
|
94
|
+
if (id && !listener) {
|
|
95
|
+
if (id === "*") {
|
|
96
|
+
delete this._listeners[event];
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
// No listener provided, delete all listeners
|
|
100
|
+
delete this._listeners[event][id];
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
if (!listener)
|
|
104
|
+
return this;
|
|
105
|
+
const arr = this._listeners[event][id] || [];
|
|
106
|
+
const index = arr.indexOf(listener);
|
|
107
|
+
if (index > -1) {
|
|
108
|
+
arr.splice(index, 1);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
on(event, ...rest) {
|
|
112
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
113
|
+
if (restTypes[0] === "function") {
|
|
114
|
+
return this._on.call(this, event, "*", rest[0]);
|
|
115
|
+
}
|
|
116
|
+
return this._on.call(this, event, rest[0], rest[1]);
|
|
117
|
+
}
|
|
118
|
+
once(event, ...rest) {
|
|
119
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
120
|
+
if (restTypes[0] === "function") {
|
|
121
|
+
return this._once.call(this, event, "*", rest[0]);
|
|
122
|
+
}
|
|
123
|
+
return this._once.call(this, event, rest[0], rest[1]);
|
|
124
|
+
}
|
|
125
|
+
one(event, ...rest) {
|
|
126
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
127
|
+
if (restTypes[0] === "function") {
|
|
128
|
+
this.off(event);
|
|
129
|
+
return this._on.call(this, event, "*", rest[0]);
|
|
130
|
+
}
|
|
131
|
+
this.off(event, rest[0]);
|
|
132
|
+
return this._on.call(this, event, rest[0], rest[1]);
|
|
133
|
+
}
|
|
134
|
+
off(event, ...rest) {
|
|
135
|
+
if (rest.length === 0) {
|
|
136
|
+
// Only event was provided
|
|
137
|
+
return this._off.call(this, event, "*");
|
|
138
|
+
}
|
|
139
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
140
|
+
if (restTypes[0] === "function") {
|
|
141
|
+
return this._off.call(this, event, "*", rest[0]);
|
|
142
|
+
}
|
|
143
|
+
return this._off.call(this, event, rest[0], rest[1]);
|
|
337
144
|
}
|
|
338
145
|
/**
|
|
339
146
|
* Handles emitting events, is an internal method not called directly.
|
|
@@ -342,204 +149,99 @@ function () {
|
|
|
342
149
|
* @returns {Emitter} The emitter instance.
|
|
343
150
|
* @private
|
|
344
151
|
*/
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
363
|
-
// Check we have a function to execute
|
|
364
|
-
var tmpFunc = arr[arrIndex];
|
|
365
|
-
|
|
366
|
-
if (typeof tmpFunc === "function") {
|
|
367
|
-
tmpFunc.call.apply(tmpFunc, [this].concat(data));
|
|
368
|
-
}
|
|
152
|
+
emit(event, ...data) {
|
|
153
|
+
const id = "*";
|
|
154
|
+
this._listeners = this._listeners || {};
|
|
155
|
+
this._emitting = true;
|
|
156
|
+
if (this._listeners[event] && this._listeners[event][id]) {
|
|
157
|
+
// Handle global emit
|
|
158
|
+
const arr = this._listeners[event][id];
|
|
159
|
+
const arrCount = arr.length;
|
|
160
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
161
|
+
// Check we have a function to execute
|
|
162
|
+
const tmpFunc = arr[arrIndex];
|
|
163
|
+
if (typeof tmpFunc === "function") {
|
|
164
|
+
tmpFunc.call(this, ...data);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
369
167
|
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
this._emitting = false;
|
|
373
|
-
|
|
374
|
-
this._processRemovalQueue();
|
|
375
|
-
|
|
376
|
-
return this;
|
|
377
|
-
}
|
|
378
|
-
}, {
|
|
379
|
-
key: "emitId",
|
|
380
|
-
value: function emitId(event, id) {
|
|
381
|
-
this._listeners = this._listeners || {};
|
|
382
|
-
this._emitting = true;
|
|
383
|
-
|
|
384
|
-
if (!this._listeners[event]) {
|
|
385
168
|
this._emitting = false;
|
|
386
|
-
|
|
387
169
|
this._processRemovalQueue();
|
|
388
|
-
|
|
389
170
|
return this;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
171
|
+
}
|
|
172
|
+
emitId(event, id, ...data) {
|
|
173
|
+
this._listeners = this._listeners || {};
|
|
174
|
+
this._emitting = true;
|
|
175
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
176
|
+
this._emitting = false;
|
|
177
|
+
this._processRemovalQueue();
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
// Handle id emit
|
|
181
|
+
if (this._listeners[event][id]) {
|
|
182
|
+
const arr = this._listeners[event][id];
|
|
183
|
+
const arrCount = arr.length;
|
|
184
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
185
|
+
// Check we have a function to execute
|
|
186
|
+
const tmpFunc = arr[arrIndex];
|
|
187
|
+
if (typeof tmpFunc === "function") {
|
|
188
|
+
tmpFunc.call(this, ...data);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
408
191
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
if (typeof _tmpFunc === "function") {
|
|
421
|
-
_tmpFunc.call.apply(_tmpFunc, [this].concat(data));
|
|
422
|
-
}
|
|
192
|
+
// Handle global emit
|
|
193
|
+
if (this._listeners[event]["*"]) {
|
|
194
|
+
const arr = this._listeners[event]["*"];
|
|
195
|
+
const arrCount = arr.length;
|
|
196
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
197
|
+
// Check we have a function to execute
|
|
198
|
+
const tmpFunc = arr[arrIndex];
|
|
199
|
+
if (typeof tmpFunc === "function") {
|
|
200
|
+
tmpFunc.call(this, ...data);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
423
203
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this._processRemovalQueue();
|
|
429
|
-
|
|
430
|
-
return this;
|
|
204
|
+
this._emitting = false;
|
|
205
|
+
this._processRemovalQueue();
|
|
206
|
+
return this;
|
|
431
207
|
}
|
|
432
208
|
/**
|
|
433
|
-
*
|
|
209
|
+
* Creates a static event emitter that is emitted whenever an
|
|
210
|
+
* event listener is created with the same name.
|
|
434
211
|
* @param {String} event The name of the event to emit.
|
|
435
212
|
* @param {*} data The data to emit with the event.
|
|
436
213
|
* @returns {Emitter} The emitter instance.
|
|
437
|
-
* @private
|
|
438
214
|
*/
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
this._emitting = true;
|
|
450
|
-
|
|
451
|
-
if (this._listeners[event] && this._listeners[event][id]) {
|
|
452
|
-
// Handle global emit
|
|
453
|
-
var arr = this._listeners[event][id];
|
|
454
|
-
var arrCount = arr.length;
|
|
455
|
-
|
|
456
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
457
|
-
// Check we have a function to execute
|
|
458
|
-
var tmpFunc = arr[arrIndex];
|
|
459
|
-
|
|
460
|
-
if (typeof tmpFunc === "function") {
|
|
461
|
-
tmpFunc.call.apply(tmpFunc, [this].concat(data));
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
this._emitting = false;
|
|
467
|
-
this._emitters = this._emitters || {};
|
|
468
|
-
this._emitters[event] = this._emitters[event] || [];
|
|
469
|
-
|
|
470
|
-
this._emitters[event].push({
|
|
471
|
-
"id": "*",
|
|
472
|
-
"args": data
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
this._processRemovalQueue();
|
|
476
|
-
|
|
477
|
-
return this;
|
|
215
|
+
emitStatic(event, ...data) {
|
|
216
|
+
this._emitters = this._emitters || {};
|
|
217
|
+
this._emitters[event] = this._emitters[event] || [];
|
|
218
|
+
this._emitters[event].push({
|
|
219
|
+
"id": "*",
|
|
220
|
+
"args": data
|
|
221
|
+
});
|
|
222
|
+
this.emit(event, ...data);
|
|
223
|
+
this._processRemovalQueue();
|
|
224
|
+
return this;
|
|
478
225
|
}
|
|
479
226
|
/**
|
|
480
|
-
*
|
|
227
|
+
* Creates a static event emitter that is emitted whenever an
|
|
228
|
+
* event listener is created with the same name and id.
|
|
481
229
|
* @param {String} event The name of the event to emit.
|
|
482
230
|
* @param {String} id The id of the event to emit.
|
|
483
231
|
* @param {*} data The data to emit with the event.
|
|
484
232
|
* @returns {Emitter} The emitter instance.
|
|
485
|
-
* @private
|
|
486
233
|
*/
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
if (this._listeners[event]) {
|
|
500
|
-
// Handle id emit
|
|
501
|
-
if (this._listeners[event][id]) {
|
|
502
|
-
var arr = this._listeners[event][id];
|
|
503
|
-
var arrCount = arr.length;
|
|
504
|
-
|
|
505
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
506
|
-
// Check we have a function to execute
|
|
507
|
-
var tmpFunc = arr[arrIndex];
|
|
508
|
-
|
|
509
|
-
if (typeof tmpFunc === "function") {
|
|
510
|
-
tmpFunc.call.apply(tmpFunc, [this].concat(data));
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
} // Handle global emit
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
if (this._listeners[event]["*"]) {
|
|
517
|
-
var _arr2 = this._listeners[event]["*"];
|
|
518
|
-
var _arrCount2 = _arr2.length;
|
|
519
|
-
|
|
520
|
-
for (var _arrIndex2 = 0; _arrIndex2 < _arrCount2; _arrIndex2++) {
|
|
521
|
-
// Check we have a function to execute
|
|
522
|
-
var _tmpFunc2 = _arr2[_arrIndex2];
|
|
523
|
-
|
|
524
|
-
if (typeof _tmpFunc2 === "function") {
|
|
525
|
-
_tmpFunc2.call.apply(_tmpFunc2, [this].concat(data));
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
this._emitting = false;
|
|
532
|
-
this._emitters = this._emitters || {};
|
|
533
|
-
this._emitters[event] = this._emitters[event] || [];
|
|
534
|
-
|
|
535
|
-
this._emitters[event].push({
|
|
536
|
-
id: id,
|
|
537
|
-
"args": data
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
this._processRemovalQueue();
|
|
541
|
-
|
|
542
|
-
return this;
|
|
234
|
+
emitStaticId(event, id, ...data) {
|
|
235
|
+
if (!id)
|
|
236
|
+
throw new Error("Missing id from emitStaticId call!");
|
|
237
|
+
this._emitters = this._emitters || {};
|
|
238
|
+
this._emitters[event] = this._emitters[event] || [];
|
|
239
|
+
this._emitters[event].push({
|
|
240
|
+
id,
|
|
241
|
+
"args": data
|
|
242
|
+
});
|
|
243
|
+
this.emitId(event, id, ...data);
|
|
244
|
+
return this;
|
|
543
245
|
}
|
|
544
246
|
/**
|
|
545
247
|
* Handles removing emitters, is an internal method not called directly.
|
|
@@ -547,13 +249,10 @@ function () {
|
|
|
547
249
|
* @returns {Emitter} The emitter instance.
|
|
548
250
|
* @private
|
|
549
251
|
*/
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
this._emitters = this._emitters || {};
|
|
555
|
-
this._emitters[event] = [];
|
|
556
|
-
return this;
|
|
252
|
+
cancelStatic(event) {
|
|
253
|
+
this._emitters = this._emitters || {};
|
|
254
|
+
this._emitters[event] = [];
|
|
255
|
+
return this;
|
|
557
256
|
}
|
|
558
257
|
/**
|
|
559
258
|
* Checks if an event has any event listeners or not.
|
|
@@ -563,29 +262,21 @@ function () {
|
|
|
563
262
|
* @returns {boolean} True if one or more event listeners are registered for
|
|
564
263
|
* the event. False if none are found.
|
|
565
264
|
*/
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
var id = "*";
|
|
571
|
-
|
|
572
|
-
if (!this._listeners || !this._listeners[event]) {
|
|
573
|
-
return false;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
var arr = this._listeners[event][id];
|
|
577
|
-
var arrCount = arr.length;
|
|
578
|
-
|
|
579
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
580
|
-
// Check we have a function to execute
|
|
581
|
-
var tmpFunc = arr[arrIndex];
|
|
582
|
-
|
|
583
|
-
if (typeof tmpFunc === "function") {
|
|
584
|
-
return true;
|
|
265
|
+
willEmit(event) {
|
|
266
|
+
const id = "*";
|
|
267
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
268
|
+
return false;
|
|
585
269
|
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
270
|
+
const arr = this._listeners[event][id];
|
|
271
|
+
const arrCount = arr.length;
|
|
272
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
273
|
+
// Check we have a function to execute
|
|
274
|
+
const tmpFunc = arr[arrIndex];
|
|
275
|
+
if (typeof tmpFunc === "function") {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
589
280
|
}
|
|
590
281
|
/**
|
|
591
282
|
* Checks if an event has any event listeners or not based on the passed id.
|
|
@@ -596,89 +287,63 @@ function () {
|
|
|
596
287
|
* @returns {boolean} True if one or more event listeners are registered for
|
|
597
288
|
* the event. False if none are found.
|
|
598
289
|
*/
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
var tmpFunc = arr[arrIndex];
|
|
615
|
-
|
|
616
|
-
if (typeof tmpFunc === "function") {
|
|
617
|
-
return true;
|
|
618
|
-
}
|
|
290
|
+
willEmitId(event, id) {
|
|
291
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
// Handle id emit
|
|
295
|
+
if (this._listeners[event][id]) {
|
|
296
|
+
const arr = this._listeners[event][id];
|
|
297
|
+
const arrCount = arr.length;
|
|
298
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
299
|
+
// Check we have a function to execute
|
|
300
|
+
const tmpFunc = arr[arrIndex];
|
|
301
|
+
if (typeof tmpFunc === "function") {
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
619
305
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
if (typeof _tmpFunc3 === "function") {
|
|
632
|
-
return true;
|
|
633
|
-
}
|
|
306
|
+
// Handle global emit
|
|
307
|
+
if (this._listeners[event]["*"]) {
|
|
308
|
+
const arr = this._listeners[event]["*"];
|
|
309
|
+
const arrCount = arr.length;
|
|
310
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
311
|
+
// Check we have a function to execute
|
|
312
|
+
const tmpFunc = arr[arrIndex];
|
|
313
|
+
if (typeof tmpFunc === "function") {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
634
317
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
return false;
|
|
318
|
+
return false;
|
|
638
319
|
}
|
|
639
320
|
/**
|
|
640
321
|
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
641
322
|
* events of the same type that occur within 100 milliseconds of a previous
|
|
642
323
|
* one will all be wrapped into a single emit rather than emitting tons of
|
|
643
|
-
* events for
|
|
644
|
-
*
|
|
324
|
+
* events for the same eventName. Only the data from the last de-bounced event
|
|
325
|
+
* will be emitted.
|
|
645
326
|
* @memberOf Emitter
|
|
646
327
|
* @method deferEmit
|
|
647
328
|
* @param {String} eventName The name of the event to emit.
|
|
648
329
|
* @param {*=} data Optional data to emit with the event.
|
|
649
330
|
* @returns {Emitter} The emitter instance.
|
|
650
331
|
*/
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
for (var _len9 = arguments.length, data = new Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) {
|
|
658
|
-
data[_key9 - 1] = arguments[_key9];
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
if (!this._noEmitDefer && (!this._db || this._db && !this._db._noEmitDefer)) {
|
|
332
|
+
deferEmit(eventName, ...data) {
|
|
333
|
+
if (this._noEmitDefer) {
|
|
334
|
+
this.emit(eventName, ...data);
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
662
337
|
// Check for an existing timeout
|
|
663
338
|
this._deferTimeout = this._deferTimeout || {};
|
|
664
|
-
|
|
665
339
|
if (this._deferTimeout[eventName]) {
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
(_this4$emit = _this4.emit).call.apply(_this4$emit, [_this4, eventName].concat(data));
|
|
674
|
-
}, 1);
|
|
675
|
-
} else {
|
|
676
|
-
var _this$emit;
|
|
677
|
-
|
|
678
|
-
(_this$emit = this.emit).call.apply(_this$emit, [this, eventName].concat(data));
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
return this;
|
|
340
|
+
clearTimeout(this._deferTimeout[eventName]);
|
|
341
|
+
}
|
|
342
|
+
// Set a timeout
|
|
343
|
+
this._deferTimeout[eventName] = setTimeout(() => {
|
|
344
|
+
this.emit(eventName, ...data);
|
|
345
|
+
}, 100);
|
|
346
|
+
return this;
|
|
682
347
|
}
|
|
683
348
|
/**
|
|
684
349
|
* If events are cleared with the off() method while the event emitter is
|
|
@@ -688,90 +353,63 @@ function () {
|
|
|
688
353
|
* the emitter is running through them. This method is called after the
|
|
689
354
|
* event emitter is finished processing.
|
|
690
355
|
* @private
|
|
356
|
+
* @returns {undefined}
|
|
691
357
|
*/
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
this._eventRemovalQueue[i]();
|
|
703
|
-
} // Clear the removal queue
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
this._eventRemovalQueue = [];
|
|
358
|
+
_processRemovalQueue() {
|
|
359
|
+
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
// Execute each removal call
|
|
363
|
+
for (let i = 0; i < this._eventRemovalQueue.length; i++) {
|
|
364
|
+
this._eventRemovalQueue[i]();
|
|
365
|
+
}
|
|
366
|
+
// Clear the removal queue
|
|
367
|
+
this._eventRemovalQueue = [];
|
|
707
368
|
}
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
if (typeof obj === "boolean" && prototypeMode === undefined) {
|
|
735
|
-
prototypeMode = obj;
|
|
736
|
-
obj = {};
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
if (prototypeMode === undefined) {
|
|
740
|
-
prototypeMode = true;
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
if ((0, _typeof2.default)(obj) !== "object" && typeof obj !== "function") {
|
|
744
|
-
throw new Error("Cannot operate on a non-object / non-function passed as first argument!");
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
if (prototypeMode) {
|
|
748
|
-
if (obj.prototype === undefined) {
|
|
749
|
-
throw new Error("Cannot modify prototype of passed object, it has no prototype property! Was it instantiated with the new operator correctly?");
|
|
369
|
+
}
|
|
370
|
+
exports.Emitter = Emitter;
|
|
371
|
+
const makeEmitter = (obj, prototypeMode = false) => {
|
|
372
|
+
let operateOnObject;
|
|
373
|
+
if (obj === undefined && prototypeMode === undefined) {
|
|
374
|
+
obj = {};
|
|
375
|
+
prototypeMode = false;
|
|
376
|
+
}
|
|
377
|
+
if (typeof obj === "boolean" && prototypeMode === undefined) {
|
|
378
|
+
prototypeMode = obj;
|
|
379
|
+
obj = {};
|
|
380
|
+
}
|
|
381
|
+
if (prototypeMode === undefined) {
|
|
382
|
+
prototypeMode = false;
|
|
383
|
+
}
|
|
384
|
+
if (typeof obj !== "object" && typeof obj !== "function") {
|
|
385
|
+
throw new Error("Cannot operate on passed value, it is not an object, function or class!");
|
|
386
|
+
}
|
|
387
|
+
if (prototypeMode) {
|
|
388
|
+
if (obj.prototype === undefined) {
|
|
389
|
+
throw new Error("Cannot modify prototype of passed object, it has no prototype property!");
|
|
390
|
+
}
|
|
391
|
+
operateOnObject = obj.prototype;
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
operateOnObject = obj;
|
|
750
395
|
}
|
|
751
|
-
|
|
752
|
-
operateOnObject =
|
|
753
|
-
|
|
754
|
-
operateOnObject =
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
operateOnObject.willEmitId = Emitter.prototype.willEmitId;
|
|
770
|
-
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
771
|
-
return obj;
|
|
396
|
+
// Convert the object to have eventing capability
|
|
397
|
+
operateOnObject.on = Emitter.prototype.on;
|
|
398
|
+
operateOnObject.off = Emitter.prototype.off;
|
|
399
|
+
operateOnObject.one = Emitter.prototype.one;
|
|
400
|
+
operateOnObject.once = Emitter.prototype.once;
|
|
401
|
+
operateOnObject.emit = Emitter.prototype.emit;
|
|
402
|
+
operateOnObject.emitId = Emitter.prototype.emitId;
|
|
403
|
+
operateOnObject.emitStatic = Emitter.prototype.emitStatic;
|
|
404
|
+
operateOnObject.emitStaticId = Emitter.prototype.emitStaticId;
|
|
405
|
+
operateOnObject.cancelStatic = Emitter.prototype.cancelStatic;
|
|
406
|
+
operateOnObject.deferEmit = Emitter.prototype.deferEmit;
|
|
407
|
+
operateOnObject.willEmit = Emitter.prototype.willEmit;
|
|
408
|
+
operateOnObject.willEmitId = Emitter.prototype.willEmitId;
|
|
409
|
+
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
410
|
+
operateOnObject._on = Emitter.prototype._on;
|
|
411
|
+
operateOnObject._off = Emitter.prototype._off;
|
|
412
|
+
operateOnObject._once = Emitter.prototype._once;
|
|
413
|
+
return obj;
|
|
772
414
|
};
|
|
773
|
-
|
|
774
|
-
module.exports = {
|
|
775
|
-
Emitter: Emitter,
|
|
776
|
-
makeEmitter: makeEmitter
|
|
777
|
-
};
|
|
415
|
+
exports.makeEmitter = makeEmitter;
|