@creejs/commons-events 1.0.3 → 1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creejs/commons-events",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Commons EventEmitter",
5
5
  "main": "index.js",
6
6
  "private": false,
@@ -0,0 +1 @@
1
+ export const DefaultOwner: "DOwner$#$";
@@ -0,0 +1,178 @@
1
+ export = EventEmitter;
2
+ /**
3
+ * 1. An EventEmitter follows the API of NodeJS EventEmitter.
4
+ * 2. Enhancement:
5
+ * * Listeners are grouped by "owner".
6
+ * * Operation via "owner"
7
+ * * Duplicate listeners are filtered out. Only unique One kept.
8
+ */
9
+ declare class EventEmitter {
10
+ /**
11
+ * Mixes EventEmitter methods into the given object.
12
+ * @template T
13
+ * @param {T} obj - The target object to mix methods into.
14
+ * @returns {T} The modified object with EventEmitter methods.
15
+ */
16
+ static mixin<T>(obj: T): T;
17
+ static set defaultMaxListeners(maxListeners: number);
18
+ static get defaultMaxListeners(): number;
19
+ /**
20
+ * @type {Map<string|Symbol, Event>}
21
+ */
22
+ _name2Event: Map<string | Symbol, Event>;
23
+ _maxListeners: number;
24
+ /**
25
+ * Alias of {@linkcode EventEmitter.addListener}
26
+ * @param {string|Symbol} eventName
27
+ * @param {function} listener
28
+ * @param {*} [owner]
29
+ * @returns {this}
30
+ */
31
+ addListener(eventName: string | Symbol, listener: Function, owner?: any): this;
32
+ /**
33
+ * Adds the listener function to the beginning of the listeners array for the event named eventName.
34
+ * @param {string|Symbol} eventName
35
+ * @param {function} listener
36
+ * @param {*} [owner]
37
+ * @returns {this}
38
+ */
39
+ prependListener(eventName: string | Symbol, listener: Function, owner?: any): this;
40
+ /**
41
+ * Adds a one-time listener function for the event named eventName to the beginning of the listeners array.
42
+ * @param {string|Symbol} eventName
43
+ * @param {function} listener
44
+ * @param {*} [owner]
45
+ * @returns {this}
46
+ */
47
+ prependOnceListener(eventName: string | Symbol, listener: Function, owner?: any): this;
48
+ /**
49
+ * Synchronously calls each of the listeners registered for the event named eventName,
50
+ * in the order they were registered, passing the supplied arguments to each.
51
+ * Returns true if the event had listeners, false otherwise.
52
+ * @param {string|Symbol} eventName - The name of the event.
53
+ * @param {...*} args arguments to pass to the listeners.
54
+ * @returns {boolean}
55
+ */
56
+ emit(eventName: string | Symbol, ...args: any[]): boolean;
57
+ /**
58
+ * Returns an array listing the events for which the emitter has registered listeners.
59
+ * @returns {Array<string|Symbol>} An array of event names.
60
+ */
61
+ eventNames(): Array<string | Symbol>;
62
+ getMaxListeners(): number;
63
+ /**
64
+ * Returns the number of listeners listening for the event named eventName.
65
+ * If listener is provided, it will return how many times the listener is found
66
+ * in the list of the listeners of the event.
67
+ * @param {string|Symbol} eventName - The name of the event to check
68
+ * @param {function} [listener] - Optional specific listener to count
69
+ * @returns {number} The number of listeners for the event
70
+ */
71
+ listenerCount(eventName: string | Symbol, listener?: Function): number;
72
+ /**
73
+ * Returns a copy of the array of listeners for the event named eventName.
74
+ * 1. if a callback function added multiple times, it will be returned multiple times.
75
+ * @param {string} eventName
76
+ * @returns {Array<function>}
77
+ */
78
+ listeners(eventName: string): Array<Function>;
79
+ /**
80
+ * Removes a callback function from a specific event.
81
+ * 1. If the event doesn't exist or has no listeners, do nothing
82
+ * 2. if one callback function added multiple times, all of them will be removed.
83
+ * * !!! This is different from the behavior of Node.js EventEmitter.
84
+ *
85
+ * @param {string} eventName - The name of the event to remove callback from
86
+ * @param {function} callback - The callback function to remove
87
+ * @returns {EventEmitter} Returns the emitter instance for chaining
88
+ */
89
+ off(eventName: string, callback: Function): EventEmitter;
90
+ /**
91
+ * Removes all listeners for the specified event.
92
+ * if owner is limited, only the listeners of the owner will be removed.
93
+ *
94
+ * @param {string|Symbol} eventName - The name of the event to clear listeners for.
95
+ * @param {*} owner - The owner whose listeners should be removed.
96
+ * @returns {EventEmitter} The emitter instance for chaining.
97
+ */
98
+ offAll(eventName: string | Symbol, owner: any): EventEmitter;
99
+ /**
100
+ * Removes all event listeners belonging to the specified owner.
101
+ * @param {*} owner - The owner whose listeners should be removed.
102
+ * @returns {this}
103
+ */
104
+ offOwner(owner: any): this;
105
+ /**
106
+ * Adds the listener function to the end of the listeners array for the event named eventName.
107
+ * @param {string|Symbol} eventName
108
+ * @param {function} callback
109
+ * @param {*} [owner]
110
+ * @returns {this}
111
+ */
112
+ on(eventName: string | Symbol, callback: Function, owner?: any): this;
113
+ /**
114
+ * Checks if the number of listeners for the given event exceeds the maximum allowed.
115
+ * Emits a warning if the listener count reaches or exceeds the maxListeners threshold.
116
+ * @private
117
+ * @param {string|Symbol} eventName - The name of the event to check
118
+ * @returns {void}
119
+ */
120
+ private _checkMaxListeners;
121
+ /**
122
+ * 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`)
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Adds a one-time callback function for the specified event.
128
+ * The callback is invoked only once and then automatically removed.
129
+ *
130
+ * @param {string} eventName - The name of the event to listen for.
131
+ * @param {Function} callback - The callback function to execute when the event is emitted.
132
+ * @param {*} [owner] - Optional owner used to group listener callbacks.
133
+ * @returns {EventEmitter} Returns the emitter instance for chaining.
134
+ */
135
+ once(eventName: string, callback: Function, owner?: any): EventEmitter;
136
+ /**
137
+ * Returns a copy of the array of listeners for the event named eventName,
138
+ * including any wrappers (such as those created by .once()).
139
+ * @param {string} eventName - The name of the event.
140
+ * @returns {Listener[]} An array of raw listener functions, or empty array if none exist.
141
+ */
142
+ rawListeners(eventName: string): Listener[];
143
+ /**
144
+ * Alias of {@linkcode EventEmitter.offAll}
145
+ * @param {string|Symbol} eventName - The name of the event to remove listeners for.
146
+ * @param {*} owner - The owner of the listeners to be removed.
147
+ * @returns {EventEmitter} The emitter instance for chaining.
148
+ */
149
+ removeAllListeners(eventName: string | Symbol, owner: any): EventEmitter;
150
+ /**
151
+ * Alias of {@linkcode EventEmitter.off}
152
+ * @param {string} eventName - The name of the event.
153
+ * @param {Function} callback - The callback function to remove.
154
+ * @returns {EventEmitter} The emitter instance for chaining.
155
+ */
156
+ removeListener(eventName: string, callback: Function): EventEmitter;
157
+ /**
158
+ * Sets the maximum number of listeners that can be added to this event emitter.
159
+ * @param {number} max - The maximum number of listeners.
160
+ * @returns {this} The set maximum number of listeners.
161
+ */
162
+ setMaxListeners(max: number): this;
163
+ /**
164
+ * Gets an existing event by name or creates a new one if it doesn't exist.
165
+ * @private
166
+ * @param {string|Symbol} eventName - The name of the event to get or create.
167
+ * @returns {Event} The existing or newly created Event instance.
168
+ */
169
+ private _getOrCreateEvent;
170
+ /**
171
+ * Checks if the specified owner has any registered events.
172
+ * @param {Object} owner - The owner object to check for registered events.
173
+ * @returns {boolean} True if the owner has any registered events, false otherwise.
174
+ */
175
+ hasOwner(owner: Object): boolean;
176
+ }
177
+ import Event = require("./event");
178
+ import Listener = require("./listener");
@@ -0,0 +1,142 @@
1
+ export = Event;
2
+ /**
3
+ * An Event definition
4
+ * 1. listeners are grouped by owner
5
+ * * if not specified, group it into one Default owner
6
+ * 2. one listener may belong to multiple owners
7
+ * 3. one owner may have multiple listeners
8
+ */
9
+ declare class Event {
10
+ static get DefaultOwner(): string;
11
+ /**
12
+ * Creates a new Event instance with the specified name.
13
+ * @param {string|Symbol} eventName - The name of the event.
14
+ */
15
+ constructor(eventName: string | Symbol);
16
+ _name: string | Symbol;
17
+ /**
18
+ * use Set to store unique listeners
19
+ * @type {Set<function>}
20
+ */
21
+ _callbacks: Set<Function>;
22
+ /**
23
+ * @type {Array<Listener>}
24
+ */
25
+ _listeners: Array<Listener>;
26
+ /**
27
+ * @type {Map<function, Set<Listener>>}
28
+ */
29
+ _callback2Listeners: Map<Function, Set<Listener>>;
30
+ /**
31
+ * use listener to index owners. One Listener Instance only belongs to one owner
32
+ * @type {Map<Listener, *>}
33
+ */
34
+ _listener2Owner: Map<Listener, any>;
35
+ /**
36
+ * use "owner" to group listeners; one owner may have multiple listeners
37
+ * @type {Map<*, Set<Listener>>}
38
+ */
39
+ _owner2Listeners: Map<any, Set<Listener>>;
40
+ /**
41
+ * Name of the event.
42
+ * @returns {string|Symbol}
43
+ */
44
+ get name(): string | Symbol;
45
+ isEmpty(): boolean;
46
+ /**
47
+ * Returns a copy of the raw listeners array.
48
+ * @returns {Array<Listener>} A shallow copy of the listeners array.
49
+ */
50
+ rawListeners(): Array<Listener>;
51
+ /**
52
+ * Returns the number of listeners listening for the event.
53
+ * If callback is provided, it will return how many times the callback is found in the list of the listeners
54
+ * @param {Function} [callback] - The callback function to count
55
+ * @returns {number}
56
+ */
57
+ listenerCount(callback?: Function): number;
58
+ /**
59
+ * Returns a shallow copy of the registered callbacks array.
60
+ * if one callback function is added multiple times, it will be returned multiple times.
61
+ * @returns {Array<function>} A new array containing all registered callbacks.
62
+ */
63
+ callbacks(): Array<Function>;
64
+ /**
65
+ * Emits current event, invoking all registered listeners by order.
66
+ * @param {...*} args - Arguments to be passed to each listener.
67
+ * @returns {boolean} True if the event had listeners, false otherwise.
68
+ */
69
+ emit(...args: any[]): boolean;
70
+ /**
71
+ * Checks if listener is registered with this event.
72
+ * @param {function} callback - The listener function to check.
73
+ * @returns {boolean} True if the listener is registered, false otherwise.
74
+ */
75
+ hasListener(callback: Function): boolean;
76
+ /**
77
+ * Checks if owner has any registered listeners.
78
+ * @param {*} owner - The owner to check for registered listeners.
79
+ * @returns {boolean} True if the owner has listeners, false otherwise.
80
+ */
81
+ hasOwner(owner: any): boolean;
82
+ /**
83
+ * Adds an event listener
84
+ * @param {function} callback - The callback function to be invoked when the event occurs.
85
+ * @param {*} [owner] - use "owner" to group listeners, then can remove all listeners for an owner.
86
+ * @returns {boolean} true if added, false otherwise.
87
+ */
88
+ addListener(callback: Function, owner?: any): boolean;
89
+ /**
90
+ * Prepends a listener callback to the beginning of the listeners array.
91
+ * No checks are made to see if the listener has already been added.
92
+ * Multiple calls passing the same combination of eventName and listener will result in the listener being added,
93
+ * and called, multiple times.
94
+ * @param {Function} callback - The callback function to be executed when the event is emitted.
95
+ * @param {Object} owner - The owner object to which the listener belongs.
96
+ * @returns {boolean}
97
+ */
98
+ prependListener(callback: Function, owner: Object): boolean;
99
+ /**
100
+ * Adds a one-time listener for the event. The listener is automatically removed after being invoked once.
101
+ * @param {Function} callback - The function to call when the event is triggered.
102
+ * @param {Object} [owner] - The object that owns the callback (used for binding `this` context).
103
+ * @returns {boolean}
104
+ */
105
+ addOnceListener(callback: Function, owner?: Object): boolean;
106
+ /**
107
+ * Adds a one-time event listener that will be automatically removed after being triggered once.
108
+ * The listener will only be triggered if the event is pretended (simulated).
109
+ * @param {Function} callback - The function to call when the event is triggered.
110
+ * @param {Object} owner - The object that owns the listener (used for reference tracking).
111
+ * @returns {boolean} The listener function that was added.
112
+ */
113
+ prependOnceListener(callback: Function, owner: Object): boolean;
114
+ /**
115
+ * Adds a listener for the event.
116
+ * @param {Function} callback - The callback function to be executed when the event occurs
117
+ * @param {*} [owner] - The owner object that the listener belongs to (defaults to DeaultOwner)
118
+ * @param {boolean} [isOnce=false] - Whether the listener should be removed after first invocation
119
+ * @param {boolean} [isPrepend=false] - Whether the listener should be inert at the beginning of the listeners array
120
+ * @returns {boolean} Returns true if listener was added successfully, false if callback is nil or duplicate
121
+ * @protected
122
+ */
123
+ protected _addListener(callback: Function, owner?: any, isOnce?: boolean, isPrepend?: boolean): boolean;
124
+ /**
125
+ * Removes a callback
126
+ * @param {Function} callback - The callback function to remove.
127
+ * @returns {boolean} true if removed, false otherwise.
128
+ */
129
+ removeListener(callback: Function): boolean;
130
+ /**
131
+ * Removes a listener from both global and owner indexes.
132
+ * @param {Listener} listener - The listener function to remove
133
+ */
134
+ _remove(listener: Listener): void;
135
+ /**
136
+ * Removes all event listeners
137
+ * @param {*} [owner] - Without owner, all listeners will be removed; otherwise, only listeners of "owner" will be removed.
138
+ * @returns {this}
139
+ */
140
+ removeAllListeners(owner?: any): this;
141
+ }
142
+ import Listener = require("./listener");
@@ -0,0 +1,2 @@
1
+ export { EventEmitter };
2
+ import EventEmitter = require("./event-emitter");
@@ -0,0 +1,46 @@
1
+ export = Listener;
2
+ /**
3
+ * Wraps a function to be called when an event is fired.
4
+ * @class Listener
5
+ */
6
+ declare class Listener {
7
+ /**
8
+ * @param {Event} event
9
+ * @param {function} callback - The function to be called when event is fired
10
+ * @param {boolean} [isOnce=false] - is a one time listener?
11
+ */
12
+ constructor(event: Event, callback: Function, isOnce?: boolean);
13
+ _event: Event;
14
+ _callback: Function;
15
+ _isOnce: boolean;
16
+ _owner: any;
17
+ /**
18
+ * Sets the owner of this listener.
19
+ * @param {*} owner - The owner object to be associated with this listener.
20
+ */
21
+ set owner(owner: any);
22
+ get owner(): any;
23
+ get event(): Event;
24
+ get isOnce(): boolean;
25
+ /**
26
+ * Checks if the provided function is the same as the listener's wrapped function.
27
+ * @param {Function} callback - The function to compare against.
28
+ * @returns {boolean} True if the functions are the same, false otherwise.
29
+ */
30
+ isSameCallback(callback: Function): boolean;
31
+ get callback(): Function;
32
+ /**
33
+ * Invokes the stored function with the provided arguments.
34
+ * @param {...*} args - Arguments to pass to the function.
35
+ * @returns {*} The result of the function invocation.
36
+ */
37
+ invoke(...args: any[]): any;
38
+ /**
39
+ * Invokes the listener with the provided arguments.
40
+ * Alias for {@linkcode Listener.invoke}
41
+ * @param {...*} args - Arguments to be passed to the listener.
42
+ * @returns {*} The result of the listener invocation.
43
+ */
44
+ listener(...args: any[]): any;
45
+ }
46
+ import Event = require("./event");