-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventEmitter.js
1107 lines (1002 loc) · 35.3 KB
/
EventEmitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* # EventEmitter
* Copyright(c) 2021 Stefano Balietti
* MIT Licensed
*
* Event emitter engine for `nodeGame`
*
* Keeps a register of events listeners.
*/
(function(exports, parent) {
"use strict";
// ## Global scope
var J = parent.JSUS,
NDDB = parent.NDDB,
GameStage = parent.GameStage;
exports.EventEmitter = EventEmitter;
exports.EventEmitterManager = EventEmitterManager;
/**
* ## EventEmitter constructor
*
* Creates a new instance of EventEmitter
*/
function EventEmitter(name, node) {
if ('string' !== typeof name) {
throw new TypeError('EventEmitter constructor: name must be ' +
'string. Found: ' + name);
}
this.node = node;
// ## Public properties
this.name = name;
/**
* ### EventEmitter.listeners
*
* Event listeners collection
*/
this.events = {};
/**
* ## EventEmitter.recordChanges
*
* If TRUE, keeps tracks of addition and deletion of listeners
*
* @see EventEmitter.changes
*/
this.recordChanges = false;
/**
* ## EventEmitter.changes
*
* If TRUE, keeps tracks of addition and deletion of listeners
*
* @see EventEmitter.recordChanges
*/
this.changes = {
added: [],
removed: []
};
/**
* ## EventEmitter.labels
*
* List of labels associated to an event listener
*
* @see EventEmitter.on
*/
this.labels = {};
/**
* ### EventEmitter.history
*
* Database of emitted events
*
* @experimental
*
* @see NDDB
* @see EventEmitter.EventHistory
* @see EventEmitter.store
*/
this.history = new EventHistory(this.node);
}
// ## EventEmitter methods
/**
* ### EventEmitter.on
*
* Registers a callback function for an event (event listener)
*
* @param {string} type The event name
* @param {function} listener The function to emit
* @param {string|number} label Optional. If set, it flags the listener with
* the property .__ngid = label. It will be then possible to remove
* the listener using the label
*
* @see EventEmitter.off
*/
EventEmitter.prototype.on = function(type, listener, label) {
if ('string' !== typeof type || type === '') {
throw new TypeError('EventEmitter.on: type must be a non-empty ' +
'string. Found: ' + type);
}
if ('function' !== typeof listener) {
throw new TypeError('EventEmitter.on: listener must be function.' +
'Found: ' + listener);
}
if (label) checkAndAddLabel(this, listener, label, 'on');
if (!this.events[type]) {
// Optimize the case of one listener.
// Don't need the extra array object.
this.events[type] = listener;
}
else if (typeof this.events[type] === 'object') {
// If we've already got an array, just append.
this.events[type].push(listener);
}
else {
// Adding the second element, need to change to array.
this.events[type] = [this.events[type], listener];
}
// Storing changes if necessary.
if (this.recordChanges) {
this.changes.added.push({type: type, listener: listener});
}
this.node.silly(this.name + '.on: added: ' + type);
};
/**
* ### EventEmitter.once
*
* Registers an event listener that will be removed after its first call
*
* @param {string} event The name of the event
* @param {function} listener The callback function
* @param {string|number} label Optional. If set, it flags the listener with
* the property .__ngid = label. It will be then possible to remove
* the listener using the label
*
* @see EventEmitter.on
* @see EventEmitter.off
*/
EventEmitter.prototype.once = function(type, listener, label) {
var that = this;
if (!label) label = J.uniqueKey(this.labels, type);
function g() {
var i, len, args;
args = [];
i = -1, len = arguments.length;
for ( ; ++i < len ; ) {
args[i] = arguments[i];
}
that.off(type, label);
listener.apply(that.node.game, args);
}
this.on(type, g, label);
};
/**
* ### EventEmitter.emit
*
* Fires all the listeners associated with an event
*
* The first parameter is the name of the event as _string_,
* followed by any number of parameters that will be passed to the
* callback.
*
* Return values of each callback are aggregated and returned as
* an array. If the array contains less than 2 elements, only
* element or _undefined_ is returned.
*
* @return {mixed} The return value of the callback/s
*/
EventEmitter.prototype.emit = function() {
var handler, len, args, i, listeners, type, ctx, node;
var res, tmpRes;
type = arguments[0];
handler = this.events[type];
if ('undefined' === typeof handler) return;
node = this.node;
ctx = node.game;
// Useful for debugging.
if (this.node.conf.events && this.node.conf.events.dumpEvents) {
this.node.info('F: ' + this.name + ': ' + type);
}
if ('function' === typeof handler) {
switch (arguments.length) {
// fast cases
case 1:
res = handler.call(ctx);
break;
case 2:
res = handler.call(ctx, arguments[1]);
break;
case 3:
res = handler.call(ctx, arguments[1], arguments[2]);
break;
case 4:
res = handler.call(ctx, arguments[1], arguments[2],
arguments[3]);
break;
default:
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++) {
args[i - 1] = arguments[i];
}
res = handler.apply(ctx, args);
}
}
else if (handler && 'object' === typeof handler) {
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++) {
args[i - 1] = arguments[i];
}
listeners = handler.slice();
len = listeners.length;
// If more than one event listener is registered,
// we will return an array.
res = [];
for (i = 0; i < len; i++) {
tmpRes = listeners[i].apply(node.game, args);
if ('undefined' !== typeof tmpRes)
res.push(tmpRes);
}
// If less than 2 listeners returned a value, compact the result.
if (!res.length) res = undefined;
else if (res.length === 1) res = res[0];
}
// Log the event into node.history object, if present.
if (node.conf && node.conf.events &&
node.conf.events.history) {
len = arguments.length;
args = new Array(len);
for (i = -1 ; ++i < len ; ) {
args[i] = arguments[i];
}
this.history.insert({
stage: node.game.getCurrentGameStage(),
args: args
});
}
return res;
};
/**
* ### EventEmitter.emitAsync
*
* Fires all the listeners associated with an event asynchronously
*
* The event must be already existing, cannot be added after the call.
*
* Unlike normal emit, it does not return a value.
*
* @see EventEmitter.emit
*/
EventEmitter.prototype.emitAsync = function() {
var that, len, args, i;
var arg1, arg2, arg3;
arg1 = arguments[0];
if (!this.events[arg1]) return;
len = arguments.length;
that = this;
// The arguments object must not be passed or leaked anywhere.
// Therefore, we recreate an args array here. We have a different
// timeout in a different branch for optimization.
switch(len) {
case 1:
setTimeout(function() { that.emit(arg1); }, 0);
break;
case 2:
arg2 = arguments[1];
setTimeout(function() { that.emit(arg1, arg2); }, 0);
break;
case 3:
arg2 = arguments[1], arg3 = arguments[2];
setTimeout(function() { that.emit(arg1, arg2, arg3); }, 0);
break;
default:
args = new Array(len);
for (i = -1 ; ++i < len ; ) {
args[i] = arguments[i];
}
setTimeout(function() { that.emit.apply(that, args); }, 0);
}
};
/**
* ### EventEmitter.off || remove
*
* Deregisters one or multiple event listeners
*
* If the listener is specified as a string, the first function
* with either the name or the label equal to listener will be removed.
*
* @param {string} type The event name
* @param {mixed} listener Optional. The specific function
* to deregister, its name, the label as specified during insertion,
* or undefined to remove all listeners
*
* @return {array} The array of removed listener/s
*
* @see node.on
*/
EventEmitter.prototype.remove = EventEmitter.prototype.off =
function(type, listener) {
var listeners, len, i, node, found, oneFound, removed;
removed = [];
node = this.node;
if ('string' !== typeof type) {
throw new TypeError('EventEmitter.remove (' + this.name +
'): type must be string. Found: ' + type);
}
if (listener &&
('function' !== typeof listener && 'string' !== typeof listener)) {
throw new TypeError('EventEmitter.remove (' + this.name +
'): listener must be function, string, or ' +
'undefined. Found: ' + listener);
}
if ('string' === typeof listener && listener.trim() === '') {
throw new Error('EventEmitter.remove (' + this.name + '): ' +
'listener cannot be an empty string');
}
if (this.events[type]) {
if (!listener) {
oneFound = true;
i = -1, len = this.events[type].length;
for ( ; ++i < len ; ) {
removed.push(this.events[type][i]);
}
// Null instead of delete for optimization.
this.events[type] = null;
}
else {
// Handling multiple cases:
// this.events[type] can be array or function,
// and listener can be function or string.
if ('function' === typeof this.events[type]) {
if ('function' === typeof listener) {
if (listener == this.events[type]) oneFound = true;
}
else {
// String.
if (listener === this.events[type].__ngid) {
this.labels[listener] = null;
oneFound = true;
}
else if (listener === J.funcName(this.events[type])) {
oneFound = true;
}
}
if (oneFound) {
removed.push(this.events[type]);
// Null instead of delete for optimization.
this.events[type] = null;
}
}
// this.events[type] is an array.
else {
listeners = this.events[type];
len = listeners.length;
for (i = 0; i < len; i++) {
found = false;
if ('function' === typeof listener) {
if (listeners[i] == listener) found = true;
}
else {
// String.
if (listener === listeners[i].__ngid) {
this.labels[listener] = null;
found = true;
}
else if (listener === J.funcName(listeners[i])) {
found = true;
}
}
if (found) {
oneFound = true;
removed.push(listeners[i]);
if (len === 1) {
// Null instead of delete for optimization.
this.events[type] = null;
}
else {
listeners.splice(i, 1);
// Update indexes,
// because array size has changed.
len--;
i--;
}
}
}
}
}
}
if (oneFound) {
// Storing changes if necessary.
if (this.recordChanges) {
i = -1, len = removed.length;
for ( ; ++i < len ; ) {
this.changes.removed.push({
type: type,
listener: removed[i]
});
}
}
node.silly('ee.' + this.name + ' removed listener: ' + type);
}
else {
node.warn('EventEmitter.remove (' + this.name + '): requested ' +
'listener was not found for event ' + type);
}
return removed;
};
/**
* ### EventEmitter.clear
*
* Removes all registered event listeners
*
* Clears the labels and store changes, if requested
*
* @see EventEmitter.labels
* @see EventEmitter.setRecordChanges
*/
EventEmitter.prototype.clear = function() {
var event, i, len;
if (this.recordChanges) {
for (event in this.events) {
if ('function' === typeof this.events[event]) {
this.changes.removed.push({
type: event,
listener: this.events[event]
});
}
else if (J.isArray(this.events[event])) {
i = -1, len = this.events[event].length;
for ( ; ++i < len ; ) {
this.changes.removed.push({
type: event,
listener: this.events[event][i]
});
}
}
}
}
this.events = {};
this.labels = {};
};
/**
* ### EventEmitter.size
*
* Returns the number of registered events / event listeners
*
* @param {mixed} Optional. Modifier controlling the return value
*
* @return {number} Depending on the value of the modifier returns
* the total number of:
*
* - Not set: events registered
* - String: event listeners for the specified event
* - true: event listeners for all events
*/
EventEmitter.prototype.size = function(mod) {
var count;
count = 0;
if (!mod) {
for (mod in this.events) {
if (this.events.hasOwnProperty(mod)) {
// Not null (delete events).
if (this.events[mod]) count++;
}
}
return count;
}
if ('string' === typeof mod) {
if (!this.events[mod]) return 0;
if ('function' === typeof this.events[mod]) return 1;
return this.events[mod].length;
}
for (mod in this.events) {
count += this.size(mod);
}
return count;
};
/**
* ### EventEmitter.printAll
*
* Prints to console all the registered functions
*
* @return {number} The total number of registered functions
*/
EventEmitter.prototype.printAll = function() {
var i, len, totalLen, str;
totalLen = 0, str = '';
for (i in this.events) {
if (this.events.hasOwnProperty(i)) {
len = this.size(i);
str += i + ': ' + len + "\n";
totalLen += len;
}
}
console.log('[' + this.name + '] ' + totalLen + ' listener/s.');
if (str) console.log(str);
return totalLen;
};
/**
* ### EventEmitter.getChanges
*
* Returns the list of added and removed event listeners
*
* @param {boolean} clear Optional. If TRUE, the list of current changes
* is cleared. Default FALSE
*
* @return {object} Object containing list of additions and deletions,
* or null if no changes have been recorded
*/
EventEmitter.prototype.getChanges = function(clear) {
var changes;
if (this.changes.added.length || this.changes.removed.length) {
changes = this.changes;
if (clear) {
this.changes = {
added: [],
removed: []
};
}
}
return changes;
};
/**
* ### EventEmitter.setRecordChanges
*
* Sets the value of recordChanges and returns it
*
* If called with undefined, just returns current value.
*
* @param {boolean} record If TRUE, starts recording changes. Default FALSE
*
* @return {boolean} Current value of recordChanges
*
* @see EventEmitter.recordChanges
*/
EventEmitter.prototype.setRecordChanges = function(record) {
if ('boolean' === typeof record) this.recordChanges = record;
else if ('undefined' !== typeof record) {
throw new TypeError('EventEmitter.setRecordChanged: record must ' +
'be boolean or undefined. Found: ' + record);
}
return this.recordChanges;
};
// ### Helper functions
/**
* #### checkAndAddLabel
*
* If label is valid, adds it to the labels object and marks the listener
*
* @param {EventEmitter} that The instance of event emitter
* @param {function} listener The listener function
* @param {string|number} label The label to check
* @param {string} method The invoking method (on or once)
*/
function checkAndAddLabel(that, listener, label, method) {
if ('string' === typeof label || 'number' === typeof label) {
if (that.labels[label]) {
throw new Error('EventEmitter.' + method +
': label is not unique: ' + label);
}
that.labels[label] = true;
listener.__ngid = '' + label;
}
else {
throw new TypeError('EventEmitter.' + method + ': label must be ' +
'string or undefined. Found: ' + label);
}
}
/**
* ## EventEmitterManager constructor
*
* @param {NodeGameClient} node A reference to the node object
*/
function EventEmitterManager(node) {
this.node = node;
this.ee = {};
this.createEE('ng');
this.createEE('game');
this.createEE('stage');
this.createEE('step');
}
// ## EventEmitterManager methods
/**
* ### EventEmitterManager.createEE
*
* Creates and registers an event emitter
*
* A double reference is added to _this.ee_ and to _this_.
*
* @param {string} name The name of the event emitter
*
* @return {EventEmitter} A reference to the newly created event emitter
*
* @see EventEmitter constructor
*/
EventEmitterManager.prototype.createEE = function(name) {
this.ee[name] = new EventEmitter(name, this.node);
this[name] = this.ee[name];
return this.ee[name];
};
/**
* ### EventEmitterManager.destroyEE
*
* Removes an existing event emitter
*
* @param {string} name The name of the event emitter
*
* @return {boolean} TRUE, on success
*
* @see EventEmitterManager.createEE
*/
EventEmitterManager.prototype.destroyEE = function(name) {
if ('string' !== typeof name) {
throw new TypeError('EventEmitterManager.destroyEE: name must be ' +
'string. Found: ' + name);
}
if (!this.ee[name]) return false;
delete this[name];
delete this.ee[name];
return true;
};
/**
* ### EventEmitterManager.clear
*
* Removes all registered event listeners from all event emitters
*/
EventEmitterManager.prototype.clear = function() {
this.ng.clear();
this.game.clear();
this.stage.clear();
this.step.clear();
};
/**
* ### EventEmitterManager.emit
*
* Emits an event on all registered event emitters
*
* Accepts a variable number of input parameters.
*
* @param {string} eventName The name of the event
*
* @return {mixed} The values returned by all fired event listeners
*
* @see EventEmitterManager.emit
*/
EventEmitterManager.prototype.emit = function(eventName) {
var i, tmpRes, res, args, len, ees;
if ('string' !== typeof eventName) {
throw new TypeError(
'EventEmitterManager.emit: eventName must be string. Found: ' +
eventName);
}
res = [];
len = arguments.length;
// The scope might `node` if this method is invoked from `node.emit`.
ees = this.ee || this.events.ee;
// The arguments object must not be passed or leaked anywhere.
switch(len) {
case 1:
tmpRes = ees.ng.emit(eventName);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.game.emit(eventName);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.stage.emit(eventName);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.step.emit(eventName);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
break;
case 2:
tmpRes = ees.ng.emit(eventName, arguments[1]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.game.emit(eventName, arguments[1]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.stage.emit(eventName, arguments[1]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.step.emit(eventName, arguments[1]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
break;
case 3:
tmpRes = ees.ng.emit(eventName, arguments[1], arguments[2]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.game.emit(eventName, arguments[1], arguments[2]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.stage.emit(eventName, arguments[1], arguments[2]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.step.emit(eventName, arguments[1], arguments[2]);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
break;
default:
args = new Array(len);
for (i = -1 ; ++i < len ; ) {
args[i] = arguments[i];
}
tmpRes = ees.ng.emit.apply(ees.ng, args);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.game.emit.apply(ees.game, args);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.stage.emit.apply(ees.stage, args);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
tmpRes = ees.step.emit.apply(ees.step, args);
if ('undefined' !== typeof tmpRes) res.push(tmpRes);
}
// If there are less than 2 elements, unpack the array.
// res[0] is either undefined or some value.
return res.length < 2 ? res[0] : res;
};
/**
* ### EventEmitterManager.emitAsync
*
* Emits an event on all registered event emitters asynchrounsly
*
* Accepts a variable number of input parameters.
*
* @param {string} eventName The name of the event
*
* @see EventEmitterManager.emit
*/
EventEmitterManager.prototype.emitAsync = function(eventName) {
var i, len, args, ees;
if ('string' !== typeof eventName) {
throw new TypeError(
'EventEmitterManager.emit: eventName must be string. Found: ' +
eventName);
}
len = arguments.length;
// The scope might `node` if this method is invoked from `node.emit`.
ees = this.ee || this.events.ee;
// The arguments object must not be passed or leaked anywhere.
switch(len) {
case 1:
ees.ng.emitAsync(eventName);
ees.game.emitAsync(eventName);
ees.stage.emitAsync(eventName);
ees.step.emitAsync(eventName);
break;
case 2:
ees.ng.emitAsync(eventName, arguments[1]);
ees.game.emitAsync(eventName, arguments[1]);
ees.stage.emitAsync(eventName, arguments[1]);
ees.step.emitAsync(eventName, arguments[1]);
break;
case 3:
ees.ng.emitAsync(eventName, arguments[1], arguments[2]);
ees.game.emitAsync(eventName, arguments[1], arguments[2]);
ees.stage.emitAsync(eventName, arguments[1], arguments[2]);
ees.step.emitAsync(eventName, arguments[1], arguments[2]);
break;
default:
args = new Array(len);
for (i = -1 ; ++i < len ; ) {
args[i] = arguments[i];
}
ees.ng.emitAsync.apply(ees.ng, args);
ees.game.emitAsync.apply(ees.game, args);
ees.stage.emitAsync.apply(ees.stage, args);
ees.step.emitAsync.apply(ees.step, args);
}
};
/**
* ### EventEmitterManager.remove
*
* Removes an event / event listener from all registered event emitters
*
* @param {string} eventName The name of the event
* @param {function|string} listener Optional A reference to the
* function to remove, or its name
*
* @return {object} Object containing removed listeners by event emitter
*/
EventEmitterManager.prototype.remove = function(eventName, listener) {
var res;
if ('string' !== typeof eventName) {
throw new TypeError('EventEmitterManager.remove: eventName ' +
'must be string. Found: ' + eventName);
}
if (listener &&
('function' !== typeof listener && 'string' !== typeof listener)) {
throw new TypeError('EventEmitter.remove (' + this.name +
'): listener must be function, string, or ' +
'undefined. Found: ' + listener);
}
res = {};
res.ng = this.ng.remove(eventName, listener);
res.game = this.game.remove(eventName, listener);
res.stage = this.stage.remove(eventName, listener);
res.step = this.step.remove(eventName, listener);
return res;
};
/**
* ### EventEmitterManager.printAll
*
* Prints all registered events
*
* @param {string} eventEmitterName Optional The name of the event emitter
*/
EventEmitterManager.prototype.printAll = function(eventEmitterName) {
var total;
if (eventEmitterName && 'string' !== typeof eventEmitterName) {
throw new TypeError('EventEmitterManager.printAll: ' +
'eventEmitterName must be string or ' +
'undefined. Found: ' + eventEmitterName);
}
if (eventEmitterName && !this.ee[eventEmitterName]) {
throw new TypeError('EventEmitterManager.printAll: event' +
'emitter not found: ' + eventEmitterName);
}
if (eventEmitterName) {
total = this.ee[eventEmitterName].printAll();
}
else {
total = 0;
total += this.ng.printAll();
total += this.game.printAll();
total += this.stage.printAll();
total += this.step.printAll();
console.log('Total number of registered listeners: ' + total);
}
return total;
};
/**
* ### EventEmitterManager.getAll
*
* Returns all registered events
*
* @param {string} eventEmitterName Optional The name of the event emitter
*/
EventEmitterManager.prototype.getAll = function(eventEmitterName) {
var events;
if (eventEmitterName && 'string' !== typeof eventEmitterName) {
throw new TypeError('EventEmitterManager.getAll: ' +
'eventEmitterName must be string or ' +
'undefined. Found: ' + eventEmitterName);
}
if (eventEmitterName && !this.ee[eventEmitterName]) {
throw new TypeError('EventEmitterManager.getAll: event' +
'emitter not found: ' + eventEmitterName);
}
if (eventEmitterName) {
events = this.ee[eventEmitterName].events;
}
else {
events = {
ng: this.ng.events,
game: this.game.events,
stage: this.stage.events,
step: this.step.events
};
}
return events;
};
/**
* ### EventEmitterManager.getChanges
*
* Returns the list of changes from all event emitters
*
* Considered event emitters: ng, game, stage, step.
*
* @param {boolean} clear Optional. If TRUE, the list of current changes
* is cleared. Default FALSE
*
* @return {object} Object containing changes for all event emitters, or
* null if no changes have been recorded
*
* @see EventEmitter.getChanges
*/
EventEmitterManager.prototype.getChanges = function(clear) {
var changes, tmp;
changes = {};
tmp = this.ee.ng.getChanges(clear);
if (tmp) changes.ng = tmp;
tmp = this.ee.game.getChanges(clear);
if (tmp) changes.game = tmp;
tmp = this.ee.stage.getChanges(clear);
if (tmp) changes.stage = tmp;
tmp = this.ee.step.getChanges(clear);
if (tmp) changes.step = tmp;
return J.isEmpty(changes) ? null : changes;
};
/**
* ### EventEmitterManager.setRecordChanges
*
* Sets the value of recordChanges for all event emitters and returns it
*
* If called with undefined, just returns current value.
*
* @param {boolean} record If TRUE, starts recording changes. Default FALSE
*
* @return {object} Current values of recordChanges for all event emitters
*
* @see EventEmitter.recordChanges
*/
EventEmitterManager.prototype.setRecordChanges = function(record) {
var out;
out = {};
out.ng = this.ee.ng.setRecordChanges(record);
out.game = this.ee.game.setRecordChanges(record);
out.stage = this.ee.stage.setRecordChanges(record);
out.step = this.ee.step.setRecordChanges(record);
return out;
};
/**
* ### EventEmitterManager.size
*
* Returns the number of registered events / event listeners
*
* Calls the `size` method of each event emitter.
*
* @param {mixed} Optional. Modifier controlling the return value
*
* @return {number} Total number of registered events / event listeners
*
* @see EventEmitter.size
*/
EventEmitterManager.prototype.size = function(mod) {
var count;
count = this.ng.size(mod);
count += this.game.size(mod);
count += this.stage.size(mod);
count += this.step.size(mod);