@@ -469,15 +469,6 @@ "use strict"; | ||
| onHook(event, handler) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(handler); | ||
| } else { | ||
| this._hooks.set(event, [handler]); | ||
| } | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -487,3 +478,12 @@ * @returns {void} | ||
| onHookEntry(hookEntry) { | ||
| this.onHook(hookEntry.event, hookEntry.handler); | ||
| this.validateHookName(hookEntry.event); | ||
| if (!this.checkDeprecatedHook(hookEntry.event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(hookEntry.event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(hookEntry.handler); | ||
| } else { | ||
| this._hooks.set(hookEntry.event, [hookEntry.handler]); | ||
| } | ||
| } | ||
@@ -497,3 +497,3 @@ /** | ||
| addHook(event, handler) { | ||
| this.onHook(event, handler); | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
@@ -616,2 +616,35 @@ /** | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync(event, ...arguments_) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| for (const handler of eventHandlers) { | ||
| if (handler.constructor.name === "AsyncFunction") { | ||
| continue; | ||
| } | ||
| try { | ||
| handler(...arguments_); | ||
| } catch (error) { | ||
| const message = `${event}: ${error.message}`; | ||
| this.emit("error", new Error(message)); | ||
| if (this._throwOnHookError) { | ||
| throw new Error(message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -618,0 +651,0 @@ * @param {string} event - The event name |
+46
-13
@@ -467,15 +467,6 @@ var __defProp = Object.defineProperty; | ||
| onHook(event, handler) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(handler); | ||
| } else { | ||
| this._hooks.set(event, [handler]); | ||
| } | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -485,3 +476,12 @@ * @returns {void} | ||
| onHookEntry(hookEntry) { | ||
| this.onHook(hookEntry.event, hookEntry.handler); | ||
| this.validateHookName(hookEntry.event); | ||
| if (!this.checkDeprecatedHook(hookEntry.event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(hookEntry.event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(hookEntry.handler); | ||
| } else { | ||
| this._hooks.set(hookEntry.event, [hookEntry.handler]); | ||
| } | ||
| } | ||
@@ -495,3 +495,3 @@ /** | ||
| addHook(event, handler) { | ||
| this.onHook(event, handler); | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
@@ -614,2 +614,35 @@ /** | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync(event, ...arguments_) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| for (const handler of eventHandlers) { | ||
| if (handler.constructor.name === "AsyncFunction") { | ||
| continue; | ||
| } | ||
| try { | ||
| handler(...arguments_); | ||
| } catch (error) { | ||
| const message = `${event}: ${error.message}`; | ||
| this.emit("error", new Error(message)); | ||
| if (this._throwOnHookError) { | ||
| throw new Error(message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -616,0 +649,0 @@ * @param {string} event - The event name |
+46
-13
@@ -490,15 +490,6 @@ "use strict"; | ||
| onHook(event, handler) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(handler); | ||
| } else { | ||
| this._hooks.set(event, [handler]); | ||
| } | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -508,3 +499,12 @@ * @returns {void} | ||
| onHookEntry(hookEntry) { | ||
| this.onHook(hookEntry.event, hookEntry.handler); | ||
| this.validateHookName(hookEntry.event); | ||
| if (!this.checkDeprecatedHook(hookEntry.event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(hookEntry.event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(hookEntry.handler); | ||
| } else { | ||
| this._hooks.set(hookEntry.event, [hookEntry.handler]); | ||
| } | ||
| } | ||
@@ -518,3 +518,3 @@ /** | ||
| addHook(event, handler) { | ||
| this.onHook(event, handler); | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
@@ -637,2 +637,35 @@ /** | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync(event, ...arguments_) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| for (const handler of eventHandlers) { | ||
| if (handler.constructor.name === "AsyncFunction") { | ||
| continue; | ||
| } | ||
| try { | ||
| handler(...arguments_); | ||
| } catch (error) { | ||
| const message = `${event}: ${error.message}`; | ||
| this.emit("error", new Error(message)); | ||
| if (this._throwOnHookError) { | ||
| throw new Error(message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -639,0 +672,0 @@ * @param {string} event - The event name |
@@ -442,3 +442,3 @@ type Logger = { | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -501,2 +501,13 @@ * @returns {void} | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync<T>(event: string, ...arguments_: T[]): void; | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -503,0 +514,0 @@ * @param {string} event - The event name |
+12
-1
@@ -442,3 +442,3 @@ type Logger = { | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -501,2 +501,13 @@ * @returns {void} | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync<T>(event: string, ...arguments_: T[]): void; | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -503,0 +514,0 @@ * @param {string} event - The event name |
+46
-13
@@ -463,15 +463,6 @@ // src/eventified.ts | ||
| onHook(event, handler) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(handler); | ||
| } else { | ||
| this._hooks.set(event, [handler]); | ||
| } | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
| /** | ||
| * Adds a handler function for a specific event that runs before all other handlers | ||
| * Adds a handler function for a specific event | ||
| * @param {HookEntry} hookEntry | ||
@@ -481,3 +472,12 @@ * @returns {void} | ||
| onHookEntry(hookEntry) { | ||
| this.onHook(hookEntry.event, hookEntry.handler); | ||
| this.validateHookName(hookEntry.event); | ||
| if (!this.checkDeprecatedHook(hookEntry.event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(hookEntry.event); | ||
| if (eventHandlers) { | ||
| eventHandlers.push(hookEntry.handler); | ||
| } else { | ||
| this._hooks.set(hookEntry.event, [hookEntry.handler]); | ||
| } | ||
| } | ||
@@ -491,3 +491,3 @@ /** | ||
| addHook(event, handler) { | ||
| this.onHook(event, handler); | ||
| this.onHookEntry({ event, handler }); | ||
| } | ||
@@ -610,2 +610,35 @@ /** | ||
| /** | ||
| * Calls all synchronous handlers for a specific event. | ||
| * Async handlers (declared with `async` keyword) are silently skipped. | ||
| * | ||
| * Note: The `hook` method is preferred as it executes both sync and async functions. | ||
| * Use `hookSync` only when you specifically need synchronous execution. | ||
| * @param {string} event | ||
| * @param {T[]} arguments_ | ||
| * @returns {void} | ||
| */ | ||
| hookSync(event, ...arguments_) { | ||
| this.validateHookName(event); | ||
| if (!this.checkDeprecatedHook(event)) { | ||
| return; | ||
| } | ||
| const eventHandlers = this._hooks.get(event); | ||
| if (eventHandlers) { | ||
| for (const handler of eventHandlers) { | ||
| if (handler.constructor.name === "AsyncFunction") { | ||
| continue; | ||
| } | ||
| try { | ||
| handler(...arguments_); | ||
| } catch (error) { | ||
| const message = `${event}: ${error.message}`; | ||
| this.emit("error", new Error(message)); | ||
| if (this._throwOnHookError) { | ||
| throw new Error(message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`. | ||
@@ -612,0 +645,0 @@ * @param {string} event - The event name |
+3
-3
| { | ||
| "name": "hookified", | ||
| "version": "1.14.0", | ||
| "version": "1.15.0", | ||
| "description": "Event Emitting and Middleware Hooks", | ||
@@ -73,3 +73,3 @@ "type": "module", | ||
| "@monstermann/tinybench-pretty-printer": "^0.3.0", | ||
| "@types/node": "^24.10.1", | ||
| "@types/node": "^25.0.3", | ||
| "@vitest/coverage-v8": "^4.0.15", | ||
@@ -79,3 +79,3 @@ "docula": "^0.31.1", | ||
| "eventemitter3": "^5.0.1", | ||
| "hookable": "^5.5.3", | ||
| "hookable": "^6.0.1", | ||
| "pino": "^10.1.0", | ||
@@ -82,0 +82,0 @@ "rimraf": "^6.1.2", |
+41
-2
@@ -49,2 +49,3 @@  | ||
| - [.afterHook(eventName, ...args)](#afterhookeventname-args) | ||
| - [.hookSync(eventName, ...args)](#hooksync-eventname-args) | ||
| - [.hooks](#hooks) | ||
@@ -805,2 +806,40 @@ - [.getHooks(eventName)](#gethookseventname) | ||
| ## .hookSync(eventName, ...args) | ||
| Run a hook event synchronously. Async handlers (functions declared with `async` keyword) are silently skipped and only synchronous handlers are executed. | ||
| > **Note:** The `.hook()` method is preferred as it executes both sync and async functions. Use `.hookSync()` only when you specifically need synchronous execution. | ||
| ```javascript | ||
| import { Hookified } from 'hookified'; | ||
| class MyClass extends Hookified { | ||
| constructor() { | ||
| super(); | ||
| } | ||
| myMethodWithSyncHooks() { | ||
| let data = { some: 'data' }; | ||
| // Only synchronous handlers will execute | ||
| this.hookSync('before:myMethod', data); | ||
| return data; | ||
| } | ||
| } | ||
| const myClass = new MyClass(); | ||
| // This sync handler will execute | ||
| myClass.onHook('before:myMethod', (data) => { | ||
| data.some = 'modified'; | ||
| }); | ||
| // This async handler will be silently skipped | ||
| myClass.onHook('before:myMethod', async (data) => { | ||
| data.some = 'will not run'; | ||
| }); | ||
| myClass.myMethodWithSyncHooks(); // Only sync handler runs | ||
| ``` | ||
| ## .hooks | ||
@@ -1335,4 +1374,4 @@ | ||
| |-----------------------|:---------:|----------:|----------:|:--------:|----------:| | ||
| | Hookified (v1.13.0) | 🥇 | 5M | 238ns | ±1.06% | 4M | | ||
| | Hookable (v5.5.3) | -68% | 1M | 826ns | ±2.25% | 1M | | ||
| | Hookified (v1.14.0) | 🥇 | 3M | 318ns | ±0.01% | 3M | | ||
| | Hookable (v6.0.1) | -57% | 1M | 834ns | ±0.01% | 1M | | ||
@@ -1339,0 +1378,0 @@ ## Emits |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
239450
4.18%3323
4.5%1425
2.81%