@carry0987/event-emitter
Advanced tools
Comparing version 1.4.3 to 1.4.4
class EventEmitter { | ||
// Initialize callbacks with an empty object | ||
callbacks = {}; | ||
/** | ||
* Initializes the callbacks for a given event. If the event does not already have | ||
* an entry in the callbacks object, a new empty array is created for it. | ||
* @param event - The name of the event to initialize. If not provided, it checks | ||
* for undefined events and initializes them if needed. | ||
*/ | ||
init(event) { | ||
@@ -9,2 +15,8 @@ if (event && !this.callbacks[event]) { | ||
} | ||
/** | ||
* Checks if a listener is a valid function. Throws a TypeError if the listener | ||
* is not a function. | ||
* @param listener - The listener to check. Should be a function that either returns void | ||
* or a Promise that resolves to void. | ||
*/ | ||
checkListener(listener) { | ||
@@ -15,11 +27,36 @@ if (typeof listener !== 'function') { | ||
} | ||
/** | ||
* Checks whether a specific event has been registered within the emitter. | ||
* @param event - The name of the event to check for existence. | ||
* @returns A boolean indicating whether the event exists in the callbacks. | ||
*/ | ||
hasEvent(event) { | ||
return this.callbacks[event] !== undefined; | ||
} | ||
/** | ||
* Retrieves all the listeners currently registered to the emitter. | ||
* @returns An object containing all registered events and their associated listeners. | ||
* Each key is a string representing the event name, mapping to an array of | ||
* listener functions. | ||
*/ | ||
listeners() { | ||
return this.callbacks; | ||
} | ||
/** | ||
* Adds a listener function for the specified event. This method is an alias for the | ||
* `on` method, purely for semantic purposes. | ||
* @param event - The name of the event to listen to. | ||
* @param listener - The function to invoke when the event is emitted. Can be asynchronous. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
addListener(event, listener) { | ||
return this.on(event, listener); | ||
} | ||
/** | ||
* Clears all listeners for a specific event or, if no event is provided, clears all | ||
* listeners for all events. | ||
* @param event - Optional. The name of the event whose listeners should be cleared. | ||
* If omitted, all event listeners are cleared. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
clearListener(event) { | ||
@@ -34,2 +71,9 @@ if (event) { | ||
} | ||
/** | ||
* Adds a listener for a specific event type. Initializes the event if it's not already | ||
* present and ensures the listener is valid. | ||
* @param event - The name of the event to listen to. | ||
* @param listener - The function to call when the event is emitted. Can return a promise. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
on(event, listener) { | ||
@@ -41,4 +85,14 @@ this.checkListener(listener); | ||
} | ||
/** | ||
* Removes a listener from a specific event. If no listener is provided, all listeners | ||
* for the event are removed. | ||
* @param event - The name of the event to remove a listener from. | ||
* @param listener - Optional. The specific listener to remove. If not provided, all | ||
* listeners for the event are removed. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
off(event, listener) { | ||
this.checkListener(listener); | ||
if (listener) { | ||
this.checkListener(listener); | ||
} | ||
const eventName = event; | ||
@@ -50,5 +104,20 @@ this.init(); | ||
} | ||
this.callbacks[eventName] = this.callbacks[eventName].filter((value) => value != listener); | ||
if (listener) { | ||
this.callbacks[eventName] = this.callbacks[eventName].filter((value) => value !== listener); | ||
} | ||
else { | ||
// Remove all listeners if no specific listener is provided | ||
this.callbacks[eventName] = []; | ||
} | ||
return this; | ||
} | ||
/** | ||
* Emits an event, invoking all registered listeners for that event with the provided | ||
* arguments. If any listener returns a promise, the method itself will return a promise | ||
* that resolves when all listeners have been processed. | ||
* @param event - The name of the event to emit. | ||
* @param args - Arguments to pass to each listener when invoked. | ||
* @returns A boolean or a promise resolving to a boolean indicating if listeners were | ||
* successfully called and resolved/ran without error. | ||
*/ | ||
emit(event, ...args) { | ||
@@ -90,2 +159,9 @@ const eventName = event; | ||
} | ||
/** | ||
* Adds a listener for a specific event that will only be invoked once. After the first | ||
* invocation, the listener will be automatically removed. | ||
* @param event - The name of the event to listen to once. | ||
* @param listener - The function to invoke once when the event is emitted. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
once(event, listener) { | ||
@@ -105,4 +181,4 @@ this.checkListener(listener); | ||
const version = '1.4.3'; | ||
const version = '1.4.4'; | ||
export { EventEmitter, version }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).eventEmitter={})}(this,(function(e){"use strict";e.EventEmitter=class{callbacks={};init(e){e&&!this.callbacks[e]&&(this.callbacks[e]=[])}checkListener(e){if("function"!=typeof e)throw new TypeError("The listener must be a function")}hasEvent(e){return void 0!==this.callbacks[e]}listeners(){return this.callbacks}addListener(e,t){return this.on(e,t)}clearListener(e){return e?this.callbacks[e]=[]:this.callbacks={},this}on(e,t){return this.checkListener(t),this.init(e),this.callbacks[e].push(t),this}off(e,t){this.checkListener(t);const s=e;return this.init(),this.callbacks[s]&&0!==this.callbacks[s].length?(this.callbacks[s]=this.callbacks[s].filter((e=>e!=t)),this):this}emit(e,...t){const s=e;if(this.init(s),this.callbacks[s].length<=0)return!1;const i=this.callbacks[s].map((e=>{try{const s=e(...t);return s instanceof Promise?s:Promise.resolve(s)}catch(e){return console.error(`Error in event listener for event: ${s}`,e),Promise.resolve()}}));return!i.some((e=>e instanceof Promise))||Promise.all(i).then((()=>!0)).catch((e=>(console.error(`Error handling promises for event: ${s}`,e),!1)))}once(e,t){this.checkListener(t);const s=(...i)=>{const n=t(...i);return this.off(e,s),n instanceof Promise?n:Promise.resolve(n)};return this.on(e,s)}},e.version="1.4.3"})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).eventEmitter={})}(this,(function(e){"use strict";e.EventEmitter=class{callbacks={};init(e){e&&!this.callbacks[e]&&(this.callbacks[e]=[])}checkListener(e){if("function"!=typeof e)throw new TypeError("The listener must be a function")}hasEvent(e){return void 0!==this.callbacks[e]}listeners(){return this.callbacks}addListener(e,t){return this.on(e,t)}clearListener(e){return e?this.callbacks[e]=[]:this.callbacks={},this}on(e,t){return this.checkListener(t),this.init(e),this.callbacks[e].push(t),this}off(e,t){t&&this.checkListener(t);const s=e;return this.init(),this.callbacks[s]&&0!==this.callbacks[s].length?(this.callbacks[s]=t?this.callbacks[s].filter((e=>e!==t)):[],this):this}emit(e,...t){const s=e;if(this.init(s),this.callbacks[s].length<=0)return!1;const i=this.callbacks[s].map((e=>{try{const s=e(...t);return s instanceof Promise?s:Promise.resolve(s)}catch(e){return console.error(`Error in event listener for event: ${s}`,e),Promise.resolve()}}));return!i.some((e=>e instanceof Promise))||Promise.all(i).then((()=>!0)).catch((e=>(console.error(`Error handling promises for event: ${s}`,e),!1)))}once(e,t){this.checkListener(t);const s=(...i)=>{const n=t(...i);return this.off(e,s),n instanceof Promise?n:Promise.resolve(n)};return this.on(e,s)}},e.version="1.4.4"})); |
@@ -5,13 +5,81 @@ type EventArgs<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T]; | ||
private callbacks; | ||
/** | ||
* Initializes the callbacks for a given event. If the event does not already have | ||
* an entry in the callbacks object, a new empty array is created for it. | ||
* @param event - The name of the event to initialize. If not provided, it checks | ||
* for undefined events and initializes them if needed. | ||
*/ | ||
private init; | ||
/** | ||
* Checks if a listener is a valid function. Throws a TypeError if the listener | ||
* is not a function. | ||
* @param listener - The listener to check. Should be a function that either returns void | ||
* or a Promise that resolves to void. | ||
*/ | ||
private checkListener; | ||
/** | ||
* Checks whether a specific event has been registered within the emitter. | ||
* @param event - The name of the event to check for existence. | ||
* @returns A boolean indicating whether the event exists in the callbacks. | ||
*/ | ||
hasEvent(event: string): boolean; | ||
/** | ||
* Retrieves all the listeners currently registered to the emitter. | ||
* @returns An object containing all registered events and their associated listeners. | ||
* Each key is a string representing the event name, mapping to an array of | ||
* listener functions. | ||
*/ | ||
listeners(): { | ||
[event: string]: ((...args: any[]) => void | Promise<void>)[]; | ||
}; | ||
/** | ||
* Adds a listener function for the specified event. This method is an alias for the | ||
* `on` method, purely for semantic purposes. | ||
* @param event - The name of the event to listen to. | ||
* @param listener - The function to invoke when the event is emitted. Can be asynchronous. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
addListener<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
/** | ||
* Clears all listeners for a specific event or, if no event is provided, clears all | ||
* listeners for all events. | ||
* @param event - Optional. The name of the event whose listeners should be cleared. | ||
* If omitted, all event listeners are cleared. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
clearListener<EventName extends keyof EventTypes>(event?: EventName): EventEmitter<EventTypes>; | ||
/** | ||
* Adds a listener for a specific event type. Initializes the event if it's not already | ||
* present and ensures the listener is valid. | ||
* @param event - The name of the event to listen to. | ||
* @param listener - The function to call when the event is emitted. Can return a promise. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
on<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
off<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
/** | ||
* Removes a listener from a specific event. If no listener is provided, all listeners | ||
* for the event are removed. | ||
* @param event - The name of the event to remove a listener from. | ||
* @param listener - Optional. The specific listener to remove. If not provided, all | ||
* listeners for the event are removed. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
off<EventName extends keyof EventTypes>(event: EventName, listener?: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
/** | ||
* Emits an event, invoking all registered listeners for that event with the provided | ||
* arguments. If any listener returns a promise, the method itself will return a promise | ||
* that resolves when all listeners have been processed. | ||
* @param event - The name of the event to emit. | ||
* @param args - Arguments to pass to each listener when invoked. | ||
* @returns A boolean or a promise resolving to a boolean indicating if listeners were | ||
* successfully called and resolved/ran without error. | ||
*/ | ||
emit<EventName extends keyof EventTypes>(event: EventName, ...args: EventArgs<EventTypes[EventName]>): boolean | Promise<boolean>; | ||
/** | ||
* Adds a listener for a specific event that will only be invoked once. After the first | ||
* invocation, the listener will be automatically removed. | ||
* @param event - The name of the event to listen to once. | ||
* @param listener - The function to invoke once when the event is emitted. | ||
* @returns The instance of the EventEmitter for method chaining. | ||
*/ | ||
once<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
@@ -18,0 +86,0 @@ } |
{ | ||
"name": "@carry0987/event-emitter", | ||
"version": "1.4.3", | ||
"version": "1.4.4", | ||
"description": "EventEmitter is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application.", | ||
@@ -34,7 +34,7 @@ "type": "module", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"rollup": "^4.18.0", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup": "^4.21.2", | ||
"rollup-plugin-delete": "^2.1.0", | ||
"rollup-plugin-dts": "^6.1.1", | ||
"tslib": "^2.6.3", | ||
"vitest": "^1.6.0" | ||
"tslib": "^2.7.0", | ||
"vitest": "^2.0.5" | ||
}, | ||
@@ -41,0 +41,0 @@ "scripts": { |
20090
267