@creejs/commons-events 2.0.2 → 2.0.3

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