hookified 1.4.0 → 1.5.1
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/README.md +451 -4
- package/dist/browser/index.global.js +1 -1
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +134 -14
- package/dist/node/index.d.cts +125 -4
- package/dist/node/index.d.ts +125 -4
- package/dist/node/index.js +134 -14
- package/package.json +6 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/eventified.ts","../../src/index.ts"],"sourcesContent":["import {type IEventEmitter} from './event-emitter.js';\n\nexport type EventListener = (...arguments_: any[]) => void;\n\nexport class Eventified implements IEventEmitter {\n\t_eventListeners: Map<string | symbol, EventListener[]>;\n\t_maxListeners: number;\n\n\tconstructor() {\n\t\tthis._eventListeners = new Map<string | symbol, EventListener[]>();\n\t\tthis._maxListeners = 100; // Default maximum number of listeners\n\t}\n\n\tonce(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst onceListener: EventListener = (...arguments_: any[]) => {\n\t\t\tthis.off(eventName as string, onceListener);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\tlistener(...arguments_);\n\t\t};\n\n\t\tthis.on(eventName as string, onceListener);\n\t\treturn this;\n\t}\n\n\tlistenerCount(eventName?: string | symbol): number {\n\t\tif (!eventName) {\n\t\t\treturn this.getAllListeners().length;\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(eventName as string);\n\t\treturn listeners ? listeners.length : 0;\n\t}\n\n\teventNames(): Array<string | symbol> {\n\t\treturn Array.from(this._eventListeners.keys());\n\t}\n\n\trawListeners(eventName?: string | symbol): EventListener[] {\n\t\tif (!eventName) {\n\t\t\treturn this.getAllListeners();\n\t\t}\n\n\t\treturn this._eventListeners.get(eventName) ?? [];\n\t}\n\n\tprependListener(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst listeners = this._eventListeners.get(eventName) ?? [];\n\t\tlisteners.unshift(listener);\n\t\tthis._eventListeners.set(eventName, listeners);\n\t\treturn this;\n\t}\n\n\tprependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst onceListener: EventListener = (...arguments_: any[]) => {\n\t\t\tthis.off(eventName as string, onceListener);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\tlistener(...arguments_);\n\t\t};\n\n\t\tthis.prependListener(eventName as string, onceListener);\n\t\treturn this;\n\t}\n\n\tpublic maxListeners(): number {\n\t\treturn this._maxListeners;\n\t}\n\n\t// Add an event listener\n\tpublic addListener(event: string | symbol, listener: EventListener): IEventEmitter {\n\t\tthis.on(event, listener);\n\t\treturn this;\n\t}\n\n\tpublic on(event: string | symbol, listener: EventListener): IEventEmitter {\n\t\tif (!this._eventListeners.has(event)) {\n\t\t\tthis._eventListeners.set(event, []);\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners) {\n\t\t\tif (listeners.length >= this._maxListeners) {\n\t\t\t\tconsole.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event as string} listeners added. Use setMaxListeners() to increase limit.`);\n\t\t\t}\n\n\t\t\tlisteners.push(listener);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t// Remove an event listener\n\tpublic removeListener(event: string, listener: EventListener): IEventEmitter {\n\t\tthis.off(event, listener);\n\t\treturn this;\n\t}\n\n\tpublic off(event: string, listener: EventListener): IEventEmitter {\n\t\tconst listeners = this._eventListeners.get(event) ?? [];\n\t\tconst index = listeners.indexOf(listener);\n\t\tif (index > -1) {\n\t\t\tlisteners.splice(index, 1);\n\t\t}\n\n\t\tif (listeners.length === 0) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t// Emit an event\n\tpublic emit(event: string, ...arguments_: any[]): boolean {\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners && listeners.length > 0) {\n\t\t\tfor (const listener of listeners) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tlistener(...arguments_);\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t// Get all listeners for a specific event\n\tpublic listeners(event: string): EventListener[] {\n\t\treturn this._eventListeners.get(event) ?? [];\n\t}\n\n\t// Remove all listeners for a specific event\n\tpublic removeAllListeners(event?: string): IEventEmitter {\n\t\tif (event) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t} else {\n\t\t\tthis._eventListeners.clear();\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t// Set the maximum number of listeners for a single event\n\tpublic setMaxListeners(n: number): void {\n\t\tthis._maxListeners = n;\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tif (listeners.length > n) {\n\t\t\t\tlisteners.splice(n);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getAllListeners(): EventListener[] {\n\t\tlet result = new Array<EventListener>();\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tresult = result.concat(listeners);\n\t\t}\n\n\t\treturn result;\n\t}\n}\n","import {Eventified} from './eventified.js';\n\nexport type Hook = (...arguments_: any[]) => Promise<void> | void;\n\nexport class Hookified extends Eventified {\n\t_hooks: Map<string, Hook[]>;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis._hooks = new Map();\n\t}\n\n\t// Adds a handler function for a specific event\n\tonHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\teventHandlers.push(handler);\n\t\t} else {\n\t\t\tthis._hooks.set(event, [handler]);\n\t\t}\n\t}\n\n\t// Removes a specific handler function for a specific event\n\tremoveHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tconst index = eventHandlers.indexOf(handler);\n\t\t\tif (index !== -1) {\n\t\t\t\teventHandlers.splice(index, 1);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Triggers all handlers for a specific event with provided data\n\tasync hook<T>(event: string, ...arguments_: T[]) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tfor (const handler of eventHandlers) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line no-await-in-loop\n\t\t\t\t\tawait handler(...arguments_);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.emit('error', new Error(`Error in hook handler for event \"${event}\": ${(error as Error).message}`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Provides read-only access to the current handlers\n\tget hooks() {\n\t\t// Creating a new map to prevent external modifications to the original map\n\t\treturn this._hooks;\n\t}\n\n\tgetHooks(event: string) {\n\t\treturn this._hooks.get(event);\n\t}\n\n\tclearHooks() {\n\t\tthis._hooks.clear();\n\t}\n}\n\nexport {Eventified, type EventListener} from './eventified.js';\n"],"mappings":"oKAIO,IAAMA,EAAN,KAA0C,CAIhD,aAAc,CAHdC,EAAA,wBACAA,EAAA,sBAGC,KAAK,gBAAkB,IAAI,IAC3B,KAAK,cAAgB,GACtB,CAEA,KAAKC,EAA4BC,EAAwC,CACxE,IAAMC,EAA8B,IAAIC,IAAsB,CAC7D,KAAK,IAAIH,EAAqBE,CAAY,EAE1CD,EAAS,GAAGE,CAAU,CACvB,EAEA,YAAK,GAAGH,EAAqBE,CAAY,EAClC,IACR,CAEA,cAAcF,EAAqC,CAClD,GAAI,CAACA,EACJ,OAAO,KAAK,gBAAgB,EAAE,OAG/B,IAAMI,EAAY,KAAK,gBAAgB,IAAIJ,CAAmB,EAC9D,OAAOI,EAAYA,EAAU,OAAS,CACvC,CAEA,YAAqC,CACpC,OAAO,MAAM,KAAK,KAAK,gBAAgB,KAAK,CAAC,CAC9C,CAEA,aAAaJ,EAA8C,CAC1D,OAAKA,EAIE,KAAK,gBAAgB,IAAIA,CAAS,GAAK,CAAC,EAHvC,K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,CACxG,CAGH,CAGA,IAAI,OAAQ,CAEX,OAAO,KAAK,MACb,CAEA,SAASL,EAAe,CACvB,OAAO,KAAK,OAAO,IAAIA,CAAK,CAC7B,CAEA,YAAa,CACZ,KAAK,OAAO,MAAM,CACnB,CACD","names":["Eventified","__publicField","eventName","listener","onceListener","arguments_","listeners","event","index","n","result","Hookified","Eventified","__publicField","event","handler","eventHandlers","index","arguments_","error"]}
|
|
1
|
+
{"version":3,"sources":["../../src/eventified.ts","../../src/index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/naming-convention\nexport type IEventEmitter = {\n\t/**\n\t * Registers a listener for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to listen for.\n\t * @param listener - A callback function that will be invoked when the event is emitted.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.on('data', (message) => {\n\t * console.log(message);\n\t * });\n\t */\n\ton(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Alias for `on`. Registers a listener for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to listen for.\n\t * @param listener - A callback function that will be invoked when the event is emitted.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t */\n\taddListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Registers a one-time listener for the specified event. The listener is removed after it is called once.\n\t *\n\t * @param eventName - The name (or symbol) of the event to listen for.\n\t * @param listener - A callback function that will be invoked once when the event is emitted.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.once('close', () => {\n\t * console.log('The connection was closed.');\n\t * });\n\t */\n\tonce(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Removes a previously registered listener for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to stop listening for.\n\t * @param listener - The specific callback function to remove.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.off('data', myListener);\n\t */\n\toff(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Alias for `off`. Removes a previously registered listener for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to stop listening for.\n\t * @param listener - The specific callback function to remove.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t */\n\tremoveListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Emits the specified event, invoking all registered listeners with the provided arguments.\n\t *\n\t * @param eventName - The name (or symbol) of the event to emit.\n\t * @param args - Arguments passed to each listener.\n\t * @returns `true` if the event had listeners, `false` otherwise.\n\t *\n\t * @example\n\t * emitter.emit('data', 'Hello World');\n\t */\n\temit(eventName: string | symbol, ...arguments_: any[]): boolean;\n\n\t/**\n\t * Returns the number of listeners registered for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event.\n\t * @returns The number of registered listeners.\n\t *\n\t * @example\n\t * const count = emitter.listenerCount('data');\n\t * console.log(count); // e.g., 2\n\t */\n\tlistenerCount(eventName: string | symbol): number;\n\n\t/**\n\t * Removes all listeners for the specified event. If no event is specified, it removes all listeners for all events.\n\t *\n\t * @param eventName - (Optional) The name (or symbol) of the event.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.removeAllListeners('data');\n\t */\n\tremoveAllListeners(eventName?: string | symbol): IEventEmitter;\n\n\t/**\n\t * Returns an array of event names for which listeners have been registered.\n\t *\n\t * @returns An array of event names (or symbols).\n\t *\n\t * @example\n\t * const events = emitter.eventNames();\n\t * console.log(events); // e.g., ['data', 'close']\n\t */\n\teventNames(): Array<string | symbol>;\n\n\t/**\n\t * Returns an array of listeners registered for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event.\n\t * @returns An array of listener functions.\n\t *\n\t * @example\n\t * const listeners = emitter.listeners('data');\n\t * console.log(listeners.length); // e.g., 2\n\t */\n\tlisteners(eventName: string | symbol): Array<(...arguments_: any[]) => void>;\n\n\t/**\n\t * Returns an array of raw listeners for the specified event. This includes listeners wrapped by internal mechanisms (e.g., once-only listeners).\n\t *\n\t * @param eventName - The name (or symbol) of the event.\n\t * @returns An array of raw listener functions.\n\t *\n\t * @example\n\t * const rawListeners = emitter.rawListeners('data');\n\t */\n\trawListeners(eventName: string | symbol): Array<(...arguments_: any[]) => void>;\n\n\t/**\n\t * Adds a listener to the beginning of the listeners array for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to listen for.\n\t * @param listener - A callback function that will be invoked when the event is emitted.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.prependListener('data', (message) => {\n\t * console.log('This will run first.');\n\t * });\n\t */\n\tprependListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n\n\t/**\n\t * Adds a one-time listener to the beginning of the listeners array for the specified event.\n\t *\n\t * @param eventName - The name (or symbol) of the event to listen for.\n\t * @param listener - A callback function that will be invoked once when the event is emitted.\n\t * @returns The current instance of EventEmitter for method chaining.\n\t *\n\t * @example\n\t * emitter.prependOnceListener('data', (message) => {\n\t * console.log('This will run first and only once.');\n\t * });\n\t */\n\tprependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;\n};\n\nexport type EventListener = (...arguments_: any[]) => void;\n\nexport class Eventified implements IEventEmitter {\n\t_eventListeners: Map<string | symbol, EventListener[]>;\n\t_maxListeners: number;\n\n\tconstructor() {\n\t\tthis._eventListeners = new Map<string | symbol, EventListener[]>();\n\t\tthis._maxListeners = 100; // Default maximum number of listeners\n\t}\n\n\t/**\n\t * Adds a handler function for a specific event that will run only once\n\t * @param {string | symbol} eventName\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tonce(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst onceListener: EventListener = (...arguments_: any[]) => {\n\t\t\tthis.off(eventName as string, onceListener);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\tlistener(...arguments_);\n\t\t};\n\n\t\tthis.on(eventName as string, onceListener);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners\n\t * @param {string} eventName The event name. Not required\n\t * @returns {number} The number of listeners\n\t */\n\tlistenerCount(eventName?: string | symbol): number {\n\t\tif (!eventName) {\n\t\t\treturn this.getAllListeners().length;\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(eventName as string);\n\t\treturn listeners ? listeners.length : 0;\n\t}\n\n\t/**\n\t * Gets an array of event names\n\t * @returns {Array<string | symbol>} An array of event names\n\t */\n\teventNames(): Array<string | symbol> {\n\t\treturn Array.from(this._eventListeners.keys());\n\t}\n\n\t/**\n\t * Gets an array of listeners for a specific event. If no event is provided, it returns all listeners\n\t * @param {string} [event] (Optional) The event name\n\t * @returns {EventListener[]} An array of listeners\n\t */\n\trawListeners(event?: string | symbol): EventListener[] {\n\t\tif (!event) {\n\t\t\treturn this.getAllListeners();\n\t\t}\n\n\t\treturn this._eventListeners.get(event) ?? [];\n\t}\n\n\t/**\n\t * Prepends a listener to the beginning of the listeners array for the specified event\n\t * @param {string | symbol} eventName\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tprependListener(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst listeners = this._eventListeners.get(eventName) ?? [];\n\t\tlisteners.unshift(listener);\n\t\tthis._eventListeners.set(eventName, listeners);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Prepends a one-time listener to the beginning of the listeners array for the specified event\n\t * @param {string | symbol} eventName\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tprependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst onceListener: EventListener = (...arguments_: any[]) => {\n\t\t\tthis.off(eventName as string, onceListener);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\tlistener(...arguments_);\n\t\t};\n\n\t\tthis.prependListener(eventName as string, onceListener);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Gets the maximum number of listeners that can be added for a single event\n\t * @returns {number} The maximum number of listeners\n\t */\n\tpublic maxListeners(): number {\n\t\treturn this._maxListeners;\n\t}\n\n\t/**\n\t * Adds a listener for a specific event. It is an alias for the on() method\n\t * @param {string | symbol} event\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tpublic addListener(event: string | symbol, listener: EventListener): IEventEmitter {\n\t\tthis.on(event, listener);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Adds a listener for a specific event\n\t * @param {string | symbol} event\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tpublic on(event: string | symbol, listener: EventListener): IEventEmitter {\n\t\tif (!this._eventListeners.has(event)) {\n\t\t\tthis._eventListeners.set(event, []);\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners) {\n\t\t\tif (listeners.length >= this._maxListeners) {\n\t\t\t\tconsole.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event as string} listeners added. Use setMaxListeners() to increase limit.`);\n\t\t\t}\n\n\t\t\tlisteners.push(listener);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes a listener for a specific event. It is an alias for the off() method\n\t * @param {string | symbol} event\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tpublic removeListener(event: string, listener: EventListener): IEventEmitter {\n\t\tthis.off(event, listener);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes a listener for a specific event\n\t * @param {string | symbol} event\n\t * @param {EventListener} listener\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tpublic off(event: string | symbol, listener: EventListener): IEventEmitter {\n\t\tconst listeners = this._eventListeners.get(event) ?? [];\n\t\tconst index = listeners.indexOf(listener);\n\t\tif (index > -1) {\n\t\t\tlisteners.splice(index, 1);\n\t\t}\n\n\t\tif (listeners.length === 0) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Calls all listeners for a specific event\n\t * @param {string | symbol} event\n\t * @param arguments_ The arguments to pass to the listeners\n\t * @returns {boolean} Returns true if the event had listeners, false otherwise\n\t */\n\tpublic emit(event: string | symbol, ...arguments_: any[]): boolean {\n\t\tlet result = false;\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners && listeners.length > 0) {\n\t\t\tfor (const listener of listeners) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tlistener(...arguments_);\n\t\t\t\tresult = true;\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Gets all listeners for a specific event. If no event is provided, it returns all listeners\n\t * @param {string} [event] (Optional) The event name\n\t * @returns {EventListener[]} An array of listeners\n\t */\n\tpublic listeners(event: string): EventListener[] {\n\t\treturn this._eventListeners.get(event) ?? [];\n\t}\n\n\t/**\n\t * Removes all listeners for a specific event. If no event is provided, it removes all listeners\n\t * @param {string} [event] (Optional) The event name\n\t * @returns {IEventEmitter} returns the instance of the class for chaining\n\t */\n\tpublic removeAllListeners(event?: string): IEventEmitter {\n\t\tif (event) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t} else {\n\t\t\tthis._eventListeners.clear();\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Sets the maximum number of listeners that can be added for a single event\n\t * @param {number} n The maximum number of listeners\n\t * @returns {void}\n\t */\n\tpublic setMaxListeners(n: number): void {\n\t\tthis._maxListeners = n;\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tif (listeners.length > n) {\n\t\t\t\tlisteners.splice(n);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Gets all listeners\n\t * @returns {EventListener[]} An array of listeners\n\t */\n\tpublic getAllListeners(): EventListener[] {\n\t\tlet result = new Array<EventListener>();\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tresult = result.concat(listeners);\n\t\t}\n\n\t\treturn result;\n\t}\n}\n","import {Eventified} from './eventified.js';\n\nexport type Hook = (...arguments_: any[]) => Promise<void> | void;\n\nexport class Hookified extends Eventified {\n\t_hooks: Map<string, Hook[]>;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis._hooks = new Map();\n\t}\n\n\t/**\n\t * Adds a handler function for a specific event\n\t * @param {string} event\n\t * @param {Hook} handler - this can be async or sync\n\t * @returns {void}\n\t */\n\tonHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\teventHandlers.push(handler);\n\t\t} else {\n\t\t\tthis._hooks.set(event, [handler]);\n\t\t}\n\t}\n\n\t/**\n\t * Adds a handler that only executes once for a specific event\n\t * @param event\n\t * @param handler\n\t */\n\tonceHook(event: string, handler: Hook) {\n\t\tconst hook = async (...arguments_: any[]) => {\n\t\t\tthis.removeHook(event, hook);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\treturn handler(...arguments_);\n\t\t};\n\n\t\tthis.onHook(event, hook);\n\t}\n\n\t/**\n\t * Removes a handler function for a specific event\n\t * @param {string} event\n\t * @param {Hook} handler\n\t * @returns {void}\n\t */\n\tremoveHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tconst index = eventHandlers.indexOf(handler);\n\t\t\tif (index !== -1) {\n\t\t\t\teventHandlers.splice(index, 1);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Calls all handlers for a specific event\n\t * @param {string} event\n\t * @param {T[]} arguments_\n\t * @returns {Promise<void>}\n\t */\n\tasync hook<T>(event: string, ...arguments_: T[]) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tfor (const handler of eventHandlers) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line no-await-in-loop\n\t\t\t\t\tawait handler(...arguments_);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.emit('error', new Error(`Error in hook handler for event \"${event}\": ${(error as Error).message}`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Gets all hooks\n\t * @returns {Map<string, Hook[]>}\n\t */\n\tget hooks() {\n\t\treturn this._hooks;\n\t}\n\n\t/**\n\t * Gets all hooks for a specific event\n\t * @param {string} event\n\t * @returns {Hook[]}\n\t */\n\tgetHooks(event: string) {\n\t\treturn this._hooks.get(event);\n\t}\n\n\t/**\n\t * Removes all hooks\n\t * @returns {void}\n\t */\n\tclearHooks() {\n\t\tthis._hooks.clear();\n\t}\n}\n\nexport {Eventified, type EventListener} from './eventified.js';\n"],"mappings":"oKAgKO,IAAMA,EAAN,KAA0C,CAIhD,aAAc,CAHdC,EAAA,wBACAA,EAAA,sBAGC,KAAK,gBAAkB,IAAI,IAC3B,KAAK,cAAgB,GACtB,CAQA,KAAKC,EAA4BC,EAAwC,CACxE,IAAMC,EAA8B,IAAIC,IAAsB,CAC7D,KAAK,IAAIH,EAAqBE,CAAY,EAE1CD,EAAS,GAAGE,CAAU,CACvB,EAEA,YAAK,GAAGH,EAAqBE,CAAY,EAClC,IACR,CAOA,cAAcF,EAAqC,CAClD,GAAI,CAACA,EACJ,OAAO,KAAK,gBAAgB,EAAE,OAG/B,IAAMI,EAAY,KAAK,gBAAgB,IAAIJ,CAAmB,EAC9D,OAAOI,EAAYA,EAAU,OAAS,CACvC,CAMA,YAAqC,CACpC,OAAO,MAAM,KAAK,KAAK,gBAAgB,KAAK,CAAC,CAC9C,CAOA,aAAaC,EAA0C,CACtD,OAAKA,EAIE,KAAK,gBAAgB,IAAIA,CAAK,GAAK,CAAC,EAHnC,KAAK,gBAAgB,CAI9B,CAQA,gBAAgBL,E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GAAI,CAEH,MAAMD,EAAQ,GAAGG,CAAU,CAC5B,OAASE,EAAO,CACf,KAAK,KAAK,QAAS,IAAI,MAAM,oCAAoCN,CAAK,MAAOM,EAAgB,OAAO,EAAE,CAAC,CACxG,CAGH,CAMA,IAAI,OAAQ,CACX,OAAO,KAAK,MACb,CAOA,SAASN,EAAe,CACvB,OAAO,KAAK,OAAO,IAAIA,CAAK,CAC7B,CAMA,YAAa,CACZ,KAAK,OAAO,MAAM,CACnB,CACD","names":["Eventified","__publicField","eventName","listener","onceListener","arguments_","listeners","event","index","result","n","Hookified","Eventified","__publicField","event","handler","eventHandlers","hook","arguments_","index","error"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -33,6 +33,12 @@ var Eventified = class {
|
|
|
33
33
|
this._eventListeners = /* @__PURE__ */ new Map();
|
|
34
34
|
this._maxListeners = 100;
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Adds a handler function for a specific event that will run only once
|
|
38
|
+
* @param {string | symbol} eventName
|
|
39
|
+
* @param {EventListener} listener
|
|
40
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
41
|
+
*/
|
|
36
42
|
once(eventName, listener) {
|
|
37
43
|
const onceListener = (...arguments_) => {
|
|
38
44
|
this.off(eventName, onceListener);
|
|
@@ -41,6 +47,11 @@ var Eventified = class {
|
|
|
41
47
|
this.on(eventName, onceListener);
|
|
42
48
|
return this;
|
|
43
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
52
|
+
* @param {string} eventName The event name. Not required
|
|
53
|
+
* @returns {number} The number of listeners
|
|
54
|
+
*/
|
|
44
55
|
listenerCount(eventName) {
|
|
45
56
|
if (!eventName) {
|
|
46
57
|
return this.getAllListeners().length;
|
|
@@ -48,21 +59,42 @@ var Eventified = class {
|
|
|
48
59
|
const listeners = this._eventListeners.get(eventName);
|
|
49
60
|
return listeners ? listeners.length : 0;
|
|
50
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Gets an array of event names
|
|
64
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
65
|
+
*/
|
|
51
66
|
eventNames() {
|
|
52
67
|
return Array.from(this._eventListeners.keys());
|
|
53
68
|
}
|
|
54
|
-
|
|
55
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
71
|
+
* @param {string} [event] (Optional) The event name
|
|
72
|
+
* @returns {EventListener[]} An array of listeners
|
|
73
|
+
*/
|
|
74
|
+
rawListeners(event) {
|
|
75
|
+
if (!event) {
|
|
56
76
|
return this.getAllListeners();
|
|
57
77
|
}
|
|
58
|
-
return this._eventListeners.get(
|
|
78
|
+
return this._eventListeners.get(event) ?? [];
|
|
59
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
82
|
+
* @param {string | symbol} eventName
|
|
83
|
+
* @param {EventListener} listener
|
|
84
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
85
|
+
*/
|
|
60
86
|
prependListener(eventName, listener) {
|
|
61
87
|
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
62
88
|
listeners.unshift(listener);
|
|
63
89
|
this._eventListeners.set(eventName, listeners);
|
|
64
90
|
return this;
|
|
65
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
94
|
+
* @param {string | symbol} eventName
|
|
95
|
+
* @param {EventListener} listener
|
|
96
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
97
|
+
*/
|
|
66
98
|
prependOnceListener(eventName, listener) {
|
|
67
99
|
const onceListener = (...arguments_) => {
|
|
68
100
|
this.off(eventName, onceListener);
|
|
@@ -71,14 +103,29 @@ var Eventified = class {
|
|
|
71
103
|
this.prependListener(eventName, onceListener);
|
|
72
104
|
return this;
|
|
73
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
108
|
+
* @returns {number} The maximum number of listeners
|
|
109
|
+
*/
|
|
74
110
|
maxListeners() {
|
|
75
111
|
return this._maxListeners;
|
|
76
112
|
}
|
|
77
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
115
|
+
* @param {string | symbol} event
|
|
116
|
+
* @param {EventListener} listener
|
|
117
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
118
|
+
*/
|
|
78
119
|
addListener(event, listener) {
|
|
79
120
|
this.on(event, listener);
|
|
80
121
|
return this;
|
|
81
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Adds a listener for a specific event
|
|
125
|
+
* @param {string | symbol} event
|
|
126
|
+
* @param {EventListener} listener
|
|
127
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
128
|
+
*/
|
|
82
129
|
on(event, listener) {
|
|
83
130
|
if (!this._eventListeners.has(event)) {
|
|
84
131
|
this._eventListeners.set(event, []);
|
|
@@ -92,11 +139,22 @@ var Eventified = class {
|
|
|
92
139
|
}
|
|
93
140
|
return this;
|
|
94
141
|
}
|
|
95
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
144
|
+
* @param {string | symbol} event
|
|
145
|
+
* @param {EventListener} listener
|
|
146
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
147
|
+
*/
|
|
96
148
|
removeListener(event, listener) {
|
|
97
149
|
this.off(event, listener);
|
|
98
150
|
return this;
|
|
99
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Removes a listener for a specific event
|
|
154
|
+
* @param {string | symbol} event
|
|
155
|
+
* @param {EventListener} listener
|
|
156
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
157
|
+
*/
|
|
100
158
|
off(event, listener) {
|
|
101
159
|
const listeners = this._eventListeners.get(event) ?? [];
|
|
102
160
|
const index = listeners.indexOf(listener);
|
|
@@ -108,21 +166,36 @@ var Eventified = class {
|
|
|
108
166
|
}
|
|
109
167
|
return this;
|
|
110
168
|
}
|
|
111
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Calls all listeners for a specific event
|
|
171
|
+
* @param {string | symbol} event
|
|
172
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
173
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
174
|
+
*/
|
|
112
175
|
emit(event, ...arguments_) {
|
|
176
|
+
let result = false;
|
|
113
177
|
const listeners = this._eventListeners.get(event);
|
|
114
178
|
if (listeners && listeners.length > 0) {
|
|
115
179
|
for (const listener of listeners) {
|
|
116
180
|
listener(...arguments_);
|
|
181
|
+
result = true;
|
|
117
182
|
}
|
|
118
183
|
}
|
|
119
|
-
return
|
|
184
|
+
return result;
|
|
120
185
|
}
|
|
121
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
188
|
+
* @param {string} [event] (Optional) The event name
|
|
189
|
+
* @returns {EventListener[]} An array of listeners
|
|
190
|
+
*/
|
|
122
191
|
listeners(event) {
|
|
123
192
|
return this._eventListeners.get(event) ?? [];
|
|
124
193
|
}
|
|
125
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
196
|
+
* @param {string} [event] (Optional) The event name
|
|
197
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
198
|
+
*/
|
|
126
199
|
removeAllListeners(event) {
|
|
127
200
|
if (event) {
|
|
128
201
|
this._eventListeners.delete(event);
|
|
@@ -131,7 +204,11 @@ var Eventified = class {
|
|
|
131
204
|
}
|
|
132
205
|
return this;
|
|
133
206
|
}
|
|
134
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
209
|
+
* @param {number} n The maximum number of listeners
|
|
210
|
+
* @returns {void}
|
|
211
|
+
*/
|
|
135
212
|
setMaxListeners(n) {
|
|
136
213
|
this._maxListeners = n;
|
|
137
214
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -140,6 +217,10 @@ var Eventified = class {
|
|
|
140
217
|
}
|
|
141
218
|
}
|
|
142
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Gets all listeners
|
|
222
|
+
* @returns {EventListener[]} An array of listeners
|
|
223
|
+
*/
|
|
143
224
|
getAllListeners() {
|
|
144
225
|
let result = new Array();
|
|
145
226
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -156,7 +237,12 @@ var Hookified = class extends Eventified {
|
|
|
156
237
|
super();
|
|
157
238
|
this._hooks = /* @__PURE__ */ new Map();
|
|
158
239
|
}
|
|
159
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Adds a handler function for a specific event
|
|
242
|
+
* @param {string} event
|
|
243
|
+
* @param {Hook} handler - this can be async or sync
|
|
244
|
+
* @returns {void}
|
|
245
|
+
*/
|
|
160
246
|
onHook(event, handler) {
|
|
161
247
|
const eventHandlers = this._hooks.get(event);
|
|
162
248
|
if (eventHandlers) {
|
|
@@ -165,7 +251,24 @@ var Hookified = class extends Eventified {
|
|
|
165
251
|
this._hooks.set(event, [handler]);
|
|
166
252
|
}
|
|
167
253
|
}
|
|
168
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Adds a handler that only executes once for a specific event
|
|
256
|
+
* @param event
|
|
257
|
+
* @param handler
|
|
258
|
+
*/
|
|
259
|
+
onceHook(event, handler) {
|
|
260
|
+
const hook = async (...arguments_) => {
|
|
261
|
+
this.removeHook(event, hook);
|
|
262
|
+
return handler(...arguments_);
|
|
263
|
+
};
|
|
264
|
+
this.onHook(event, hook);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Removes a handler function for a specific event
|
|
268
|
+
* @param {string} event
|
|
269
|
+
* @param {Hook} handler
|
|
270
|
+
* @returns {void}
|
|
271
|
+
*/
|
|
169
272
|
removeHook(event, handler) {
|
|
170
273
|
const eventHandlers = this._hooks.get(event);
|
|
171
274
|
if (eventHandlers) {
|
|
@@ -175,7 +278,12 @@ var Hookified = class extends Eventified {
|
|
|
175
278
|
}
|
|
176
279
|
}
|
|
177
280
|
}
|
|
178
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Calls all handlers for a specific event
|
|
283
|
+
* @param {string} event
|
|
284
|
+
* @param {T[]} arguments_
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
*/
|
|
179
287
|
async hook(event, ...arguments_) {
|
|
180
288
|
const eventHandlers = this._hooks.get(event);
|
|
181
289
|
if (eventHandlers) {
|
|
@@ -188,13 +296,25 @@ var Hookified = class extends Eventified {
|
|
|
188
296
|
}
|
|
189
297
|
}
|
|
190
298
|
}
|
|
191
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Gets all hooks
|
|
301
|
+
* @returns {Map<string, Hook[]>}
|
|
302
|
+
*/
|
|
192
303
|
get hooks() {
|
|
193
304
|
return this._hooks;
|
|
194
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Gets all hooks for a specific event
|
|
308
|
+
* @param {string} event
|
|
309
|
+
* @returns {Hook[]}
|
|
310
|
+
*/
|
|
195
311
|
getHooks(event) {
|
|
196
312
|
return this._hooks.get(event);
|
|
197
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Removes all hooks
|
|
316
|
+
* @returns {void}
|
|
317
|
+
*/
|
|
198
318
|
clearHooks() {
|
|
199
319
|
this._hooks.clear();
|
|
200
320
|
}
|
package/dist/node/index.d.cts
CHANGED
|
@@ -142,27 +142,111 @@ type IEventEmitter = {
|
|
|
142
142
|
*/
|
|
143
143
|
prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
144
144
|
};
|
|
145
|
-
|
|
146
145
|
type EventListener = (...arguments_: any[]) => void;
|
|
147
146
|
declare class Eventified implements IEventEmitter {
|
|
148
147
|
_eventListeners: Map<string | symbol, EventListener[]>;
|
|
149
148
|
_maxListeners: number;
|
|
150
149
|
constructor();
|
|
150
|
+
/**
|
|
151
|
+
* Adds a handler function for a specific event that will run only once
|
|
152
|
+
* @param {string | symbol} eventName
|
|
153
|
+
* @param {EventListener} listener
|
|
154
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
155
|
+
*/
|
|
151
156
|
once(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
157
|
+
/**
|
|
158
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
159
|
+
* @param {string} eventName The event name. Not required
|
|
160
|
+
* @returns {number} The number of listeners
|
|
161
|
+
*/
|
|
152
162
|
listenerCount(eventName?: string | symbol): number;
|
|
163
|
+
/**
|
|
164
|
+
* Gets an array of event names
|
|
165
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
166
|
+
*/
|
|
153
167
|
eventNames(): Array<string | symbol>;
|
|
154
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
170
|
+
* @param {string} [event] (Optional) The event name
|
|
171
|
+
* @returns {EventListener[]} An array of listeners
|
|
172
|
+
*/
|
|
173
|
+
rawListeners(event?: string | symbol): EventListener[];
|
|
174
|
+
/**
|
|
175
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
176
|
+
* @param {string | symbol} eventName
|
|
177
|
+
* @param {EventListener} listener
|
|
178
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
179
|
+
*/
|
|
155
180
|
prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
181
|
+
/**
|
|
182
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
183
|
+
* @param {string | symbol} eventName
|
|
184
|
+
* @param {EventListener} listener
|
|
185
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
186
|
+
*/
|
|
156
187
|
prependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
188
|
+
/**
|
|
189
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
190
|
+
* @returns {number} The maximum number of listeners
|
|
191
|
+
*/
|
|
157
192
|
maxListeners(): number;
|
|
193
|
+
/**
|
|
194
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
195
|
+
* @param {string | symbol} event
|
|
196
|
+
* @param {EventListener} listener
|
|
197
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
198
|
+
*/
|
|
158
199
|
addListener(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
200
|
+
/**
|
|
201
|
+
* Adds a listener for a specific event
|
|
202
|
+
* @param {string | symbol} event
|
|
203
|
+
* @param {EventListener} listener
|
|
204
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
205
|
+
*/
|
|
159
206
|
on(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
207
|
+
/**
|
|
208
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
209
|
+
* @param {string | symbol} event
|
|
210
|
+
* @param {EventListener} listener
|
|
211
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
212
|
+
*/
|
|
160
213
|
removeListener(event: string, listener: EventListener): IEventEmitter;
|
|
161
|
-
|
|
162
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Removes a listener for a specific event
|
|
216
|
+
* @param {string | symbol} event
|
|
217
|
+
* @param {EventListener} listener
|
|
218
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
219
|
+
*/
|
|
220
|
+
off(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
221
|
+
/**
|
|
222
|
+
* Calls all listeners for a specific event
|
|
223
|
+
* @param {string | symbol} event
|
|
224
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
225
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
226
|
+
*/
|
|
227
|
+
emit(event: string | symbol, ...arguments_: any[]): boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
230
|
+
* @param {string} [event] (Optional) The event name
|
|
231
|
+
* @returns {EventListener[]} An array of listeners
|
|
232
|
+
*/
|
|
163
233
|
listeners(event: string): EventListener[];
|
|
234
|
+
/**
|
|
235
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
236
|
+
* @param {string} [event] (Optional) The event name
|
|
237
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
238
|
+
*/
|
|
164
239
|
removeAllListeners(event?: string): IEventEmitter;
|
|
240
|
+
/**
|
|
241
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
242
|
+
* @param {number} n The maximum number of listeners
|
|
243
|
+
* @returns {void}
|
|
244
|
+
*/
|
|
165
245
|
setMaxListeners(n: number): void;
|
|
246
|
+
/**
|
|
247
|
+
* Gets all listeners
|
|
248
|
+
* @returns {EventListener[]} An array of listeners
|
|
249
|
+
*/
|
|
166
250
|
getAllListeners(): EventListener[];
|
|
167
251
|
}
|
|
168
252
|
|
|
@@ -170,11 +254,48 @@ type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
|
170
254
|
declare class Hookified extends Eventified {
|
|
171
255
|
_hooks: Map<string, Hook[]>;
|
|
172
256
|
constructor();
|
|
257
|
+
/**
|
|
258
|
+
* Adds a handler function for a specific event
|
|
259
|
+
* @param {string} event
|
|
260
|
+
* @param {Hook} handler - this can be async or sync
|
|
261
|
+
* @returns {void}
|
|
262
|
+
*/
|
|
173
263
|
onHook(event: string, handler: Hook): void;
|
|
264
|
+
/**
|
|
265
|
+
* Adds a handler that only executes once for a specific event
|
|
266
|
+
* @param event
|
|
267
|
+
* @param handler
|
|
268
|
+
*/
|
|
269
|
+
onceHook(event: string, handler: Hook): void;
|
|
270
|
+
/**
|
|
271
|
+
* Removes a handler function for a specific event
|
|
272
|
+
* @param {string} event
|
|
273
|
+
* @param {Hook} handler
|
|
274
|
+
* @returns {void}
|
|
275
|
+
*/
|
|
174
276
|
removeHook(event: string, handler: Hook): void;
|
|
277
|
+
/**
|
|
278
|
+
* Calls all handlers for a specific event
|
|
279
|
+
* @param {string} event
|
|
280
|
+
* @param {T[]} arguments_
|
|
281
|
+
* @returns {Promise<void>}
|
|
282
|
+
*/
|
|
175
283
|
hook<T>(event: string, ...arguments_: T[]): Promise<void>;
|
|
284
|
+
/**
|
|
285
|
+
* Gets all hooks
|
|
286
|
+
* @returns {Map<string, Hook[]>}
|
|
287
|
+
*/
|
|
176
288
|
get hooks(): Map<string, Hook[]>;
|
|
289
|
+
/**
|
|
290
|
+
* Gets all hooks for a specific event
|
|
291
|
+
* @param {string} event
|
|
292
|
+
* @returns {Hook[]}
|
|
293
|
+
*/
|
|
177
294
|
getHooks(event: string): Hook[] | undefined;
|
|
295
|
+
/**
|
|
296
|
+
* Removes all hooks
|
|
297
|
+
* @returns {void}
|
|
298
|
+
*/
|
|
178
299
|
clearHooks(): void;
|
|
179
300
|
}
|
|
180
301
|
|
package/dist/node/index.d.ts
CHANGED
|
@@ -142,27 +142,111 @@ type IEventEmitter = {
|
|
|
142
142
|
*/
|
|
143
143
|
prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
144
144
|
};
|
|
145
|
-
|
|
146
145
|
type EventListener = (...arguments_: any[]) => void;
|
|
147
146
|
declare class Eventified implements IEventEmitter {
|
|
148
147
|
_eventListeners: Map<string | symbol, EventListener[]>;
|
|
149
148
|
_maxListeners: number;
|
|
150
149
|
constructor();
|
|
150
|
+
/**
|
|
151
|
+
* Adds a handler function for a specific event that will run only once
|
|
152
|
+
* @param {string | symbol} eventName
|
|
153
|
+
* @param {EventListener} listener
|
|
154
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
155
|
+
*/
|
|
151
156
|
once(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
157
|
+
/**
|
|
158
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
159
|
+
* @param {string} eventName The event name. Not required
|
|
160
|
+
* @returns {number} The number of listeners
|
|
161
|
+
*/
|
|
152
162
|
listenerCount(eventName?: string | symbol): number;
|
|
163
|
+
/**
|
|
164
|
+
* Gets an array of event names
|
|
165
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
166
|
+
*/
|
|
153
167
|
eventNames(): Array<string | symbol>;
|
|
154
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
170
|
+
* @param {string} [event] (Optional) The event name
|
|
171
|
+
* @returns {EventListener[]} An array of listeners
|
|
172
|
+
*/
|
|
173
|
+
rawListeners(event?: string | symbol): EventListener[];
|
|
174
|
+
/**
|
|
175
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
176
|
+
* @param {string | symbol} eventName
|
|
177
|
+
* @param {EventListener} listener
|
|
178
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
179
|
+
*/
|
|
155
180
|
prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
181
|
+
/**
|
|
182
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
183
|
+
* @param {string | symbol} eventName
|
|
184
|
+
* @param {EventListener} listener
|
|
185
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
186
|
+
*/
|
|
156
187
|
prependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
188
|
+
/**
|
|
189
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
190
|
+
* @returns {number} The maximum number of listeners
|
|
191
|
+
*/
|
|
157
192
|
maxListeners(): number;
|
|
193
|
+
/**
|
|
194
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
195
|
+
* @param {string | symbol} event
|
|
196
|
+
* @param {EventListener} listener
|
|
197
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
198
|
+
*/
|
|
158
199
|
addListener(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
200
|
+
/**
|
|
201
|
+
* Adds a listener for a specific event
|
|
202
|
+
* @param {string | symbol} event
|
|
203
|
+
* @param {EventListener} listener
|
|
204
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
205
|
+
*/
|
|
159
206
|
on(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
207
|
+
/**
|
|
208
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
209
|
+
* @param {string | symbol} event
|
|
210
|
+
* @param {EventListener} listener
|
|
211
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
212
|
+
*/
|
|
160
213
|
removeListener(event: string, listener: EventListener): IEventEmitter;
|
|
161
|
-
|
|
162
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Removes a listener for a specific event
|
|
216
|
+
* @param {string | symbol} event
|
|
217
|
+
* @param {EventListener} listener
|
|
218
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
219
|
+
*/
|
|
220
|
+
off(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
221
|
+
/**
|
|
222
|
+
* Calls all listeners for a specific event
|
|
223
|
+
* @param {string | symbol} event
|
|
224
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
225
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
226
|
+
*/
|
|
227
|
+
emit(event: string | symbol, ...arguments_: any[]): boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
230
|
+
* @param {string} [event] (Optional) The event name
|
|
231
|
+
* @returns {EventListener[]} An array of listeners
|
|
232
|
+
*/
|
|
163
233
|
listeners(event: string): EventListener[];
|
|
234
|
+
/**
|
|
235
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
236
|
+
* @param {string} [event] (Optional) The event name
|
|
237
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
238
|
+
*/
|
|
164
239
|
removeAllListeners(event?: string): IEventEmitter;
|
|
240
|
+
/**
|
|
241
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
242
|
+
* @param {number} n The maximum number of listeners
|
|
243
|
+
* @returns {void}
|
|
244
|
+
*/
|
|
165
245
|
setMaxListeners(n: number): void;
|
|
246
|
+
/**
|
|
247
|
+
* Gets all listeners
|
|
248
|
+
* @returns {EventListener[]} An array of listeners
|
|
249
|
+
*/
|
|
166
250
|
getAllListeners(): EventListener[];
|
|
167
251
|
}
|
|
168
252
|
|
|
@@ -170,11 +254,48 @@ type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
|
170
254
|
declare class Hookified extends Eventified {
|
|
171
255
|
_hooks: Map<string, Hook[]>;
|
|
172
256
|
constructor();
|
|
257
|
+
/**
|
|
258
|
+
* Adds a handler function for a specific event
|
|
259
|
+
* @param {string} event
|
|
260
|
+
* @param {Hook} handler - this can be async or sync
|
|
261
|
+
* @returns {void}
|
|
262
|
+
*/
|
|
173
263
|
onHook(event: string, handler: Hook): void;
|
|
264
|
+
/**
|
|
265
|
+
* Adds a handler that only executes once for a specific event
|
|
266
|
+
* @param event
|
|
267
|
+
* @param handler
|
|
268
|
+
*/
|
|
269
|
+
onceHook(event: string, handler: Hook): void;
|
|
270
|
+
/**
|
|
271
|
+
* Removes a handler function for a specific event
|
|
272
|
+
* @param {string} event
|
|
273
|
+
* @param {Hook} handler
|
|
274
|
+
* @returns {void}
|
|
275
|
+
*/
|
|
174
276
|
removeHook(event: string, handler: Hook): void;
|
|
277
|
+
/**
|
|
278
|
+
* Calls all handlers for a specific event
|
|
279
|
+
* @param {string} event
|
|
280
|
+
* @param {T[]} arguments_
|
|
281
|
+
* @returns {Promise<void>}
|
|
282
|
+
*/
|
|
175
283
|
hook<T>(event: string, ...arguments_: T[]): Promise<void>;
|
|
284
|
+
/**
|
|
285
|
+
* Gets all hooks
|
|
286
|
+
* @returns {Map<string, Hook[]>}
|
|
287
|
+
*/
|
|
176
288
|
get hooks(): Map<string, Hook[]>;
|
|
289
|
+
/**
|
|
290
|
+
* Gets all hooks for a specific event
|
|
291
|
+
* @param {string} event
|
|
292
|
+
* @returns {Hook[]}
|
|
293
|
+
*/
|
|
177
294
|
getHooks(event: string): Hook[] | undefined;
|
|
295
|
+
/**
|
|
296
|
+
* Removes all hooks
|
|
297
|
+
* @returns {void}
|
|
298
|
+
*/
|
|
178
299
|
clearHooks(): void;
|
|
179
300
|
}
|
|
180
301
|
|