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