@irrelon/emitter 3.0.0 → 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/.editorconfig +1 -1
- package/.eslintignore +3 -1
- package/.eslintrc.json +17 -56
- package/dist/Emitter.d.ts +205 -0
- package/dist/Emitter.js +339 -777
- package/package.json +54 -50
- package/readme.md +44 -11
- package/src/Emitter.ts +672 -0
- package/tests/Emitter.test.js +277 -110
- package/tsconfig.json +29 -0
- package/src/Emitter.js +0 -884
package/dist/Emitter.js
CHANGED
|
@@ -1,853 +1,415 @@
|
|
|
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 2.0.11:
|
|
29
|
-
Added cancelStatic method to allow cancelling a static event
|
|
30
|
-
Version 2.0.7:
|
|
31
|
-
Fixed UMD module support
|
|
32
|
-
Version 2.0.6:
|
|
33
|
-
Added UMD module support
|
|
34
|
-
Version 2.0.5:
|
|
35
|
-
Added bower version number
|
|
36
|
-
Version 2.0.4:
|
|
37
|
-
Allow instantiation as independent instance, updated unit tests
|
|
38
|
-
Version 2.0.3:
|
|
39
|
-
Documentation updates, published to bower
|
|
40
|
-
Version 2.0.2:
|
|
41
|
-
Documentation updates
|
|
42
|
-
Version 2.0.1:
|
|
43
|
-
Bug fix in this._emitters usage
|
|
44
|
-
Version 2.0.0:
|
|
45
|
-
Big update to bring in line with latest developments in other projects. Event emitter can
|
|
46
|
-
now use deferEmit(), emitId(), emitStatic(), emitStaticId(), willEmit(), willEmitId().
|
|
47
|
-
Version 1.1.9:
|
|
48
|
-
Updated changelog correctly
|
|
49
|
-
Version 1.1.8:
|
|
50
|
-
Removed tons of dependencies wrongly included in main dependencies, have moved to devDependencies section of package.json
|
|
51
|
-
Version 1.1.0:
|
|
52
|
-
Added support for overloaded methods
|
|
53
|
-
Added support for events with ids
|
|
54
|
-
Version 1.0.2:
|
|
55
|
-
Removed AMD support, added browserify support
|
|
56
|
-
Added package.json
|
|
57
|
-
Added once() method
|
|
58
|
-
Added hasListener() method
|
|
59
|
-
Published to NPM as irrelon-emitter
|
|
60
|
-
Version 1.0.1:
|
|
61
|
-
Added ability to extend any object with eventing capability
|
|
62
|
-
Added AMD / Require.js support
|
|
63
|
-
Added Node.js support
|
|
64
|
-
Version 1.0.0:
|
|
65
|
-
First commit
|
|
66
|
-
*/
|
|
67
1
|
"use strict";
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*/
|
|
84
|
-
"string, function": function stringFunction(event, listener) {
|
|
85
|
-
return this.$main(event, "*", listener);
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Attach an event listener to the passed event only if the passed
|
|
90
|
-
* id matches the document id for the event being fired.
|
|
91
|
-
* @memberof Emitter
|
|
92
|
-
* @method on
|
|
93
|
-
* @param {String} event The name of the event to listen for.
|
|
94
|
-
* @param {*} id The document id to match against.
|
|
95
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
96
|
-
*/
|
|
97
|
-
"string, *, function": function stringFunction(event, id, listener) {
|
|
98
|
-
return this.$main(event, id, listener);
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* @param event
|
|
103
|
-
* @param id
|
|
104
|
-
* @param listener
|
|
105
|
-
* @return {$main}
|
|
106
|
-
* @private
|
|
107
|
-
*/
|
|
108
|
-
"$main": function $main(event, id, listener) {
|
|
109
|
-
var self = this,
|
|
110
|
-
generateTimeout,
|
|
111
|
-
emitter,
|
|
112
|
-
i;
|
|
113
|
-
|
|
114
|
-
generateTimeout = function generateTimeout(emitter) {
|
|
115
|
-
setTimeout(function () {
|
|
116
|
-
listener.apply(self, emitter.args);
|
|
117
|
-
}, 1);
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
this._listeners = this._listeners || {};
|
|
121
|
-
this._listeners[event] = this._listeners[event] || {};
|
|
122
|
-
this._listeners[event][id] = this._listeners[event][id] || [];
|
|
123
|
-
|
|
124
|
-
this._listeners[event][id].push(listener); // Check for any static emitters, and fire the event if any exist
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (this._emitters && this._emitters[event] && this._emitters[event].length) {
|
|
128
|
-
// Emit events for each emitter
|
|
129
|
-
for (i = 0; i < this._emitters[event].length; i++) {
|
|
130
|
-
emitter = this._emitters[event];
|
|
131
|
-
|
|
132
|
-
if (id === "*" || emitter.id === id) {
|
|
133
|
-
// Call the listener out of process so that any code that expects a listener
|
|
134
|
-
// to be called at some point in the future rather than immediately on registration
|
|
135
|
-
// will not fail
|
|
136
|
-
generateTimeout(emitter);
|
|
137
|
-
}
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeEmitter = exports.Emitter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @class Emitter
|
|
6
|
+
* @constructor
|
|
7
|
+
*/
|
|
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);
|
|
138
17
|
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return this;
|
|
142
18
|
}
|
|
143
|
-
}),
|
|
144
|
-
once: new Overload({
|
|
145
|
-
/**
|
|
146
|
-
* Attach an event listener to the passed event which will only fire once.
|
|
147
|
-
* @memberof Emitter
|
|
148
|
-
* @method once
|
|
149
|
-
* @param {String} event The name of the event to listen for.
|
|
150
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
151
|
-
*/
|
|
152
|
-
"string, function": function stringFunction(event, listener) {
|
|
153
|
-
var self = this,
|
|
154
|
-
fired = false,
|
|
155
|
-
internalCallback = function internalCallback() {
|
|
156
|
-
if (!fired) {
|
|
157
|
-
fired = true;
|
|
158
|
-
self.off(event, internalCallback);
|
|
159
|
-
listener.apply(self, arguments);
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
return this.on(event, internalCallback);
|
|
164
|
-
},
|
|
165
|
-
|
|
166
19
|
/**
|
|
167
20
|
* Attach an event listener to the passed event only if the passed
|
|
168
21
|
* id matches the document id for the event being fired.
|
|
169
|
-
* @
|
|
170
|
-
* @method
|
|
22
|
+
* @memberOf Emitter
|
|
23
|
+
* @method on
|
|
171
24
|
* @param {String} event The name of the event to listen for.
|
|
172
25
|
* @param {*} id The document id to match against.
|
|
173
26
|
* @param {Function} listener The method to call when the event is fired.
|
|
27
|
+
* @returns {Emitter} The emitter instance.
|
|
174
28
|
*/
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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;
|
|
187
52
|
}
|
|
188
|
-
}),
|
|
189
|
-
one: new Overload({
|
|
190
|
-
/**
|
|
191
|
-
* Attach an event listener to the passed event which will cancel all
|
|
192
|
-
* previous listeners and only fire this newest one.
|
|
193
|
-
* @memberof Emitter
|
|
194
|
-
* @method one
|
|
195
|
-
* @param {String} event The name of the event to listen for.
|
|
196
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
197
|
-
*/
|
|
198
|
-
"string, function": function stringFunction(event, listener) {
|
|
199
|
-
this.off(event);
|
|
200
|
-
return this.on(event, listener);
|
|
201
|
-
},
|
|
202
|
-
|
|
203
53
|
/**
|
|
204
54
|
* Attach an event listener to the passed event only if the passed
|
|
205
55
|
* id matches the document id for the event being fired.
|
|
206
|
-
* @
|
|
56
|
+
* @memberOf Emitter
|
|
207
57
|
* @method once
|
|
208
58
|
* @param {String} event The name of the event to listen for.
|
|
209
59
|
* @param {*} id The document id to match against.
|
|
210
60
|
* @param {Function} listener The method to call when the event is fired.
|
|
61
|
+
* @returns {Emitter} The emitter instance.
|
|
211
62
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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);
|
|
215
73
|
}
|
|
216
|
-
}),
|
|
217
|
-
off: new Overload({
|
|
218
|
-
/**
|
|
219
|
-
* Cancels all event listeners for the passed event.
|
|
220
|
-
* @memberof Emitter
|
|
221
|
-
* @method off
|
|
222
|
-
* @param {String} event The name of the event.
|
|
223
|
-
* @returns {*}
|
|
224
|
-
*/
|
|
225
|
-
"string": function string(event) {
|
|
226
|
-
var self = this;
|
|
227
|
-
|
|
228
|
-
if (this._emitting) {
|
|
229
|
-
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
230
|
-
|
|
231
|
-
this._eventRemovalQueue.push(function () {
|
|
232
|
-
self.off(event);
|
|
233
|
-
});
|
|
234
|
-
} else {
|
|
235
|
-
if (this._listeners && this._listeners[event]) {
|
|
236
|
-
delete this._listeners[event];
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return this;
|
|
241
|
-
},
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Cancels the event listener for the passed event and listener function.
|
|
245
|
-
* @memberof Emitter
|
|
246
|
-
* @method off
|
|
247
|
-
* @param {String} event The event to cancel listener for.
|
|
248
|
-
* @param {Function} listener The event listener function used in the on()
|
|
249
|
-
* or once() call to cancel.
|
|
250
|
-
* @returns {*}
|
|
251
|
-
*/
|
|
252
|
-
"string, function": function stringFunction(event, listener) {
|
|
253
|
-
var self = this,
|
|
254
|
-
arr,
|
|
255
|
-
index;
|
|
256
|
-
|
|
257
|
-
if (this._emitting) {
|
|
258
|
-
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
259
|
-
|
|
260
|
-
this._eventRemovalQueue.push(function () {
|
|
261
|
-
self.off(event, listener);
|
|
262
|
-
});
|
|
263
|
-
} else {
|
|
264
|
-
if (typeof listener === "string") {
|
|
265
|
-
if (this._listeners && this._listeners[event] && this._listeners[event][listener]) {
|
|
266
|
-
delete this._listeners[event][listener];
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
if (this._listeners && this._listeners[event]) {
|
|
270
|
-
arr = this._listeners[event]["*"] || [];
|
|
271
|
-
index = arr.indexOf(listener);
|
|
272
|
-
|
|
273
|
-
if (index > -1) {
|
|
274
|
-
arr.splice(index, 1);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return this;
|
|
281
|
-
},
|
|
282
|
-
|
|
283
74
|
/**
|
|
284
75
|
* Cancels an event listener based on an event name, id and listener function.
|
|
285
|
-
* @
|
|
76
|
+
* @memberOf Emitter
|
|
286
77
|
* @method off
|
|
287
78
|
* @param {String} event The event to cancel listener for.
|
|
288
79
|
* @param {String} id The ID of the event to cancel listening for.
|
|
289
80
|
* @param {Function} listener The event listener function used in the on()
|
|
290
81
|
* or once() call to cancel.
|
|
82
|
+
* @returns {Emitter} The emitter instance.
|
|
291
83
|
*/
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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) {
|
|
307
108
|
arr.splice(index, 1);
|
|
308
|
-
}
|
|
309
109
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
* @memberof Emitter
|
|
316
|
-
* @method off
|
|
317
|
-
* @param {String} event The event name to cancel listeners for.
|
|
318
|
-
* @param {*} id The ID to cancel all listeners for.
|
|
319
|
-
*/
|
|
320
|
-
"string, *": function string(event, id) {
|
|
321
|
-
var self = this;
|
|
322
|
-
|
|
323
|
-
if (this._emitting) {
|
|
324
|
-
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
325
|
-
|
|
326
|
-
this._eventRemovalQueue.push(function () {
|
|
327
|
-
self.off(event, id);
|
|
328
|
-
});
|
|
329
|
-
} else {
|
|
330
|
-
if (this._listeners && this._listeners[event] && this._listeners[event][id]) {
|
|
331
|
-
// Kill all listeners for this event id
|
|
332
|
-
delete this._listeners[event][id];
|
|
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]);
|
|
333
115
|
}
|
|
334
|
-
|
|
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]);
|
|
335
144
|
}
|
|
336
|
-
}),
|
|
337
|
-
emit: new Overload({
|
|
338
|
-
/**
|
|
339
|
-
* Emit an event.
|
|
340
|
-
* @memberof Emitter
|
|
341
|
-
* @method emit
|
|
342
|
-
* @param {String} event The event to emit.
|
|
343
|
-
* @returns {*}
|
|
344
|
-
*/
|
|
345
|
-
"string": function string(event) {
|
|
346
|
-
// Fire global listeners
|
|
347
|
-
return this.$main(event);
|
|
348
|
-
},
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Emit an event with data.
|
|
352
|
-
* @memberof Emitter
|
|
353
|
-
* @method emit
|
|
354
|
-
* @param {String} event The event to emit.
|
|
355
|
-
* @param {*} data Data to emit with the event.
|
|
356
|
-
* @returns {*}
|
|
357
|
-
*/
|
|
358
|
-
"string, ...": function string(event, data) {
|
|
359
|
-
// Fire global listeners first
|
|
360
|
-
this.$main.apply(this, arguments);
|
|
361
|
-
return this;
|
|
362
|
-
},
|
|
363
|
-
|
|
364
145
|
/**
|
|
365
146
|
* Handles emitting events, is an internal method not called directly.
|
|
366
147
|
* @param {String} event The name of the event to emit.
|
|
367
148
|
* @param {*} data The data to emit with the event.
|
|
368
|
-
* @returns {
|
|
149
|
+
* @returns {Emitter} The emitter instance.
|
|
369
150
|
* @private
|
|
370
151
|
*/
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
tmpFunc = arr[arrIndex];
|
|
386
|
-
|
|
387
|
-
if (typeof tmpFunc === "function") {
|
|
388
|
-
tmpFunc.apply(this, Array.prototype.slice.call(arguments, 1));
|
|
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
|
+
}
|
|
389
166
|
}
|
|
390
|
-
}
|
|
391
167
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
this._processRemovalQueue();
|
|
397
|
-
|
|
398
|
-
return this;
|
|
168
|
+
this._emitting = false;
|
|
169
|
+
this._processRemovalQueue();
|
|
170
|
+
return this;
|
|
399
171
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
// Fire global listeners first
|
|
410
|
-
this.$main.apply(this, arguments);
|
|
411
|
-
return this;
|
|
412
|
-
},
|
|
413
|
-
"$main": function $main(event, id, data) {
|
|
414
|
-
this._listeners = this._listeners || {};
|
|
415
|
-
this._emitting = true;
|
|
416
|
-
|
|
417
|
-
if (this._listeners[event]) {
|
|
418
|
-
var arrIndex, arrCount, tmpFunc, arr; // Handle global emit
|
|
419
|
-
|
|
420
|
-
if (this._listeners[event]["*"]) {
|
|
421
|
-
arr = this._listeners[event]["*"];
|
|
422
|
-
arrCount = arr.length;
|
|
423
|
-
|
|
424
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
425
|
-
// Check we have a function to execute
|
|
426
|
-
tmpFunc = arr[arrIndex];
|
|
427
|
-
|
|
428
|
-
if (typeof tmpFunc === "function") {
|
|
429
|
-
tmpFunc.apply(this, Array.prototype.slice.call(arguments, 2));
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
} // Handle id emit
|
|
433
|
-
|
|
434
|
-
|
|
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
|
|
435
181
|
if (this._listeners[event][id]) {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
tmpFunc.apply(this, Array.prototype.slice.call(arguments, 2));
|
|
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
|
+
}
|
|
445
190
|
}
|
|
446
|
-
}
|
|
447
191
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
+
}
|
|
203
|
+
}
|
|
204
|
+
this._emitting = false;
|
|
205
|
+
this._processRemovalQueue();
|
|
206
|
+
return this;
|
|
455
207
|
}
|
|
456
|
-
}),
|
|
457
|
-
emitStatic: new Overload({
|
|
458
208
|
/**
|
|
459
|
-
*
|
|
460
|
-
* is
|
|
461
|
-
* @memberof Emitter
|
|
462
|
-
* @method emitStatic
|
|
463
|
-
* @param {String} event The event to emit.
|
|
464
|
-
* @returns {*}
|
|
465
|
-
*/
|
|
466
|
-
"string": function string(event) {
|
|
467
|
-
// Fire global listeners
|
|
468
|
-
return this.$main(event);
|
|
469
|
-
},
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Emit an event with data that will fire on listeners even when the listener
|
|
473
|
-
* is registered AFTER the event has been emitted.
|
|
474
|
-
* @memberof Emitter
|
|
475
|
-
* @method emitStatic
|
|
476
|
-
* @param {String} event The event to emit.
|
|
477
|
-
* @param {*} data Data to emit with the event.
|
|
478
|
-
* @returns {*}
|
|
479
|
-
*/
|
|
480
|
-
"string, ...": function string(event, data) {
|
|
481
|
-
// Fire global listeners first
|
|
482
|
-
this.$main.apply(this, arguments);
|
|
483
|
-
return this;
|
|
484
|
-
},
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
* Handles emitting events, is an internal method not called directly.
|
|
209
|
+
* Creates a static event emitter that is emitted whenever an
|
|
210
|
+
* event listener is created with the same name.
|
|
488
211
|
* @param {String} event The name of the event to emit.
|
|
489
212
|
* @param {*} data The data to emit with the event.
|
|
490
|
-
* @returns {
|
|
491
|
-
* @private
|
|
213
|
+
* @returns {Emitter} The emitter instance.
|
|
492
214
|
*/
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
arrCount = arr.length;
|
|
504
|
-
|
|
505
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
506
|
-
// Check we have a function to execute
|
|
507
|
-
tmpFunc = arr[arrIndex];
|
|
508
|
-
|
|
509
|
-
if (typeof tmpFunc === "function") {
|
|
510
|
-
tmpFunc.apply(this, Array.prototype.slice.call(arguments, 1));
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
this._emitting = false;
|
|
517
|
-
this._emitters = this._emitters || {};
|
|
518
|
-
this._emitters[event] = this._emitters[event] || [];
|
|
519
|
-
|
|
520
|
-
this._emitters[event].push({
|
|
521
|
-
id: "*",
|
|
522
|
-
args: Array.prototype.slice.call(arguments, 1)
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
this._processRemovalQueue();
|
|
526
|
-
|
|
527
|
-
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;
|
|
528
225
|
}
|
|
529
|
-
}),
|
|
530
|
-
emitStaticId: new Overload({
|
|
531
|
-
/**
|
|
532
|
-
* Require an id to emit.
|
|
533
|
-
* @memberof Emitter
|
|
534
|
-
* @method emitStaticId
|
|
535
|
-
* @param event
|
|
536
|
-
*/
|
|
537
|
-
"string": function string(event) {
|
|
538
|
-
throw "Missing id from emitId call!";
|
|
539
|
-
},
|
|
540
|
-
|
|
541
226
|
/**
|
|
542
|
-
*
|
|
543
|
-
* is
|
|
544
|
-
* @
|
|
545
|
-
* @method emitStaticId
|
|
546
|
-
* @param {String} event The event to emit.
|
|
227
|
+
* Creates a static event emitter that is emitted whenever an
|
|
228
|
+
* event listener is created with the same name and id.
|
|
229
|
+
* @param {String} event The name of the event to emit.
|
|
547
230
|
* @param {String} id The id of the event to emit.
|
|
548
|
-
* @
|
|
231
|
+
* @param {*} data The data to emit with the event.
|
|
232
|
+
* @returns {Emitter} The emitter instance.
|
|
549
233
|
*/
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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;
|
|
245
|
+
}
|
|
554
246
|
/**
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* @
|
|
558
|
-
* @
|
|
559
|
-
* @param {String} event The event to emit.
|
|
560
|
-
* @param {String} id The id of the event to emit.
|
|
561
|
-
* @param {*=} data The data to emit with the event.
|
|
562
|
-
* @returns {*}
|
|
247
|
+
* Handles removing emitters, is an internal method not called directly.
|
|
248
|
+
* @param {String} event The event to remove static emitter for.
|
|
249
|
+
* @returns {Emitter} The emitter instance.
|
|
250
|
+
* @private
|
|
563
251
|
*/
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
}
|
|
569
|
-
|
|
252
|
+
cancelStatic(event) {
|
|
253
|
+
this._emitters = this._emitters || {};
|
|
254
|
+
this._emitters[event] = [];
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
570
257
|
/**
|
|
571
|
-
*
|
|
572
|
-
* @
|
|
573
|
-
* @
|
|
574
|
-
* @param {
|
|
575
|
-
* @returns {
|
|
576
|
-
*
|
|
258
|
+
* Checks if an event has any event listeners or not.
|
|
259
|
+
* @memberOf Emitter
|
|
260
|
+
* @method willEmit
|
|
261
|
+
* @param {String} event The name of the event to check for.
|
|
262
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
263
|
+
* the event. False if none are found.
|
|
577
264
|
*/
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
arr = this._listeners[event]["*"];
|
|
587
|
-
arrCount = arr.length;
|
|
588
|
-
|
|
589
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
265
|
+
willEmit(event) {
|
|
266
|
+
const id = "*";
|
|
267
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
const arr = this._listeners[event][id];
|
|
271
|
+
const arrCount = arr.length;
|
|
272
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
590
273
|
// Check we have a function to execute
|
|
591
|
-
tmpFunc = arr[arrIndex];
|
|
592
|
-
|
|
274
|
+
const tmpFunc = arr[arrIndex];
|
|
593
275
|
if (typeof tmpFunc === "function") {
|
|
594
|
-
|
|
276
|
+
return true;
|
|
595
277
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Checks if an event has any event listeners or not based on the passed id.
|
|
283
|
+
* @memberOf Emitter
|
|
284
|
+
* @method willEmitId
|
|
285
|
+
* @param {String} event The name of the event to check for.
|
|
286
|
+
* @param {String} id The event ID to check for.
|
|
287
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
288
|
+
* the event. False if none are found.
|
|
289
|
+
*/
|
|
290
|
+
willEmitId(event, id) {
|
|
291
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
// Handle id emit
|
|
600
295
|
if (this._listeners[event][id]) {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
tmpFunc.apply(this, Array.prototype.slice.call(arguments, 2));
|
|
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
|
+
}
|
|
610
304
|
}
|
|
611
|
-
}
|
|
612
305
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return this;
|
|
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
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return false;
|
|
627
319
|
}
|
|
628
|
-
}),
|
|
629
|
-
cancelStatic: new Overload({
|
|
630
320
|
/**
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
321
|
+
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
322
|
+
* events of the same type that occur within 100 milliseconds of a previous
|
|
323
|
+
* one will all be wrapped into a single emit rather than emitting tons of
|
|
324
|
+
* events for the same eventName. Only the data from the last de-bounced event
|
|
325
|
+
* will be emitted.
|
|
326
|
+
* @memberOf Emitter
|
|
327
|
+
* @method deferEmit
|
|
328
|
+
* @param {String} eventName The name of the event to emit.
|
|
329
|
+
* @param {*=} data Optional data to emit with the event.
|
|
330
|
+
* @returns {Emitter} The emitter instance.
|
|
636
331
|
*/
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
332
|
+
deferEmit(eventName, ...data) {
|
|
333
|
+
if (this._noEmitDefer) {
|
|
334
|
+
this.emit(eventName, ...data);
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
337
|
+
// Check for an existing timeout
|
|
338
|
+
this._deferTimeout = this._deferTimeout || {};
|
|
339
|
+
if (this._deferTimeout[eventName]) {
|
|
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;
|
|
347
|
+
}
|
|
642
348
|
/**
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
349
|
+
* If events are cleared with the off() method while the event emitter is
|
|
350
|
+
* actively processing any events then the off() calls get added to a
|
|
351
|
+
* queue to be executed after the event emitter is finished. This stops
|
|
352
|
+
* errors that might occur by potentially modifying the event queue while
|
|
353
|
+
* the emitter is running through them. This method is called after the
|
|
354
|
+
* event emitter is finished processing.
|
|
646
355
|
* @private
|
|
356
|
+
* @returns {undefined}
|
|
647
357
|
*/
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
return this;
|
|
652
|
-
}
|
|
653
|
-
}),
|
|
654
|
-
|
|
655
|
-
/**
|
|
656
|
-
* Checks if an event has any event listeners or not.
|
|
657
|
-
* @memberof Emitter
|
|
658
|
-
* @method willEmit
|
|
659
|
-
* @param {String} event The name of the event to check for.
|
|
660
|
-
* @returns {boolean} True if one or more event listeners are registered for
|
|
661
|
-
* the event. False if none are found.
|
|
662
|
-
*/
|
|
663
|
-
willEmit: function willEmit(event) {
|
|
664
|
-
var id = "*";
|
|
665
|
-
|
|
666
|
-
if (this._listeners && this._listeners[event]) {
|
|
667
|
-
var arrIndex, arrCount, tmpFunc, arr; // Handle global emit
|
|
668
|
-
|
|
669
|
-
if (this._listeners[event][id]) {
|
|
670
|
-
arr = this._listeners[event][id];
|
|
671
|
-
arrCount = arr.length;
|
|
672
|
-
|
|
673
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
674
|
-
// Check we have a function to execute
|
|
675
|
-
tmpFunc = arr[arrIndex];
|
|
676
|
-
|
|
677
|
-
if (typeof tmpFunc === "function") {
|
|
678
|
-
return true;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
return false;
|
|
685
|
-
},
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Checks if an event has any event listeners or not based on the passed id.
|
|
689
|
-
* @memberof Emitter
|
|
690
|
-
* @method willEmitId
|
|
691
|
-
* @param {String} event The name of the event to check for.
|
|
692
|
-
* @param {String} id The event ID to check for.
|
|
693
|
-
* @returns {boolean} True if one or more event listeners are registered for
|
|
694
|
-
* the event. False if none are found.
|
|
695
|
-
*/
|
|
696
|
-
willEmitId: function willEmitId(event, id) {
|
|
697
|
-
if (this._listeners && this._listeners[event]) {
|
|
698
|
-
var arrIndex, arrCount, tmpFunc, arr; // Handle global emit
|
|
699
|
-
|
|
700
|
-
if (this._listeners[event]["*"]) {
|
|
701
|
-
arr = this._listeners[event]["*"];
|
|
702
|
-
arrCount = arr.length;
|
|
703
|
-
|
|
704
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
705
|
-
// Check we have a function to execute
|
|
706
|
-
tmpFunc = arr[arrIndex];
|
|
707
|
-
|
|
708
|
-
if (typeof tmpFunc === "function") {
|
|
709
|
-
return true;
|
|
710
|
-
}
|
|
358
|
+
_processRemovalQueue() {
|
|
359
|
+
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
360
|
+
return;
|
|
711
361
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
if (this._listeners[event][id]) {
|
|
716
|
-
arr = this._listeners[event][id];
|
|
717
|
-
arrCount = arr.length;
|
|
718
|
-
|
|
719
|
-
for (arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
720
|
-
// Check we have a function to execute
|
|
721
|
-
tmpFunc = arr[arrIndex];
|
|
722
|
-
|
|
723
|
-
if (typeof tmpFunc === "function") {
|
|
724
|
-
return true;
|
|
725
|
-
}
|
|
362
|
+
// Execute each removal call
|
|
363
|
+
for (let i = 0; i < this._eventRemovalQueue.length; i++) {
|
|
364
|
+
this._eventRemovalQueue[i]();
|
|
726
365
|
}
|
|
727
|
-
|
|
366
|
+
// Clear the removal queue
|
|
367
|
+
this._eventRemovalQueue = [];
|
|
728
368
|
}
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
* queue to be executed after the event emitter is finished. This stops
|
|
737
|
-
* errors that might occur by potentially modifying the event queue while
|
|
738
|
-
* the emitter is running through them. This method is called after the
|
|
739
|
-
* event emitter is finished processing.
|
|
740
|
-
* @private
|
|
741
|
-
*/
|
|
742
|
-
_processRemovalQueue: function _processRemovalQueue() {
|
|
743
|
-
var i;
|
|
744
|
-
|
|
745
|
-
if (this._eventRemovalQueue && this._eventRemovalQueue.length) {
|
|
746
|
-
// Execute each removal call
|
|
747
|
-
for (i = 0; i < this._eventRemovalQueue.length; i++) {
|
|
748
|
-
this._eventRemovalQueue[i]();
|
|
749
|
-
} // Clear the removal queue
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
this._eventRemovalQueue = [];
|
|
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;
|
|
753
376
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
758
|
-
* events of the same type that occur within 100 milliseconds of a previous
|
|
759
|
-
* one will all be wrapped into a single emit rather than emitting tons of
|
|
760
|
-
* events for lots of chained inserts etc. Only the data from the last
|
|
761
|
-
* de-bounced event will be emitted.
|
|
762
|
-
* @memberof Emitter
|
|
763
|
-
* @method deferEmit
|
|
764
|
-
* @param {String} eventName The name of the event to emit.
|
|
765
|
-
* @param {*=} data Optional data to emit with the event.
|
|
766
|
-
*/
|
|
767
|
-
deferEmit: function deferEmit(eventName, data) {
|
|
768
|
-
var self = this,
|
|
769
|
-
args;
|
|
770
|
-
|
|
771
|
-
if (!this._noEmitDefer && (!this._db || this._db && !this._db._noEmitDefer)) {
|
|
772
|
-
args = arguments; // Check for an existing timeout
|
|
773
|
-
|
|
774
|
-
this._deferTimeout = this._deferTimeout || {};
|
|
775
|
-
|
|
776
|
-
if (this._deferTimeout[eventName]) {
|
|
777
|
-
clearTimeout(this._deferTimeout[eventName]);
|
|
778
|
-
} // Set a timeout
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
this._deferTimeout[eventName] = setTimeout(function () {
|
|
782
|
-
self.emit.apply(self, args);
|
|
783
|
-
}, 1);
|
|
784
|
-
} else {
|
|
785
|
-
this.emit.apply(this, arguments);
|
|
377
|
+
if (typeof obj === "boolean" && prototypeMode === undefined) {
|
|
378
|
+
prototypeMode = obj;
|
|
379
|
+
obj = {};
|
|
786
380
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
* @constructor
|
|
802
|
-
*/
|
|
803
|
-
|
|
804
|
-
var Emitter = function Emitter(obj, prototypeMode) {
|
|
805
|
-
var operateOnObject;
|
|
806
|
-
|
|
807
|
-
if (obj === undefined && prototypeMode === undefined) {
|
|
808
|
-
obj = {};
|
|
809
|
-
prototypeMode = false;
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
if (typeof obj === "boolean" && prototypeMode === undefined) {
|
|
813
|
-
prototypeMode = obj;
|
|
814
|
-
obj = {};
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
if (prototypeMode === undefined) {
|
|
818
|
-
prototypeMode = true;
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
if ((0, _typeof2.default)(obj) !== "object" && typeof obj !== "function") {
|
|
822
|
-
throw new Error("Cannot operate on a non-object / non-function passed as first argument!");
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
if (prototypeMode) {
|
|
826
|
-
if (obj.prototype === undefined) {
|
|
827
|
-
throw new Error("Cannot modify prototype of passed object, it has no prototype property! Was it instantiated with the new operator correctly?");
|
|
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;
|
|
828
395
|
}
|
|
829
|
-
|
|
830
|
-
operateOnObject =
|
|
831
|
-
|
|
832
|
-
operateOnObject =
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
operateOnObject.willEmitId = EventMethods.willEmitId;
|
|
848
|
-
operateOnObject._processRemovalQueue = EventMethods._processRemovalQueue;
|
|
849
|
-
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;
|
|
850
414
|
};
|
|
851
|
-
|
|
852
|
-
Emitter.prototype = EventMethods;
|
|
853
|
-
module.exports = Emitter;
|
|
415
|
+
exports.makeEmitter = makeEmitter;
|