Skip to content

Commit

Permalink
updating with getHooks and clearHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Sep 6, 2024
1 parent dc4f422 commit c423d9c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Hookified extends Emittery {
}

// Adds a handler function for a specific event
async onHook(event: string, handler: Hook) {
onHook(event: string, handler: Hook) {
const eventHandlers = this._hooks.get(event);
if (eventHandlers) {
eventHandlers.push(handler);
Expand All @@ -21,7 +21,7 @@ export class Hookified extends Emittery {
}

// Removes a specific handler function for a specific event
async removeHook(event: string, handler: Hook) {
removeHook(event: string, handler: Hook) {
const eventHandlers = this._hooks.get(event);
if (eventHandlers) {
const index = eventHandlers.indexOf(handler);
Expand Down Expand Up @@ -52,4 +52,12 @@ export class Hookified extends Emittery {
// Creating a new map to prevent external modifications to the original map
return this._hooks;
}

getHooks(event: string) {
return this._hooks.get(event);
}

clearHooks() {
this._hooks.clear();
}
}
40 changes: 27 additions & 13 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@ describe('Hookified', () => {
const handler = () => {};
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler2 = () => {};
await hookified.onHook('event', handler);
await hookified.onHook('event2', handler2);
expect(hookified.hooks.get('event')).toEqual([handler]);
expect(hookified.hooks.get('event2')).toEqual([handler2]);
hookified.onHook('event', handler);
hookified.onHook('event2', handler2);
expect(hookified.getHooks('event')).toEqual([handler]);
expect(hookified.getHooks('event2')).toEqual([handler2]);
expect(hookified.hooks.size).toBe(2);
});

test('onHook with Clear', async () => {
const hookified = new Hookified();
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler = () => {};
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler2 = () => {};
hookified.onHook('event', handler);
hookified.onHook('event2', handler2);
expect(hookified.getHooks('event')).toEqual([handler]);
expect(hookified.getHooks('event2')).toEqual([handler2]);
hookified.clearHooks();
expect(hookified.hooks.size).toBe(0);
});

test('onHook multiple handlers', async () => {
const hookified = new Hookified();
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler = () => {};
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler2 = () => {};
await hookified.onHook('event', handler);
await hookified.onHook('event', handler2);
hookified.onHook('event', handler);
hookified.onHook('event', handler2);
expect(hookified.hooks.get('event')).toEqual([handler, handler2]);
expect(hookified.hooks.size).toBe(1);
});
Expand All @@ -38,10 +52,10 @@ describe('Hookified', () => {
const handler = () => {};
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handler2 = () => {};
await hookified.onHook('event', handler);
await hookified.onHook('event', handler2);
await hookified.removeHook('event', handler);
expect(hookified.hooks.get('event')).toEqual([handler2]);
hookified.onHook('event', handler);
hookified.onHook('event', handler2);
hookified.removeHook('event', handler);
expect(hookified.getHooks('event')).toEqual([handler2]);
expect(hookified.hooks.size).toBe(1);
});

Expand All @@ -56,7 +70,7 @@ describe('Hookified', () => {
handlerData = data;
};

await hookified.onHook('event', handler);
hookified.onHook('event', handler);
await hookified.hook('event', data);
expect(handlerData.key).toBe('modified');
});
Expand All @@ -78,7 +92,7 @@ describe('Hookified', () => {
console.log(handlerData);
};

await hookified.onHook('event', handler);
hookified.onHook('event', handler);
await hookified.hook('event', data, data2, data3);
expect(handlerData[0].key).toBe('modified');
expect(handlerData[1].key).toBe('foo');
Expand All @@ -99,7 +113,7 @@ describe('Hookified', () => {
throw new Error('error');
};

await hookified.onHook('event', handler);
hookified.onHook('event', handler);
await hookified.hook('event', data);
expect(errorMessage).toBe('Error in hook handler for event "event": error');
});
Expand Down

0 comments on commit c423d9c

Please sign in to comment.