@creejs/commons-events 2.0.2 → 2.0.4

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.
@@ -0,0 +1,821 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var r={isFunction:u,isNil:l};function u(t){return "function"==typeof t}function l(t){return null==t}function y(t){return null!=t&&"number"==typeof t}function d(t){return null!=t&&"string"==typeof t}function m(t){return null!=t&&"symbol"==typeof t}var N={assertNumber:E,assertFunction:v,assertNotNil:function(t,e){if(l(t))throw new Error((e?'"'+e+'" ':" ")+"Should Not Nil")},assertString:b,assertStringOrSymbol:function(t,e){if(!d(t)&&!m(t))throw new Error(`${e?'"'+e+'" ':" "}Not String or Symbol: type=${typeof t} value=${JSON.stringify(t)}`)}};function b(t,e){if(!d(t))throw new Error(`${e?'"'+e+'" ':" "}Not String: type=${typeof t} value=${JSON.stringify(t)}`)}function E(t,e){if(!y(t))throw new Error(`${e?'"'+e+'" ':" "}Not Number: type=${typeof t} value=${JSON.stringify(t)}`)}function v(t,e){if(!u(t))throw new Error(`${e?'"'+e+'" ':" "}Not Function: type=${typeof t} value=${JSON.stringify(t)}`)}
6
+
7
+ const DefaultOwner = 'DOwner$#$';
8
+
9
+ // 3rd
10
+ // internal
11
+
12
+ // module vars
13
+ const { assertFunction: assertFunction$2, assertNotNil: assertNotNil$1 } = N;
14
+ /**
15
+ * Wraps a function to be called when an event is fired.
16
+ * @typedef {import('./event.js').default} Event
17
+ * @class Listener
18
+ */
19
+ class Listener {
20
+ /**
21
+ * @param {Event} event
22
+ * @param {function} callback - The function to be called when event is fired
23
+ * @param {boolean} [isOnce=false] - is a one time listener?
24
+ */
25
+ constructor (event, callback, isOnce = false) {
26
+ assertNotNil$1(event, 'event');
27
+ assertFunction$2(callback, 'callback');
28
+ this._event = event;
29
+ this._callback = callback;
30
+ this._isOnce = !!isOnce; // is Once Listener?
31
+ this._owner = undefined;
32
+ }
33
+
34
+ /**
35
+ * Sets the owner of this listener.
36
+ * @param {*} owner - The owner object to be associated with this listener.
37
+ */
38
+ set owner (owner) {
39
+ this._owner = owner;
40
+ }
41
+
42
+ get owner () {
43
+ return this._owner === DefaultOwner ? undefined : this._owner
44
+ }
45
+
46
+ get event () {
47
+ return this._event
48
+ }
49
+
50
+ get isOnce () {
51
+ return this._isOnce
52
+ }
53
+
54
+ /**
55
+ * Checks if the provided function is the same as the listener's wrapped function.
56
+ * @param {Function} callback - The function to compare against.
57
+ * @returns {boolean} True if the functions are the same, false otherwise.
58
+ */
59
+ isSameCallback (callback) {
60
+ return this._callback === callback
61
+ }
62
+
63
+ get callback () {
64
+ return this._callback
65
+ }
66
+
67
+ /**
68
+ * Invokes the stored function with the provided arguments.
69
+ * @param {...*} args - Arguments to pass to the function.
70
+ * @returns {*} The result of the function invocation.
71
+ */
72
+ invoke (...args) {
73
+ try {
74
+ return this._callback(...args)
75
+ } finally {
76
+ if (this._isOnce) {
77
+ try {
78
+ this._event._remove(this);
79
+ } catch (err) {
80
+ // do nothing
81
+ console.warn(err);
82
+ }
83
+ }
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Invokes the listener with the provided arguments.
89
+ * Alias for {@linkcode Listener.invoke}
90
+ * @param {...*} args - Arguments to be passed to the listener.
91
+ * @returns {*} The result of the listener invocation.
92
+ */
93
+ listener (...args) {
94
+ return this.invoke(...args)
95
+ }
96
+ }
97
+
98
+ // 3rd
99
+ // internal
100
+
101
+ // module vars
102
+ const { isFunction, isNil: isNil$1 } = r;
103
+ const { assertStringOrSymbol: assertStringOrSymbol$1, assertFunction: assertFunction$1 } = N;
104
+
105
+ /**
106
+ * An Event definition
107
+ * 1. listeners are grouped by owner
108
+ * * if not specified, group it into one Default owner
109
+ * 2. one listener may belong to multiple owners
110
+ * 3. one owner may have multiple listeners
111
+ */
112
+ class Event {
113
+ static get DefaultOwner () {
114
+ return DefaultOwner
115
+ }
116
+
117
+ /**
118
+ * Creates a new Event instance with the specified name.
119
+ * @param {string|Symbol} eventName - The name of the event.
120
+ */
121
+ constructor (eventName) {
122
+ assertStringOrSymbol$1(eventName, 'eventName');
123
+ this._name = eventName;
124
+ /**
125
+ * use Set to store unique listeners
126
+ * @type {Set<function>}
127
+ */
128
+ this._callbacks = new Set();
129
+ /**
130
+ * @type {Array<Listener>}
131
+ */
132
+ this._listeners = []; // user array to keep order
133
+ /**
134
+ * @type {Map<function, Set<Listener>>}
135
+ */
136
+ this._callback2Listeners = new Map();
137
+ /**
138
+ * use listener to index owners. One Listener Instance only belongs to one owner
139
+ * @type {Map<Listener, *>}
140
+ */
141
+ this._listener2Owner = new Map();
142
+ /**
143
+ * use "owner" to group listeners; one owner may have multiple listeners
144
+ * @type {Map<*, Set<Listener>>}
145
+ */
146
+ this._owner2Listeners = new Map();
147
+ }
148
+
149
+ /**
150
+ * Name of the event.
151
+ * @returns {string|Symbol}
152
+ */
153
+ get name () {
154
+ return this._name
155
+ }
156
+
157
+ isEmpty () {
158
+ return this._callbacks.size === 0
159
+ }
160
+
161
+ /**
162
+ * Returns a copy of the raw listeners array.
163
+ * @returns {Array<Listener>} A shallow copy of the listeners array.
164
+ */
165
+ rawListeners () {
166
+ return [...this._listeners]
167
+ }
168
+
169
+ /**
170
+ * Returns the number of listeners listening for the event.
171
+ * If callback is provided, it will return how many times the callback is found in the list of the listeners
172
+ * @param {Function} [callback] - The callback function to count
173
+ * @returns {number}
174
+ */
175
+ listenerCount (callback) {
176
+ if (callback == null) {
177
+ return this._listeners.length
178
+ }
179
+ return this._callback2Listeners.get(callback)?.size ?? 0
180
+ }
181
+
182
+ /**
183
+ * Returns a shallow copy of the registered callbacks array.
184
+ * if one callback function is added multiple times, it will be returned multiple times.
185
+ * @returns {Array<function>} A new array containing all registered callbacks.
186
+ */
187
+ callbacks () {
188
+ return [...this.rawListeners().map(listener => listener.callback)]
189
+ }
190
+
191
+ /**
192
+ * Emits current event, invoking all registered listeners by order.
193
+ * @param {...*} args - Arguments to be passed to each listener.
194
+ * @returns {boolean} True if the event had listeners, false otherwise.
195
+ */
196
+ emit (...args) {
197
+ if (this._listeners.length === 0) {
198
+ return false
199
+ }
200
+ // Clone _listeners, it may be changed during call listener
201
+ for (const listener of [...this._listeners]) {
202
+ listener.invoke(...args);
203
+ }
204
+ return true
205
+ }
206
+
207
+ /**
208
+ * Checks if listener is registered with this event.
209
+ * @param {function} callback - The listener function to check.
210
+ * @returns {boolean} True if the listener is registered, false otherwise.
211
+ */
212
+ hasListener (callback) {
213
+ if (!isFunction(callback)) {
214
+ return false
215
+ }
216
+ return this._callbacks.has(callback)
217
+ }
218
+
219
+ /**
220
+ * Checks if owner has any registered listeners.
221
+ * @param {*} owner - The owner to check for registered listeners.
222
+ * @returns {boolean} True if the owner has listeners, false otherwise.
223
+ */
224
+ hasOwner (owner) {
225
+ if (isNil$1(owner)) {
226
+ return false
227
+ }
228
+ return this._owner2Listeners.has(owner)
229
+ }
230
+
231
+ /**
232
+ * Adds an event listener
233
+ * @param {function} callback - The callback function to be invoked when the event occurs.
234
+ * @param {*} [owner] - use "owner" to group listeners, then can remove all listeners for an owner.
235
+ * @returns {boolean} true if added, false otherwise.
236
+ */
237
+ addListener (callback, owner) {
238
+ return this._addListener(callback, owner, false, false)
239
+ }
240
+
241
+ /**
242
+ * Prepends a listener callback to the beginning of the listeners array.
243
+ * No checks are made to see if the listener has already been added.
244
+ * Multiple calls passing the same combination of eventName and listener will result in the listener being added,
245
+ * and called, multiple times.
246
+ * @param {Function} callback - The callback function to be executed when the event is emitted.
247
+ * @param {Object} owner - The owner object to which the listener belongs.
248
+ * @returns {boolean}
249
+ */
250
+ prependListener (callback, owner) {
251
+ return this._addListener(callback, owner, false, true)
252
+ }
253
+
254
+ /**
255
+ * Adds a one-time listener for the event. The listener is automatically removed after being invoked once.
256
+ * @param {Function} callback - The function to call when the event is triggered.
257
+ * @param {Object} [owner] - The object that owns the callback (used for binding `this` context).
258
+ * @returns {boolean}
259
+ */
260
+ addOnceListener (callback, owner) {
261
+ return this._addListener(callback, owner, true, false)
262
+ }
263
+
264
+ /**
265
+ * Adds a one-time event listener that will be automatically removed after being triggered once.
266
+ * The listener will only be triggered if the event is pretended (simulated).
267
+ * @param {Function} callback - The function to call when the event is triggered.
268
+ * @param {Object} owner - The object that owns the listener (used for reference tracking).
269
+ * @returns {boolean} The listener function that was added.
270
+ */
271
+ prependOnceListener (callback, owner) {
272
+ return this._addListener(callback, owner, true, true)
273
+ }
274
+
275
+ /**
276
+ * Adds a listener for the event.
277
+ * @param {Function} callback - The callback function to be executed when the event occurs
278
+ * @param {*} [owner] - The owner object that the listener belongs to (defaults to DeaultOwner)
279
+ * @param {boolean} [isOnce=false] - Whether the listener should be removed after first invocation
280
+ * @param {boolean} [isPrepend=false] - Whether the listener should be inert at the beginning of the listeners array
281
+ * @returns {boolean} Returns true if listener was added successfully, false if callback is nil or duplicate
282
+ * @protected
283
+ */
284
+ _addListener (callback, owner, isOnce, isPrepend) {
285
+ if (isNil$1(callback)) {
286
+ return false
287
+ }
288
+ assertFunction$1(callback);
289
+ if (!this._callbacks.has(callback)) {
290
+ this._callbacks.add(callback);
291
+ }
292
+ // a listener must belong to an owner
293
+ owner = owner ?? DefaultOwner;
294
+
295
+ // use Listener to wrap callback
296
+ const listener = new Listener(this, callback, isOnce);
297
+ listener.owner = owner;
298
+ if (isPrepend) {
299
+ this._listeners.unshift(listener);
300
+ } else {
301
+ this._listeners.push(listener);
302
+ }
303
+
304
+ // index, rapadly find one listener's owner
305
+ this._listener2Owner.set(listener, owner);
306
+
307
+ // one callback function may be registered many times
308
+ let callbackListeners = this._callback2Listeners.get(callback);
309
+ if (callbackListeners == null) {
310
+ callbackListeners = new Set();
311
+ this._callback2Listeners.set(callback, callbackListeners);
312
+ }
313
+ callbackListeners.add(listener);
314
+
315
+ // group by owner
316
+ let ownerListeners = this._owner2Listeners.get(owner);
317
+ if (ownerListeners == null) {
318
+ ownerListeners = new Set();
319
+ this._owner2Listeners.set(owner, ownerListeners);
320
+ }
321
+ ownerListeners.add(listener);
322
+
323
+ return true
324
+ }
325
+
326
+ /**
327
+ * Removes a callback
328
+ * @param {Function} callback - The callback function to remove.
329
+ * @returns {boolean} true if removed, false otherwise.
330
+ */
331
+ removeListener (callback) {
332
+ if (isNil$1(callback)) {
333
+ return false
334
+ }
335
+ if (!this._callbacks.has(callback)) {
336
+ return false
337
+ }
338
+ this._callbacks.delete(callback);
339
+
340
+ const listeners = this._callback2Listeners.get(callback);
341
+ if (listeners == null) { // should not happen
342
+ return false
343
+ }
344
+ this._callback2Listeners.delete(callback);
345
+ for (const listener of listeners) {
346
+ // remove from global index
347
+ const index = this._listeners.indexOf(listener);
348
+ index !== -1 && this._listeners.splice(this._listeners.indexOf(listener), 1);
349
+
350
+ // remove from owner index
351
+ const owner = this._listener2Owner.get(listener);
352
+ if (owner == null) {
353
+ continue
354
+ }
355
+ this._listener2Owner.delete(listener);
356
+ const ownerListeners = this._owner2Listeners.get(owner);
357
+ if (ownerListeners == null) {
358
+ continue
359
+ }
360
+ ownerListeners.delete(listener);
361
+ if (ownerListeners.size === 0) {
362
+ this._owner2Listeners.delete(owner);
363
+ }
364
+ }
365
+ return true
366
+ }
367
+
368
+ /**
369
+ * Removes a listener from both global and owner indexes.
370
+ * @param {Listener} listener - The listener function to remove
371
+ */
372
+ _remove (listener) {
373
+ // remove from global index
374
+ const index = this._listeners.indexOf(listener);
375
+ index !== -1 && this._listeners.splice(index, 1);
376
+
377
+ // clean callback index
378
+ const { callback } = listener;
379
+ const callbackListeners = this._callback2Listeners.get(callback);
380
+ if (callbackListeners != null) {
381
+ callbackListeners.delete(listener);
382
+ if (callbackListeners.size === 0) {
383
+ this._callback2Listeners.delete(callback);
384
+ this._callbacks.delete(callback);
385
+ }
386
+ }
387
+
388
+ // remove from owner index
389
+ const owner = this._listener2Owner.get(listener);
390
+ if (owner == null) {
391
+ return
392
+ }
393
+ this._listener2Owner.delete(listener);
394
+ const ownerListeners = this._owner2Listeners.get(owner);
395
+ if (ownerListeners == null) {
396
+ return
397
+ }
398
+ ownerListeners.delete(listener);
399
+ if (ownerListeners.size === 0) {
400
+ this._owner2Listeners.delete(owner);
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Removes all event listeners
406
+ * @param {*} [owner] - Without owner, all listeners will be removed; otherwise, only listeners of "owner" will be removed.
407
+ * @returns {this}
408
+ */
409
+ removeAllListeners (owner) {
410
+ // remove listeners of owner
411
+ if (isNil$1(owner)) {
412
+ // remove all Listeners
413
+ this._callbacks.clear();
414
+ this._listeners.length = 0;
415
+ this._callback2Listeners.clear();
416
+ this._listener2Owner.clear();
417
+ this._owner2Listeners.clear();
418
+ return this
419
+ }
420
+ // all listeners of the owner
421
+ const ownerListeners = this._owner2Listeners.get(owner);
422
+ // no owner
423
+ if (ownerListeners == null) {
424
+ return this
425
+ }
426
+ // clear owner index
427
+ this._owner2Listeners.delete(owner);
428
+ // clearn listeners of owner one by one
429
+ for (const listener of ownerListeners) {
430
+ // remove from global index
431
+ const index = this._listeners.indexOf(listener);
432
+ index !== -1 && this._listeners.splice(this._listeners.indexOf(listener), 1);
433
+
434
+ // clean listener-owner index
435
+ this._listener2Owner.delete(listener);
436
+
437
+ // one callback function may be registered many times, has many Listeners
438
+ const { callback } = listener;
439
+ const callbackListeners = this._callback2Listeners.get(callback);
440
+ if (callbackListeners == null) {
441
+ continue
442
+ }
443
+ callbackListeners.delete(listener);
444
+ if (callbackListeners.size === 0) {
445
+ this._callback2Listeners.delete(callback);
446
+ this._callbacks.delete(callback);
447
+ }
448
+ }
449
+ return this
450
+ }
451
+ }
452
+
453
+ // 3rd
454
+ // internal
455
+
456
+ // module vars
457
+ const { isNil } = r;
458
+ const {
459
+ assertString, assertFunction, assertNumber,
460
+ assertStringOrSymbol, assertNotNil
461
+ } = N;
462
+
463
+ /**
464
+ * methods allowed to mixin other objects
465
+ */
466
+ const MixinMethods = [
467
+ 'on', 'once', 'addListener', 'prependListener', 'prependOnceListener',
468
+ 'off', 'offAll', 'offOwner', 'removeAllListeners', 'removeListener',
469
+ 'emit', 'setMaxListeners', 'getMaxListeners', 'hasOwner',
470
+ 'listeners', 'listenerCount', 'eventNames', 'rawListeners'
471
+ ];
472
+ let DefaultMaxListeners = 10;
473
+ /**
474
+ * 1. An EventEmitter follows the API of NodeJS EventEmitter.
475
+ * 2. Enhancement:
476
+ * * Listeners are grouped by "owner".
477
+ * * Operation via "owner"
478
+ * * Duplicate listeners are filtered out. Only unique One kept.
479
+ */
480
+ class EventEmitter {
481
+ /**
482
+ * Mixes EventEmitter methods into the given object.
483
+ * @template T
484
+ * @param {T} obj - The target object to mix methods into.
485
+ * @returns {T} The modified object with EventEmitter methods.
486
+ */
487
+ static mixin (obj) {
488
+ const emitter = new EventEmitter();
489
+ // @ts-ignore
490
+ obj.__emitter = emitter;
491
+ for (const name of MixinMethods) {
492
+ // @ts-ignore
493
+ const method = emitter[name];
494
+ // @ts-ignore
495
+ obj[name] = method.bind(emitter);
496
+ }
497
+ return obj
498
+ }
499
+
500
+ static get defaultMaxListeners () {
501
+ return DefaultMaxListeners
502
+ }
503
+
504
+ static set defaultMaxListeners (maxListeners) {
505
+ assertNumber(maxListeners);
506
+ DefaultMaxListeners = maxListeners ?? 10;
507
+ }
508
+
509
+ /**
510
+ */
511
+ constructor () {
512
+ /**
513
+ * @type {Map<string|Symbol, Event>}
514
+ */
515
+ this._name2Event = new Map();
516
+ this._maxListeners = DefaultMaxListeners;
517
+ }
518
+
519
+ /**
520
+ * Alias of {@linkcode EventEmitter.addListener}
521
+ * @param {string|Symbol} eventName
522
+ * @param {function} listener
523
+ * @param {*} [owner]
524
+ * @returns {this}
525
+ */
526
+ addListener (eventName, listener, owner) {
527
+ return this.on(eventName, listener, owner)
528
+ }
529
+
530
+ /**
531
+ * Adds the listener function to the beginning of the listeners array for the event named eventName.
532
+ * @param {string|Symbol} eventName
533
+ * @param {function} listener
534
+ * @param {*} [owner]
535
+ * @returns {this}
536
+ */
537
+ prependListener (eventName, listener, owner) {
538
+ assertString(eventName);
539
+ assertFunction(listener);
540
+ this._checkMaxListeners(eventName);
541
+
542
+ const event = this._getOrCreateEvent(eventName);
543
+ event.prependListener(listener, owner);
544
+ return this
545
+ }
546
+
547
+ /**
548
+ * Adds a one-time listener function for the event named eventName to the beginning of the listeners array.
549
+ * @param {string|Symbol} eventName
550
+ * @param {function} listener
551
+ * @param {*} [owner]
552
+ * @returns {this}
553
+ */
554
+ prependOnceListener (eventName, listener, owner) {
555
+ assertString(eventName);
556
+ assertFunction(listener);
557
+ this._checkMaxListeners(eventName);
558
+
559
+ const event = this._getOrCreateEvent(eventName);
560
+ event.prependOnceListener(listener, owner);
561
+ return this
562
+ }
563
+
564
+ /**
565
+ * Synchronously calls each of the listeners registered for the event named eventName,
566
+ * in the order they were registered, passing the supplied arguments to each.
567
+ * Returns true if the event had listeners, false otherwise.
568
+ * @param {string|Symbol} eventName - The name of the event.
569
+ * @param {...*} args arguments to pass to the listeners.
570
+ * @returns {boolean}
571
+ */
572
+ emit (eventName, ...args) {
573
+ const event = this._name2Event.get(eventName);
574
+ if (event == null || event.isEmpty()) {
575
+ return false
576
+ }
577
+ event.emit(...args);
578
+ return true
579
+ }
580
+
581
+ /**
582
+ * Returns an array listing the events for which the emitter has registered listeners.
583
+ * @returns {Array<string|Symbol>} An array of event names.
584
+ */
585
+ eventNames () {
586
+ return [...this._name2Event.keys()]
587
+ }
588
+
589
+ getMaxListeners () {
590
+ return this._maxListeners
591
+ }
592
+
593
+ /**
594
+ * Returns the number of listeners listening for the event named eventName.
595
+ * If listener is provided, it will return how many times the listener is found
596
+ * in the list of the listeners of the event.
597
+ * @param {string|Symbol} eventName - The name of the event to check
598
+ * @param {function} [listener] - Optional specific listener to count
599
+ * @returns {number} The number of listeners for the event
600
+ */
601
+ listenerCount (eventName, listener) {
602
+ assertStringOrSymbol(eventName, 'eventName');
603
+ const event = this._name2Event.get(eventName);
604
+ if (event == null || event.isEmpty()) {
605
+ return 0
606
+ }
607
+ return event.listenerCount(listener)
608
+ }
609
+
610
+ /**
611
+ * Returns a copy of the array of listeners for the event named eventName.
612
+ * 1. if a callback function added multiple times, it will be returned multiple times.
613
+ * @param {string} eventName
614
+ * @returns {Array<function>}
615
+ */
616
+ listeners (eventName) {
617
+ assertStringOrSymbol(eventName, 'eventName');
618
+ const event = this._name2Event.get(eventName);
619
+ if (event == null || event.isEmpty()) {
620
+ return []
621
+ }
622
+ return event.callbacks()
623
+ }
624
+
625
+ /**
626
+ * Removes a callback function from a specific event.
627
+ * 1. If the event doesn't exist or has no listeners, do nothing
628
+ * 2. if one callback function added multiple times, all of them will be removed.
629
+ * * !!! This is different from the behavior of Node.js EventEmitter.
630
+ *
631
+ * @param {string} eventName - The name of the event to remove callback from
632
+ * @param {function} callback - The callback function to remove
633
+ * @returns {EventEmitter} Returns the emitter instance for chaining
634
+ */
635
+ off (eventName, callback) {
636
+ const event = this._name2Event.get(eventName);
637
+ if (event == null) {
638
+ return this
639
+ }
640
+ event.removeListener(callback);
641
+ if (event.isEmpty()) {
642
+ this._name2Event.delete(eventName);
643
+ return this
644
+ }
645
+ return this
646
+ }
647
+
648
+ /**
649
+ * Removes all listeners for the specified event.
650
+ * if owner is limited, only the listeners of the owner will be removed.
651
+ *
652
+ * @param {string|Symbol} eventName - The name of the event to clear listeners for.
653
+ * @param {*} owner - The owner whose listeners should be removed.
654
+ * @returns {EventEmitter} The emitter instance for chaining.
655
+ */
656
+ offAll (eventName, owner) {
657
+ assertStringOrSymbol(eventName, 'eventName');
658
+ const event = this._name2Event.get(eventName);
659
+ if (event == null) {
660
+ return this
661
+ }
662
+ event.removeAllListeners(owner);
663
+ if (event.isEmpty()) {
664
+ this._name2Event.delete(eventName);
665
+ return this
666
+ }
667
+ return this
668
+ }
669
+
670
+ /**
671
+ * Removes all event listeners belonging to the specified owner.
672
+ * @param {*} owner - The owner whose listeners should be removed.
673
+ * @returns {this}
674
+ */
675
+ offOwner (owner) {
676
+ assertNotNil(owner, 'owner');
677
+ const events = [...this._name2Event.values()];
678
+ for (const event of events) {
679
+ event.removeAllListeners(owner);
680
+ if (event.isEmpty()) {
681
+ this._name2Event.delete(event.name);
682
+ }
683
+ }
684
+ return this
685
+ }
686
+
687
+ /**
688
+ * Adds the listener function to the end of the listeners array for the event named eventName.
689
+ * @param {string|Symbol} eventName
690
+ * @param {function} callback
691
+ * @param {*} [owner]
692
+ * @returns {this}
693
+ */
694
+ on (eventName, callback, owner) {
695
+ assertString(eventName);
696
+ assertFunction(callback);
697
+ this._checkMaxListeners(eventName);
698
+
699
+ const event = this._getOrCreateEvent(eventName);
700
+ event.addListener(callback, owner);
701
+ return this
702
+ }
703
+
704
+ /**
705
+ * Checks if the number of listeners for the given event exceeds the maximum allowed.
706
+ * Emits a warning if the listener count reaches or exceeds the maxListeners threshold.
707
+ * @private
708
+ * @param {string|Symbol} eventName - The name of the event to check
709
+ * @returns {void}
710
+ */
711
+ _checkMaxListeners (eventName) {
712
+ let listenerCount = 0;
713
+ if (this._maxListeners !== 0 && this._maxListeners !== Infinity && (listenerCount = this.listenerCount(eventName)) >= this._maxListeners) {
714
+ console.warn(`maxlistenersexceededwarning: Possible EventEmitter memory leak detected. ${listenerCount} ${eventName} listeners added to [${this}]. Use emitter.setMaxListeners() to increase limit`);
715
+ }
716
+ }
717
+
718
+ /**
719
+ * Adds a listener that will be invoked only once for the event named eventName.SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit`)
720
+ }
721
+ }
722
+
723
+ /**
724
+ * Adds a one-time callback function for the specified event.
725
+ * The callback is invoked only once and then automatically removed.
726
+ *
727
+ * @param {string} eventName - The name of the event to listen for.
728
+ * @param {Function} callback - The callback function to execute when the event is emitted.
729
+ * @param {*} [owner] - Optional owner used to group listener callbacks.
730
+ * @returns {EventEmitter} Returns the emitter instance for chaining.
731
+ */
732
+ once (eventName, callback, owner) {
733
+ assertString(eventName);
734
+ assertFunction(callback);
735
+
736
+ const event = this._getOrCreateEvent(eventName);
737
+ event.addOnceListener(callback, owner);
738
+ return this
739
+ }
740
+
741
+ /**
742
+ * Returns a copy of the array of listeners for the event named eventName,
743
+ * including any wrappers (such as those created by .once()).
744
+ * @param {string} eventName - The name of the event.
745
+ * @returns {Listener[]} An array of raw listener functions, or empty array if none exist.
746
+ */
747
+ rawListeners (eventName) {
748
+ return this._name2Event.get(eventName)?.rawListeners() || []
749
+ }
750
+
751
+ /**
752
+ * Alias of {@linkcode EventEmitter.offAll}
753
+ * @param {string|Symbol} eventName - The name of the event to remove listeners for.
754
+ * @param {*} owner - The owner of the listeners to be removed.
755
+ * @returns {EventEmitter} The emitter instance for chaining.
756
+ */
757
+ removeAllListeners (eventName, owner) {
758
+ return this.offAll(eventName, owner)
759
+ }
760
+
761
+ /**
762
+ * Alias of {@linkcode EventEmitter.off}
763
+ * @param {string} eventName - The name of the event.
764
+ * @param {Function} callback - The callback function to remove.
765
+ * @returns {EventEmitter} The emitter instance for chaining.
766
+ */
767
+ removeListener (eventName, callback) {
768
+ return this.off(eventName, callback)
769
+ }
770
+
771
+ /**
772
+ * Sets the maximum number of listeners that can be added to this event emitter.
773
+ * @param {number} max - The maximum number of listeners.
774
+ * @returns {this} The set maximum number of listeners.
775
+ */
776
+ setMaxListeners (max) {
777
+ assertNumber(max);
778
+ if (max < 0) {
779
+ throw new RangeError('maxListeners must >=0')
780
+ }
781
+ this._maxListeners = max;
782
+ return this
783
+ }
784
+
785
+ /**
786
+ * Gets an existing event by name or creates a new one if it doesn't exist.
787
+ * @private
788
+ * @param {string|Symbol} eventName - The name of the event to get or create.
789
+ * @returns {Event} The existing or newly created Event instance.
790
+ */
791
+ _getOrCreateEvent (eventName) {
792
+ if (this._name2Event.has(eventName)) {
793
+ // @ts-ignore
794
+ return this._name2Event.get(eventName)
795
+ }
796
+ const event = new Event(eventName);
797
+ this._name2Event.set(eventName, event);
798
+ return event
799
+ }
800
+
801
+ /**
802
+ * Checks if the specified owner has any registered events.
803
+ * @param {Object} owner - The owner object to check for registered events.
804
+ * @returns {boolean} True if the owner has any registered events, false otherwise.
805
+ */
806
+ hasOwner (owner) {
807
+ if (isNil(owner)) {
808
+ return false
809
+ }
810
+ for (const event of this._name2Event.values()) {
811
+ if (event.hasOwner(owner)) {
812
+ return true
813
+ }
814
+ }
815
+ return false
816
+ }
817
+ }
818
+
819
+ exports.EventEmitter = EventEmitter;
820
+ exports.default = EventEmitter;
821
+ //# sourceMappingURL=index-dev.cjs.map