diff --git a/src/index.ts b/src/index.ts index 5788283..fc030ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); @@ -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); @@ -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(); + } } diff --git a/test/index.test.ts b/test/index.test.ts index faa8824..c99c4cd 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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); }); @@ -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); }); @@ -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'); }); @@ -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'); @@ -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'); });