Skip to content

Commit

Permalink
Merge pull request #19 from jaredwray/adding-in-sync-handler-for-hooks
Browse files Browse the repository at this point in the history
adding in sync handler for hooks
  • Loading branch information
jaredwray authored Oct 16, 2024
2 parents 479d7b7 + f37594d commit 1592d83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 1592d83

Please sign in to comment.