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'); + }); });