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