@irrelon/emitter 4.0.1 → 5.0.2
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/.eslintrc.json +36 -13
- package/.prettierignore +9 -0
- package/.prettierrc.yml +9 -0
- package/fixPaths.cjs +183 -0
- package/package.json +61 -52
- package/readme.md +5 -1
- package/src/Emitter.d.ts +190 -0
- package/src/Emitter.js +550 -645
- package/src/Emitter.ts +719 -0
- package/tests/Emitter.test.d.ts +1 -0
- package/tests/Emitter.test.js +267 -289
- package/tests/Emitter.test.ts +335 -0
- package/tsconfig.json +50 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/.babelrc +0 -11
- package/dist/Emitter.js +0 -777
package/dist/Emitter.js
DELETED
|
@@ -1,777 +0,0 @@
|
|
|
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
|
-
"use strict";
|
|
81
|
-
|
|
82
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
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
|
-
};
|
|
207
|
-
/**
|
|
208
|
-
* @class Emitter
|
|
209
|
-
* @constructor
|
|
210
|
-
*/
|
|
211
|
-
|
|
212
|
-
var Emitter =
|
|
213
|
-
/*#__PURE__*/
|
|
214
|
-
function () {
|
|
215
|
-
function Emitter() {
|
|
216
|
-
(0, _classCallCheck2.default)(this, Emitter);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
(0, _createClass2.default)(Emitter, [{
|
|
220
|
-
key: "on",
|
|
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]);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Attach an event listener to the passed event only if the passed
|
|
249
|
-
* id matches the document id for the event being fired.
|
|
250
|
-
* @memberOf Emitter
|
|
251
|
-
* @method once
|
|
252
|
-
* @param {String} event The name of the event to listen for.
|
|
253
|
-
* @param {*} id The document id to match against.
|
|
254
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
255
|
-
* @returns {Emitter} The emitter instance.
|
|
256
|
-
*/
|
|
257
|
-
|
|
258
|
-
}, {
|
|
259
|
-
key: "once",
|
|
260
|
-
value: function once(event) {
|
|
261
|
-
for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
262
|
-
rest[_key2 - 1] = arguments[_key2];
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
var restTypes = rest.map(function (arg) {
|
|
266
|
-
return (0, _typeof2.default)(arg);
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
if (restTypes[0] === "function") {
|
|
270
|
-
return EventMainMethods._once.call(this, event, "*", rest[0]);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
return EventMainMethods._once.call(this, event, rest[0], rest[1]);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Attach an event listener to the passed event only if the passed
|
|
277
|
-
* id matches the document id for the event being fired.
|
|
278
|
-
* @memberOf Emitter
|
|
279
|
-
* @method once
|
|
280
|
-
* @param {String} event The name of the event to listen for.
|
|
281
|
-
* @param {*} id The document id to match against.
|
|
282
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
283
|
-
* @returns {Emitter} The emitter instance.
|
|
284
|
-
*/
|
|
285
|
-
|
|
286
|
-
}, {
|
|
287
|
-
key: "one",
|
|
288
|
-
value: function one(event) {
|
|
289
|
-
for (var _len3 = arguments.length, rest = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
290
|
-
rest[_key3 - 1] = arguments[_key3];
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
var restTypes = rest.map(function (arg) {
|
|
294
|
-
return (0, _typeof2.default)(arg);
|
|
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]);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Cancels an event listener based on an event name, id and listener function.
|
|
307
|
-
* @memberOf Emitter
|
|
308
|
-
* @method off
|
|
309
|
-
* @param {String} event The event to cancel listener for.
|
|
310
|
-
* @param {String} id The ID of the event to cancel listening for.
|
|
311
|
-
* @param {Function} listener The event listener function used in the on()
|
|
312
|
-
* or once() call to cancel.
|
|
313
|
-
* @returns {Emitter} The emitter instance.
|
|
314
|
-
*/
|
|
315
|
-
|
|
316
|
-
}, {
|
|
317
|
-
key: "off",
|
|
318
|
-
value: function off(event) {
|
|
319
|
-
for (var _len4 = arguments.length, rest = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
|
|
320
|
-
rest[_key4 - 1] = arguments[_key4];
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (rest.length === 0) {
|
|
324
|
-
// Only event was provided
|
|
325
|
-
return EventMainMethods._off.call(this, event, "*");
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
var restTypes = rest.map(function (arg) {
|
|
329
|
-
return (0, _typeof2.default)(arg);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
if (restTypes[0] === "function") {
|
|
333
|
-
return EventMainMethods._off.call(this, event, "*", rest[0]);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return EventMainMethods._off.call(this, event, rest[0], rest[1]);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Handles emitting events, is an internal method not called directly.
|
|
340
|
-
* @param {String} event The name of the event to emit.
|
|
341
|
-
* @param {*} data The data to emit with the event.
|
|
342
|
-
* @returns {Emitter} The emitter instance.
|
|
343
|
-
* @private
|
|
344
|
-
*/
|
|
345
|
-
|
|
346
|
-
}, {
|
|
347
|
-
key: "emit",
|
|
348
|
-
value: function emit(event) {
|
|
349
|
-
var id = "*";
|
|
350
|
-
this._listeners = this._listeners || {};
|
|
351
|
-
this._emitting = true;
|
|
352
|
-
|
|
353
|
-
if (this._listeners[event] && this._listeners[event][id]) {
|
|
354
|
-
// Handle global emit
|
|
355
|
-
var arr = this._listeners[event][id];
|
|
356
|
-
var arrCount = arr.length;
|
|
357
|
-
|
|
358
|
-
for (var _len5 = arguments.length, data = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
|
|
359
|
-
data[_key5 - 1] = arguments[_key5];
|
|
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
|
-
}
|
|
369
|
-
}
|
|
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
|
-
this._emitting = false;
|
|
386
|
-
|
|
387
|
-
this._processRemovalQueue();
|
|
388
|
-
|
|
389
|
-
return this;
|
|
390
|
-
} // Handle id emit
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
for (var _len6 = arguments.length, data = new Array(_len6 > 2 ? _len6 - 2 : 0), _key6 = 2; _key6 < _len6; _key6++) {
|
|
394
|
-
data[_key6 - 2] = arguments[_key6];
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
if (this._listeners[event][id]) {
|
|
398
|
-
var arr = this._listeners[event][id];
|
|
399
|
-
var arrCount = arr.length;
|
|
400
|
-
|
|
401
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
402
|
-
// Check we have a function to execute
|
|
403
|
-
var tmpFunc = arr[arrIndex];
|
|
404
|
-
|
|
405
|
-
if (typeof tmpFunc === "function") {
|
|
406
|
-
tmpFunc.call.apply(tmpFunc, [this].concat(data));
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
} // Handle global emit
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
if (this._listeners[event]["*"]) {
|
|
413
|
-
var _arr = this._listeners[event]["*"];
|
|
414
|
-
var _arrCount = _arr.length;
|
|
415
|
-
|
|
416
|
-
for (var _arrIndex = 0; _arrIndex < _arrCount; _arrIndex++) {
|
|
417
|
-
// Check we have a function to execute
|
|
418
|
-
var _tmpFunc = _arr[_arrIndex];
|
|
419
|
-
|
|
420
|
-
if (typeof _tmpFunc === "function") {
|
|
421
|
-
_tmpFunc.call.apply(_tmpFunc, [this].concat(data));
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
this._emitting = false;
|
|
427
|
-
|
|
428
|
-
this._processRemovalQueue();
|
|
429
|
-
|
|
430
|
-
return this;
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Handles emitting events, is an internal method not called directly.
|
|
434
|
-
* @param {String} event The name of the event to emit.
|
|
435
|
-
* @param {*} data The data to emit with the event.
|
|
436
|
-
* @returns {Emitter} The emitter instance.
|
|
437
|
-
* @private
|
|
438
|
-
*/
|
|
439
|
-
|
|
440
|
-
}, {
|
|
441
|
-
key: "emitStatic",
|
|
442
|
-
value: function emitStatic(event) {
|
|
443
|
-
for (var _len7 = arguments.length, data = new Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) {
|
|
444
|
-
data[_key7 - 1] = arguments[_key7];
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
var id = "*";
|
|
448
|
-
this._listeners = this._listeners || {};
|
|
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;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Handles emitting events, is an internal method not called directly.
|
|
481
|
-
* @param {String} event The name of the event to emit.
|
|
482
|
-
* @param {String} id The id of the event to emit.
|
|
483
|
-
* @param {*} data The data to emit with the event.
|
|
484
|
-
* @returns {Emitter} The emitter instance.
|
|
485
|
-
* @private
|
|
486
|
-
*/
|
|
487
|
-
|
|
488
|
-
}, {
|
|
489
|
-
key: "emitStaticId",
|
|
490
|
-
value: function emitStaticId(event, id) {
|
|
491
|
-
for (var _len8 = arguments.length, data = new Array(_len8 > 2 ? _len8 - 2 : 0), _key8 = 2; _key8 < _len8; _key8++) {
|
|
492
|
-
data[_key8 - 2] = arguments[_key8];
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
if (!id) throw new Error("Missing id from emitId call!");
|
|
496
|
-
this._listeners = this._listeners || {};
|
|
497
|
-
this._emitting = true;
|
|
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;
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* Handles removing emitters, is an internal method not called directly.
|
|
546
|
-
* @param {String} event The event to remove static emitter for.
|
|
547
|
-
* @returns {Emitter} The emitter instance.
|
|
548
|
-
* @private
|
|
549
|
-
*/
|
|
550
|
-
|
|
551
|
-
}, {
|
|
552
|
-
key: "cancelStatic",
|
|
553
|
-
value: function cancelStatic(event) {
|
|
554
|
-
this._emitters = this._emitters || {};
|
|
555
|
-
this._emitters[event] = [];
|
|
556
|
-
return this;
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Checks if an event has any event listeners or not.
|
|
560
|
-
* @memberOf Emitter
|
|
561
|
-
* @method willEmit
|
|
562
|
-
* @param {String} event The name of the event to check for.
|
|
563
|
-
* @returns {boolean} True if one or more event listeners are registered for
|
|
564
|
-
* the event. False if none are found.
|
|
565
|
-
*/
|
|
566
|
-
|
|
567
|
-
}, {
|
|
568
|
-
key: "willEmit",
|
|
569
|
-
value: function willEmit(event) {
|
|
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;
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
return false;
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Checks if an event has any event listeners or not based on the passed id.
|
|
592
|
-
* @memberOf Emitter
|
|
593
|
-
* @method willEmitId
|
|
594
|
-
* @param {String} event The name of the event to check for.
|
|
595
|
-
* @param {String} id The event ID to check for.
|
|
596
|
-
* @returns {boolean} True if one or more event listeners are registered for
|
|
597
|
-
* the event. False if none are found.
|
|
598
|
-
*/
|
|
599
|
-
|
|
600
|
-
}, {
|
|
601
|
-
key: "willEmitId",
|
|
602
|
-
value: function willEmitId(event, id) {
|
|
603
|
-
if (!this._listeners || !this._listeners[event]) {
|
|
604
|
-
return false;
|
|
605
|
-
} // Handle id emit
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
if (this._listeners[event][id]) {
|
|
609
|
-
var arr = this._listeners[event][id];
|
|
610
|
-
var arrCount = arr.length;
|
|
611
|
-
|
|
612
|
-
for (var arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
613
|
-
// Check we have a function to execute
|
|
614
|
-
var tmpFunc = arr[arrIndex];
|
|
615
|
-
|
|
616
|
-
if (typeof tmpFunc === "function") {
|
|
617
|
-
return true;
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
} // Handle global emit
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
if (this._listeners[event]["*"]) {
|
|
624
|
-
var _arr3 = this._listeners[event]["*"];
|
|
625
|
-
var _arrCount3 = _arr3.length;
|
|
626
|
-
|
|
627
|
-
for (var _arrIndex3 = 0; _arrIndex3 < _arrCount3; _arrIndex3++) {
|
|
628
|
-
// Check we have a function to execute
|
|
629
|
-
var _tmpFunc3 = _arr3[_arrIndex3];
|
|
630
|
-
|
|
631
|
-
if (typeof _tmpFunc3 === "function") {
|
|
632
|
-
return true;
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
return false;
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
641
|
-
* events of the same type that occur within 100 milliseconds of a previous
|
|
642
|
-
* one will all be wrapped into a single emit rather than emitting tons of
|
|
643
|
-
* events for lots of chained inserts etc. Only the data from the last
|
|
644
|
-
* de-bounced event will be emitted.
|
|
645
|
-
* @memberOf Emitter
|
|
646
|
-
* @method deferEmit
|
|
647
|
-
* @param {String} eventName The name of the event to emit.
|
|
648
|
-
* @param {*=} data Optional data to emit with the event.
|
|
649
|
-
* @returns {Emitter} The emitter instance.
|
|
650
|
-
*/
|
|
651
|
-
|
|
652
|
-
}, {
|
|
653
|
-
key: "deferEmit",
|
|
654
|
-
value: function deferEmit(eventName) {
|
|
655
|
-
var _this4 = this;
|
|
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)) {
|
|
662
|
-
// Check for an existing timeout
|
|
663
|
-
this._deferTimeout = this._deferTimeout || {};
|
|
664
|
-
|
|
665
|
-
if (this._deferTimeout[eventName]) {
|
|
666
|
-
clearTimeout(this._deferTimeout[eventName]);
|
|
667
|
-
} // Set a timeout
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
this._deferTimeout[eventName] = setTimeout(function () {
|
|
671
|
-
var _this4$emit;
|
|
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;
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* If events are cleared with the off() method while the event emitter is
|
|
685
|
-
* actively processing any events then the off() calls get added to a
|
|
686
|
-
* queue to be executed after the event emitter is finished. This stops
|
|
687
|
-
* errors that might occur by potentially modifying the event queue while
|
|
688
|
-
* the emitter is running through them. This method is called after the
|
|
689
|
-
* event emitter is finished processing.
|
|
690
|
-
* @private
|
|
691
|
-
*/
|
|
692
|
-
|
|
693
|
-
}, {
|
|
694
|
-
key: "_processRemovalQueue",
|
|
695
|
-
value: function _processRemovalQueue() {
|
|
696
|
-
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
697
|
-
return;
|
|
698
|
-
} // Execute each removal call
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
for (var i = 0; i < this._eventRemovalQueue.length; i++) {
|
|
702
|
-
this._eventRemovalQueue[i]();
|
|
703
|
-
} // Clear the removal queue
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
this._eventRemovalQueue = [];
|
|
707
|
-
}
|
|
708
|
-
}]);
|
|
709
|
-
return Emitter;
|
|
710
|
-
}();
|
|
711
|
-
/**
|
|
712
|
-
* Makes the passed class or object into an emitter by modifying either the
|
|
713
|
-
* prototype or the actual object to include event emitter methods.
|
|
714
|
-
* @param {Object} [obj={}] The object / function / class to add event methods to.
|
|
715
|
-
* If none is provided a new object will be created. This allows you to use
|
|
716
|
-
* new Emitter() to generate an event emitter that is not tied to any other
|
|
717
|
-
* object or class.
|
|
718
|
-
* @param {Boolean} [prototypeMode=true] Defaults to true. Set to true to add emitter
|
|
719
|
-
* methods to the the passed object"s prototype property e.g. obj.prototype.on
|
|
720
|
-
* = emitter.on. Set to false to add emitter methods the object directly e.g.
|
|
721
|
-
* obj.on = emitter.on.
|
|
722
|
-
* @returns {Object} The newly augmented object.
|
|
723
|
-
*/
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
var makeEmitter = function makeEmitter(obj, prototypeMode) {
|
|
727
|
-
var operateOnObject;
|
|
728
|
-
|
|
729
|
-
if (obj === undefined && prototypeMode === undefined) {
|
|
730
|
-
obj = {};
|
|
731
|
-
prototypeMode = false;
|
|
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?");
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
operateOnObject = obj.prototype;
|
|
753
|
-
} else {
|
|
754
|
-
operateOnObject = obj;
|
|
755
|
-
} // Convert the object prototype to have eventing capability
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
operateOnObject.on = Emitter.prototype.on;
|
|
759
|
-
operateOnObject.off = Emitter.prototype.off;
|
|
760
|
-
operateOnObject.one = Emitter.prototype.one;
|
|
761
|
-
operateOnObject.once = Emitter.prototype.once;
|
|
762
|
-
operateOnObject.emit = Emitter.prototype.emit;
|
|
763
|
-
operateOnObject.emitId = Emitter.prototype.emitId;
|
|
764
|
-
operateOnObject.emitStatic = Emitter.prototype.emitStatic;
|
|
765
|
-
operateOnObject.emitStaticId = Emitter.prototype.emitStaticId;
|
|
766
|
-
operateOnObject.cancelStatic = Emitter.prototype.cancelStatic;
|
|
767
|
-
operateOnObject.deferEmit = Emitter.prototype.deferEmit;
|
|
768
|
-
operateOnObject.willEmit = Emitter.prototype.willEmit;
|
|
769
|
-
operateOnObject.willEmitId = Emitter.prototype.willEmitId;
|
|
770
|
-
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
771
|
-
return obj;
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
module.exports = {
|
|
775
|
-
Emitter: Emitter,
|
|
776
|
-
makeEmitter: makeEmitter
|
|
777
|
-
};
|