-
Notifications
You must be signed in to change notification settings - Fork 87
/
ros2d.js
2909 lines (2507 loc) · 88.7 KB
/
ros2d.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
var ROS2D = (function (exports, createjs, ROSLIB) {
'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var createjs__namespace = /*#__PURE__*/_interopNamespace(createjs);
var ROSLIB__namespace = /*#__PURE__*/_interopNamespace(ROSLIB);
// convert the given global Stage coordinates to ROS coordinates
createjs__namespace.Stage.prototype.globalToRos = function(x, y) {
var rosX = (x - this.x) / this.scaleX;
var rosY = (this.y - y) / this.scaleY;
return new ROSLIB__namespace.Vector3({
x : rosX,
y : rosY
});
};
// convert the given ROS coordinates to global Stage coordinates
createjs__namespace.Stage.prototype.rosToGlobal = function(pos) {
var x = pos.x * this.scaleX + this.x;
var y = pos.y * this.scaleY + this.y;
return {
x : x,
y : y
};
};
// convert a ROS quaternion to theta in degrees
createjs__namespace.Stage.prototype.rosQuaternionToGlobalTheta = function(orientation) {
// See https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Rotation_matrices
// here we use [x y z] = R * [1 0 0]
var q0 = orientation.w;
var q1 = orientation.x;
var q2 = orientation.y;
var q3 = orientation.z;
// Canvas rotation is clock wise and in degrees
return -Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3)) * 180.0 / Math.PI;
};
/**
* @fileOverview
* @author Russell Toris - [email protected]
*/
class ImageMap extends createjs__namespace.Bitmap {
/**
* An image map is a PNG image scaled to fit to the dimensions of a OccupancyGrid.
*
* @constructor
* @param options - object with following keys:
* * message - the occupancy grid map meta data message
* * image - the image URL to load
*/
constructor(options) {
options = options || {};
var message = options.message;
var image = options.image;
// create the bitmap
super(image);
// save the metadata we need
this.pose = new ROSLIB__namespace.Pose({
position : message.origin.position,
orientation : message.origin.orientation
});
// set the size
this.width = message.width;
this.height = message.height;
// change Y direction
this.y = -this.height * message.resolution;
// scale the image
this.scaleX = message.resolution;
this.scaleY = message.resolution;
this.width *= this.scaleX;
this.height *= this.scaleY;
// set the pose
this.x += this.pose.position.x;
this.y -= this.pose.position.y;
};
}
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var eventemitter2 = {exports: {}};
/*!
* EventEmitter2
* https://github.com/hij1nx/EventEmitter2
*
* Copyright (c) 2013 hij1nx
* Licensed under the MIT license.
*/
eventemitter2.exports;
var hasRequiredEventemitter2;
function requireEventemitter2 () {
if (hasRequiredEventemitter2) return eventemitter2.exports;
hasRequiredEventemitter2 = 1;
(function (module, exports) {
!function(undefined$1) {
var hasOwnProperty= Object.hasOwnProperty;
var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
var defaultMaxListeners = 10;
var nextTickSupported= typeof process=='object' && typeof process.nextTick=='function';
var symbolsSupported= typeof Symbol==='function';
var reflectSupported= typeof Reflect === 'object';
var setImmediateSupported= typeof setImmediate === 'function';
var _setImmediate= setImmediateSupported ? setImmediate : setTimeout;
var ownKeys= symbolsSupported? (reflectSupported && typeof Reflect.ownKeys==='function'? Reflect.ownKeys : function(obj){
var arr= Object.getOwnPropertyNames(obj);
arr.push.apply(arr, Object.getOwnPropertySymbols(obj));
return arr;
}) : Object.keys;
function init() {
this._events = {};
if (this._conf) {
configure.call(this, this._conf);
}
}
function configure(conf) {
if (conf) {
this._conf = conf;
conf.delimiter && (this.delimiter = conf.delimiter);
if(conf.maxListeners!==undefined$1){
this._maxListeners= conf.maxListeners;
}
conf.wildcard && (this.wildcard = conf.wildcard);
conf.newListener && (this._newListener = conf.newListener);
conf.removeListener && (this._removeListener = conf.removeListener);
conf.verboseMemoryLeak && (this.verboseMemoryLeak = conf.verboseMemoryLeak);
conf.ignoreErrors && (this.ignoreErrors = conf.ignoreErrors);
if (this.wildcard) {
this.listenerTree = {};
}
}
}
function logPossibleMemoryLeak(count, eventName) {
var errorMsg = '(node) warning: possible EventEmitter memory ' +
'leak detected. ' + count + ' listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.';
if(this.verboseMemoryLeak){
errorMsg += ' Event name: ' + eventName + '.';
}
if(typeof process !== 'undefined' && process.emitWarning){
var e = new Error(errorMsg);
e.name = 'MaxListenersExceededWarning';
e.emitter = this;
e.count = count;
process.emitWarning(e);
} else {
console.error(errorMsg);
if (console.trace){
console.trace();
}
}
}
var toArray = function (a, b, c) {
var n = arguments.length;
switch (n) {
case 0:
return [];
case 1:
return [a];
case 2:
return [a, b];
case 3:
return [a, b, c];
default:
var arr = new Array(n);
while (n--) {
arr[n] = arguments[n];
}
return arr;
}
};
function toObject(keys, values) {
var obj = {};
var key;
var len = keys.length;
var valuesCount = values ? value.length : 0;
for (var i = 0; i < len; i++) {
key = keys[i];
obj[key] = i < valuesCount ? values[i] : undefined$1;
}
return obj;
}
function TargetObserver(emitter, target, options) {
this._emitter = emitter;
this._target = target;
this._listeners = {};
this._listenersCount = 0;
var on, off;
if (options.on || options.off) {
on = options.on;
off = options.off;
}
if (target.addEventListener) {
on = target.addEventListener;
off = target.removeEventListener;
} else if (target.addListener) {
on = target.addListener;
off = target.removeListener;
} else if (target.on) {
on = target.on;
off = target.off;
}
if (!on && !off) {
throw Error('target does not implement any known event API');
}
if (typeof on !== 'function') {
throw TypeError('on method must be a function');
}
if (typeof off !== 'function') {
throw TypeError('off method must be a function');
}
this._on = on;
this._off = off;
var _observers= emitter._observers;
if(_observers){
_observers.push(this);
}else {
emitter._observers= [this];
}
}
Object.assign(TargetObserver.prototype, {
subscribe: function(event, localEvent, reducer){
var observer= this;
var target= this._target;
var emitter= this._emitter;
var listeners= this._listeners;
var handler= function(){
var args= toArray.apply(null, arguments);
var eventObj= {
data: args,
name: localEvent,
original: event
};
if(reducer){
var result= reducer.call(target, eventObj);
if(result!==false){
emitter.emit.apply(emitter, [eventObj.name].concat(args));
}
return;
}
emitter.emit.apply(emitter, [localEvent].concat(args));
};
if(listeners[event]){
throw Error('Event \'' + event + '\' is already listening');
}
this._listenersCount++;
if(emitter._newListener && emitter._removeListener && !observer._onNewListener){
this._onNewListener = function (_event) {
if (_event === localEvent && listeners[event] === null) {
listeners[event] = handler;
observer._on.call(target, event, handler);
}
};
emitter.on('newListener', this._onNewListener);
this._onRemoveListener= function(_event){
if(_event === localEvent && !emitter.hasListeners(_event) && listeners[event]){
listeners[event]= null;
observer._off.call(target, event, handler);
}
};
listeners[event]= null;
emitter.on('removeListener', this._onRemoveListener);
}else {
listeners[event]= handler;
observer._on.call(target, event, handler);
}
},
unsubscribe: function(event){
var observer= this;
var listeners= this._listeners;
var emitter= this._emitter;
var handler;
var events;
var off= this._off;
var target= this._target;
var i;
if(event && typeof event!=='string'){
throw TypeError('event must be a string');
}
function clearRefs(){
if(observer._onNewListener){
emitter.off('newListener', observer._onNewListener);
emitter.off('removeListener', observer._onRemoveListener);
observer._onNewListener= null;
observer._onRemoveListener= null;
}
var index= findTargetIndex.call(emitter, observer);
emitter._observers.splice(index, 1);
}
if(event){
handler= listeners[event];
if(!handler) return;
off.call(target, event, handler);
delete listeners[event];
if(!--this._listenersCount){
clearRefs();
}
}else {
events= ownKeys(listeners);
i= events.length;
while(i-->0){
event= events[i];
off.call(target, event, listeners[event]);
}
this._listeners= {};
this._listenersCount= 0;
clearRefs();
}
}
});
function resolveOptions(options, schema, reducers, allowUnknown) {
var computedOptions = Object.assign({}, schema);
if (!options) return computedOptions;
if (typeof options !== 'object') {
throw TypeError('options must be an object')
}
var keys = Object.keys(options);
var length = keys.length;
var option, value;
var reducer;
function reject(reason) {
throw Error('Invalid "' + option + '" option value' + (reason ? '. Reason: ' + reason : ''))
}
for (var i = 0; i < length; i++) {
option = keys[i];
if (!allowUnknown && !hasOwnProperty.call(schema, option)) {
throw Error('Unknown "' + option + '" option');
}
value = options[option];
if (value !== undefined$1) {
reducer = reducers[option];
computedOptions[option] = reducer ? reducer(value, reject) : value;
}
}
return computedOptions;
}
function constructorReducer(value, reject) {
if (typeof value !== 'function' || !value.hasOwnProperty('prototype')) {
reject('value must be a constructor');
}
return value;
}
function makeTypeReducer(types) {
var message= 'value must be type of ' + types.join('|');
var len= types.length;
var firstType= types[0];
var secondType= types[1];
if (len === 1) {
return function (v, reject) {
if (typeof v === firstType) {
return v;
}
reject(message);
}
}
if (len === 2) {
return function (v, reject) {
var kind= typeof v;
if (kind === firstType || kind === secondType) return v;
reject(message);
}
}
return function (v, reject) {
var kind = typeof v;
var i = len;
while (i-- > 0) {
if (kind === types[i]) return v;
}
reject(message);
}
}
var functionReducer= makeTypeReducer(['function']);
var objectFunctionReducer= makeTypeReducer(['object', 'function']);
function makeCancelablePromise(Promise, executor, options) {
var isCancelable;
var callbacks;
var timer= 0;
var subscriptionClosed;
var promise = new Promise(function (resolve, reject, onCancel) {
options= resolveOptions(options, {
timeout: 0,
overload: false
}, {
timeout: function(value, reject){
value*= 1;
if (typeof value !== 'number' || value < 0 || !Number.isFinite(value)) {
reject('timeout must be a positive number');
}
return value;
}
});
isCancelable = !options.overload && typeof Promise.prototype.cancel === 'function' && typeof onCancel === 'function';
function cleanup() {
if (callbacks) {
callbacks = null;
}
if (timer) {
clearTimeout(timer);
timer = 0;
}
}
var _resolve= function(value){
cleanup();
resolve(value);
};
var _reject= function(err){
cleanup();
reject(err);
};
if (isCancelable) {
executor(_resolve, _reject, onCancel);
} else {
callbacks = [function(reason){
_reject(reason || Error('canceled'));
}];
executor(_resolve, _reject, function (cb) {
if (subscriptionClosed) {
throw Error('Unable to subscribe on cancel event asynchronously')
}
if (typeof cb !== 'function') {
throw TypeError('onCancel callback must be a function');
}
callbacks.push(cb);
});
subscriptionClosed= true;
}
if (options.timeout > 0) {
timer= setTimeout(function(){
var reason= Error('timeout');
reason.code = 'ETIMEDOUT';
timer= 0;
promise.cancel(reason);
reject(reason);
}, options.timeout);
}
});
if (!isCancelable) {
promise.cancel = function (reason) {
if (!callbacks) {
return;
}
var length = callbacks.length;
for (var i = 1; i < length; i++) {
callbacks[i](reason);
}
// internal callback to reject the promise
callbacks[0](reason);
callbacks = null;
};
}
return promise;
}
function findTargetIndex(observer) {
var observers = this._observers;
if(!observers){
return -1;
}
var len = observers.length;
for (var i = 0; i < len; i++) {
if (observers[i]._target === observer) return i;
}
return -1;
}
// Attention, function return type now is array, always !
// It has zero elements if no any matches found and one or more
// elements (leafs) if there are matches
//
function searchListenerTree(handlers, type, tree, i, typeLength) {
if (!tree) {
return null;
}
if (i === 0) {
var kind = typeof type;
if (kind === 'string') {
var ns, n, l = 0, j = 0, delimiter = this.delimiter, dl = delimiter.length;
if ((n = type.indexOf(delimiter)) !== -1) {
ns = new Array(5);
do {
ns[l++] = type.slice(j, n);
j = n + dl;
} while ((n = type.indexOf(delimiter, j)) !== -1);
ns[l++] = type.slice(j);
type = ns;
typeLength = l;
} else {
type = [type];
typeLength = 1;
}
} else if (kind === 'object') {
typeLength = type.length;
} else {
type = [type];
typeLength = 1;
}
}
var listeners= null, branch, xTree, xxTree, isolatedBranch, endReached, currentType = type[i],
nextType = type[i + 1], branches, _listeners;
if (i === typeLength) {
//
// If at the end of the event(s) list and the tree has listeners
// invoke those listeners.
//
if(tree._listeners) {
if (typeof tree._listeners === 'function') {
handlers && handlers.push(tree._listeners);
listeners = [tree];
} else {
handlers && handlers.push.apply(handlers, tree._listeners);
listeners = [tree];
}
}
} else {
if (currentType === '*') {
//
// If the event emitted is '*' at this part
// or there is a concrete match at this patch
//
branches = ownKeys(tree);
n = branches.length;
while (n-- > 0) {
branch = branches[n];
if (branch !== '_listeners') {
_listeners = searchListenerTree(handlers, type, tree[branch], i + 1, typeLength);
if (_listeners) {
if (listeners) {
listeners.push.apply(listeners, _listeners);
} else {
listeners = _listeners;
}
}
}
}
return listeners;
} else if (currentType === '**') {
endReached = (i + 1 === typeLength || (i + 2 === typeLength && nextType === '*'));
if (endReached && tree._listeners) {
// The next element has a _listeners, add it to the handlers.
listeners = searchListenerTree(handlers, type, tree, typeLength, typeLength);
}
branches = ownKeys(tree);
n = branches.length;
while (n-- > 0) {
branch = branches[n];
if (branch !== '_listeners') {
if (branch === '*' || branch === '**') {
if (tree[branch]._listeners && !endReached) {
_listeners = searchListenerTree(handlers, type, tree[branch], typeLength, typeLength);
if (_listeners) {
if (listeners) {
listeners.push.apply(listeners, _listeners);
} else {
listeners = _listeners;
}
}
}
_listeners = searchListenerTree(handlers, type, tree[branch], i, typeLength);
} else if (branch === nextType) {
_listeners = searchListenerTree(handlers, type, tree[branch], i + 2, typeLength);
} else {
// No match on this one, shift into the tree but not in the type array.
_listeners = searchListenerTree(handlers, type, tree[branch], i, typeLength);
}
if (_listeners) {
if (listeners) {
listeners.push.apply(listeners, _listeners);
} else {
listeners = _listeners;
}
}
}
}
return listeners;
} else if (tree[currentType]) {
listeners = searchListenerTree(handlers, type, tree[currentType], i + 1, typeLength);
}
}
xTree = tree['*'];
if (xTree) {
//
// If the listener tree will allow any match for this part,
// then recursively explore all branches of the tree
//
searchListenerTree(handlers, type, xTree, i + 1, typeLength);
}
xxTree = tree['**'];
if (xxTree) {
if (i < typeLength) {
if (xxTree._listeners) {
// If we have a listener on a '**', it will catch all, so add its handler.
searchListenerTree(handlers, type, xxTree, typeLength, typeLength);
}
// Build arrays of matching next branches and others.
branches= ownKeys(xxTree);
n= branches.length;
while(n-->0){
branch= branches[n];
if (branch !== '_listeners') {
if (branch === nextType) {
// We know the next element will match, so jump twice.
searchListenerTree(handlers, type, xxTree[branch], i + 2, typeLength);
} else if (branch === currentType) {
// Current node matches, move into the tree.
searchListenerTree(handlers, type, xxTree[branch], i + 1, typeLength);
} else {
isolatedBranch = {};
isolatedBranch[branch] = xxTree[branch];
searchListenerTree(handlers, type, {'**': isolatedBranch}, i + 1, typeLength);
}
}
}
} else if (xxTree._listeners) {
// We have reached the end and still on a '**'
searchListenerTree(handlers, type, xxTree, typeLength, typeLength);
} else if (xxTree['*'] && xxTree['*']._listeners) {
searchListenerTree(handlers, type, xxTree['*'], typeLength, typeLength);
}
}
return listeners;
}
function growListenerTree(type, listener, prepend) {
var len = 0, j = 0, i, delimiter = this.delimiter, dl= delimiter.length, ns;
if(typeof type==='string') {
if ((i = type.indexOf(delimiter)) !== -1) {
ns = new Array(5);
do {
ns[len++] = type.slice(j, i);
j = i + dl;
} while ((i = type.indexOf(delimiter, j)) !== -1);
ns[len++] = type.slice(j);
}else {
ns= [type];
len= 1;
}
}else {
ns= type;
len= type.length;
}
//
// Looks for two consecutive '**', if so, don't add the event at all.
//
if (len > 1) {
for (i = 0; i + 1 < len; i++) {
if (ns[i] === '**' && ns[i + 1] === '**') {
return;
}
}
}
var tree = this.listenerTree, name;
for (i = 0; i < len; i++) {
name = ns[i];
tree = tree[name] || (tree[name] = {});
if (i === len - 1) {
if (!tree._listeners) {
tree._listeners = listener;
} else {
if (typeof tree._listeners === 'function') {
tree._listeners = [tree._listeners];
}
if (prepend) {
tree._listeners.unshift(listener);
} else {
tree._listeners.push(listener);
}
if (
!tree._listeners.warned &&
this._maxListeners > 0 &&
tree._listeners.length > this._maxListeners
) {
tree._listeners.warned = true;
logPossibleMemoryLeak.call(this, tree._listeners.length, name);
}
}
return true;
}
}
return true;
}
function collectTreeEvents(tree, events, root, asArray){
var branches= ownKeys(tree);
var i= branches.length;
var branch, branchName, path;
var hasListeners= tree['_listeners'];
var isArrayPath;
while(i-->0){
branchName= branches[i];
branch= tree[branchName];
if(branchName==='_listeners'){
path= root;
}else {
path = root ? root.concat(branchName) : [branchName];
}
isArrayPath= asArray || typeof branchName==='symbol';
hasListeners && events.push(isArrayPath? path : path.join(this.delimiter));
if(typeof branch==='object'){
collectTreeEvents.call(this, branch, events, path, isArrayPath);
}
}
return events;
}
function recursivelyGarbageCollect(root) {
var keys = ownKeys(root);
var i= keys.length;
var obj, key, flag;
while(i-->0){
key = keys[i];
obj = root[key];
if(obj){
flag= true;
if(key !== '_listeners' && !recursivelyGarbageCollect(obj)){
delete root[key];
}
}
}
return flag;
}
function Listener(emitter, event, listener){
this.emitter= emitter;
this.event= event;
this.listener= listener;
}
Listener.prototype.off= function(){
this.emitter.off(this.event, this.listener);
return this;
};
function setupListener(event, listener, options){
if (options === true) {
promisify = true;
} else if (options === false) {
async = true;
} else {
if (!options || typeof options !== 'object') {
throw TypeError('options should be an object or true');
}
var async = options.async;
var promisify = options.promisify;
var nextTick = options.nextTick;
var objectify = options.objectify;
}
if (async || nextTick || promisify) {
var _listener = listener;
var _origin = listener._origin || listener;
if (nextTick && !nextTickSupported) {
throw Error('process.nextTick is not supported');
}
if (promisify === undefined$1) {
promisify = listener.constructor.name === 'AsyncFunction';
}
listener = function () {
var args = arguments;
var context = this;
var event = this.event;
return promisify ? (nextTick ? Promise.resolve() : new Promise(function (resolve) {
_setImmediate(resolve);
}).then(function () {
context.event = event;
return _listener.apply(context, args)
})) : (nextTick ? process.nextTick : _setImmediate)(function () {
context.event = event;
_listener.apply(context, args);
});
};
listener._async = true;
listener._origin = _origin;
}
return [listener, objectify? new Listener(this, event, listener): this];
}
function EventEmitter(conf) {
this._events = {};
this._newListener = false;
this._removeListener = false;
this.verboseMemoryLeak = false;
configure.call(this, conf);
}
EventEmitter.EventEmitter2 = EventEmitter; // backwards compatibility for exporting EventEmitter property
EventEmitter.prototype.listenTo= function(target, events, options){
if(typeof target!=='object'){
throw TypeError('target musts be an object');
}
var emitter= this;
options = resolveOptions(options, {
on: undefined$1,
off: undefined$1,
reducers: undefined$1
}, {
on: functionReducer,
off: functionReducer,
reducers: objectFunctionReducer
});
function listen(events){
if(typeof events!=='object'){
throw TypeError('events must be an object');
}
var reducers= options.reducers;
var index= findTargetIndex.call(emitter, target);
var observer;
if(index===-1){
observer= new TargetObserver(emitter, target, options);
}else {
observer= emitter._observers[index];
}
var keys= ownKeys(events);
var len= keys.length;
var event;
var isSingleReducer= typeof reducers==='function';
for(var i=0; i<len; i++){
event= keys[i];
observer.subscribe(
event,
events[event] || event,
isSingleReducer ? reducers : reducers && reducers[event]
);
}
}
isArray(events)?
listen(toObject(events)) :
(typeof events==='string'? listen(toObject(events.split(/\s+/))): listen(events));
return this;
};
EventEmitter.prototype.stopListeningTo = function (target, event) {
var observers = this._observers;
if(!observers){
return false;
}
var i = observers.length;
var observer;
var matched= false;
if(target && typeof target!=='object'){
throw TypeError('target should be an object');
}
while (i-- > 0) {
observer = observers[i];
if (!target || observer._target === target) {
observer.unsubscribe(event);
matched= true;
}
}
return matched;
};
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.