@irrelon/emitter 5.0.0 → 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/.eslintignore +1 -3
- package/.eslintrc.json +35 -18
- package/.prettierignore +9 -0
- package/.prettierrc.yml +9 -0
- package/fixPaths.cjs +183 -0
- package/package.json +31 -24
- package/readme.md +16 -45
- package/src/Emitter.d.ts +190 -0
- package/src/Emitter.js +580 -0
- package/src/Emitter.ts +294 -247
- package/tests/Emitter.test.d.ts +1 -0
- package/tests/Emitter.test.js +267 -309
- package/tests/Emitter.test.ts +335 -0
- package/tsconfig.json +33 -12
- package/tsconfig.tsbuildinfo +1 -0
- package/.babelrc +0 -11
- package/dist/Emitter.d.ts +0 -205
- package/dist/Emitter.js +0 -415
package/dist/Emitter.js
DELETED
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
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);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Attach an event listener to the passed event only if the passed
|
|
21
|
-
* id matches the document id for the event being fired.
|
|
22
|
-
* @memberOf Emitter
|
|
23
|
-
* @method on
|
|
24
|
-
* @param {String} event The name of the event to listen for.
|
|
25
|
-
* @param {*} id The document id to match against.
|
|
26
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
27
|
-
* @returns {Emitter} The emitter instance.
|
|
28
|
-
*/
|
|
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;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Attach an event listener to the passed event only if the passed
|
|
55
|
-
* id matches the document id for the event being fired.
|
|
56
|
-
* @memberOf Emitter
|
|
57
|
-
* @method once
|
|
58
|
-
* @param {String} event The name of the event to listen for.
|
|
59
|
-
* @param {*} id The document id to match against.
|
|
60
|
-
* @param {Function} listener The method to call when the event is fired.
|
|
61
|
-
* @returns {Emitter} The emitter instance.
|
|
62
|
-
*/
|
|
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);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Cancels an event listener based on an event name, id and listener function.
|
|
76
|
-
* @memberOf Emitter
|
|
77
|
-
* @method off
|
|
78
|
-
* @param {String} event The event to cancel listener for.
|
|
79
|
-
* @param {String} id The ID of the event to cancel listening for.
|
|
80
|
-
* @param {Function} listener The event listener function used in the on()
|
|
81
|
-
* or once() call to cancel.
|
|
82
|
-
* @returns {Emitter} The emitter instance.
|
|
83
|
-
*/
|
|
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]);
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Handles emitting events, is an internal method not called directly.
|
|
147
|
-
* @param {String} event The name of the event to emit.
|
|
148
|
-
* @param {*} data The data to emit with the event.
|
|
149
|
-
* @returns {Emitter} The emitter instance.
|
|
150
|
-
* @private
|
|
151
|
-
*/
|
|
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
|
-
}
|
|
167
|
-
}
|
|
168
|
-
this._emitting = false;
|
|
169
|
-
this._processRemovalQueue();
|
|
170
|
-
return this;
|
|
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
|
-
}
|
|
191
|
-
}
|
|
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;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Creates a static event emitter that is emitted whenever an
|
|
210
|
-
* event listener is created with the same name.
|
|
211
|
-
* @param {String} event The name of the event to emit.
|
|
212
|
-
* @param {*} data The data to emit with the event.
|
|
213
|
-
* @returns {Emitter} The emitter instance.
|
|
214
|
-
*/
|
|
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;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
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.
|
|
230
|
-
* @param {String} id The id of the event to emit.
|
|
231
|
-
* @param {*} data The data to emit with the event.
|
|
232
|
-
* @returns {Emitter} The emitter instance.
|
|
233
|
-
*/
|
|
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
|
-
}
|
|
246
|
-
/**
|
|
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
|
|
251
|
-
*/
|
|
252
|
-
cancelStatic(event) {
|
|
253
|
-
this._emitters = this._emitters || {};
|
|
254
|
-
this._emitters[event] = [];
|
|
255
|
-
return this;
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
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.
|
|
264
|
-
*/
|
|
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++) {
|
|
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;
|
|
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
|
|
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
|
-
}
|
|
305
|
-
}
|
|
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;
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
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.
|
|
331
|
-
*/
|
|
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
|
-
}
|
|
348
|
-
/**
|
|
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.
|
|
355
|
-
* @private
|
|
356
|
-
* @returns {undefined}
|
|
357
|
-
*/
|
|
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 = [];
|
|
368
|
-
}
|
|
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;
|
|
395
|
-
}
|
|
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;
|
|
414
|
-
};
|
|
415
|
-
exports.makeEmitter = makeEmitter;
|