From f37594df7c21b4ee7f046a9ad1b42e8c0777ce25 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Wed, 16 Oct 2024 14:45:24 -0700 Subject: [PATCH] adding in sync handler for hooks --- README.md | 5 +++-- src/index.ts | 2 +- test/index.test.ts | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 655f7d1..25f35fd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![site/logo.svg](site/logo.svg) -# Event Emitting and Async Middleware Hooks +# Event Emitting and Middleware Hooks [![tests](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml) [![GitHub license](https://img.shields.io/github/license/jaredwray/hookified)](https://github.com/jaredwray/hookified/blob/master/LICENSE) @@ -10,8 +10,9 @@ # Features - Simple replacement for EventEmitter -- Async Middleware Hooks for Your Methods +- Async / Sync Middleware Hooks for Your Methods - ESM / CJS with Types and Nodejs 20+ +- Browser Support and Delivered via CDN - Maintained on a regular basis! # Installation diff --git a/src/index.ts b/src/index.ts index ccc339e..83ff7d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ export class Hookified extends Eventified { /** * Adds a handler function for a specific event * @param {string} event - * @param {Hook} handler + * @param {Hook} handler - this can be async or sync * @returns {void} */ onHook(event: string, handler: Hook) { diff --git a/test/index.test.ts b/test/index.test.ts index b58ef6d..f2bb877 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -115,4 +115,17 @@ describe('Hookified', () => { await hookified.hook('event', data); expect(errorMessage).toBe('Error in hook handler for event "event": error'); }); + + test('hook with sync function', async () => { + const hookified = new Hookified(); + const data = {key: 'value'}; + + const handler = (data: any) => { + data.key = 'modified'; + }; + + hookified.onHook('event', handler); + await hookified.hook('event', data); + expect(data.key).toBe('modified'); + }); });