Skip to content

Commit

Permalink
fix: allow multiple plugins to "register" the same hook
Browse files Browse the repository at this point in the history
Allows multiple plugins to execute the same hook(s)
  • Loading branch information
travis-w committed Aug 30, 2022
1 parent c05b7f7 commit 9b5a470
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,5 +1,5 @@
# Hijacker
[![npm](https://img.shields.io/npm/v/hijacker.svg)](https://www.npmjs.com/package/hijacker)
[![npm](https://img.shields.io/npm/v/@hijacker/core.svg)](https://www.npmjs.com/package/@hijacker/core)
[![Build Status](https://github.com/travis-w/hijacker/actions/workflows/workflow.yml/badge.svg)](https://github.com/travis-w/hijacker/actions/workflows/workflow.yml)
[![Coverage Status](https://coveralls.io/repos/github/travis-w/hijacker/badge.svg?branch=master)](https://coveralls.io/github/travis-w/hijacker?branch=master)

Expand Down
15 changes: 3 additions & 12 deletions src/utils/HookManager.spec.ts
Expand Up @@ -30,15 +30,6 @@ describe('HookManager', () => {
expect(hookManager.hooks['REQUEST']).toEqual([]);
});

it('should not allow registering hook if one with same name already exists', () => {
expect.assertions(1);

hookManager.registerHook('REQUEST');
expect(() => {
hookManager.registerHook('REQUEST');
}).toThrow('A hook already exists with that name');
});

it('should allow registering handlers', () => {
expect.assertions(1);

Expand All @@ -53,7 +44,7 @@ describe('HookManager', () => {

expect(() => {
hookManager.registerHandler('REQUEST', () => null);
}).toThrow('No hook with that name exists');
}).toThrow('Can\'t register handler for non-existant hook \'REQUEST\'');
});

it('should execute handler on value', async () => {
Expand Down Expand Up @@ -83,7 +74,7 @@ describe('HookManager', () => {
expect.assertions(1);

expect(hookManager.executeHook('REQUEST', () => null))
.rejects.toThrow('No hook with that name exists');
.rejects.toThrow('Can\'t execute non-existant hook \'REQUEST\'');
});

it('should execute handlers in order they were registered', async () => {
Expand All @@ -103,7 +94,7 @@ describe('HookManager', () => {

expect(() => {
hookManager.executeSyncHook('REQUEST', () => null);
}).toThrow('No hook with that name exists');
}).toThrow('Can\'t execute non-existant hook \'REQUEST\'');
});

it('should throw error when async handler registered for a sync hook', () => {
Expand Down
12 changes: 5 additions & 7 deletions src/utils/HookManager.ts
Expand Up @@ -30,18 +30,16 @@ export class HookManager {
registerHook(hookName: string) {
this.logger.log('DEBUG', '[HookManager]', 'registerHook');

if (hookName in this.hooks) {
throw new Error('A hook already exists with that name');
if (hookName in this.hooks === false) {
this.hooks[hookName] = [];
}

this.hooks[hookName] = [];
}

registerHandler(hookName: string, handler: Handler) {
this.logger.log('DEBUG', '[HookManager]', 'registerHandler');

if (hookName in this.hooks === false) {
throw new Error('No hook with that name exists');
throw new Error(`Can't register handler for non-existant hook '${hookName}'`);
}

this.hooks[hookName].push(handler);
Expand All @@ -51,7 +49,7 @@ export class HookManager {
this.logger.log('DEBUG', '[HookManager]', 'executeHook');

if (hookName in this.hooks === false) {
throw new Error('No hook with that name exists');
throw new Error(`Can't execute non-existant hook '${hookName}'`);
}

return await this.hooks[hookName].reduce(async (acc, cur) => cur(await acc), initialVal);
Expand All @@ -61,7 +59,7 @@ export class HookManager {
this.logger.log('DEBUG', '[HookManager]', 'executeSyncHook');

if (hookName in this.hooks === false) {
throw new Error('No hook with that name exists');
throw new Error(`Can't execute non-existant hook '${hookName}'`);
}

return this.hooks[hookName].reduce((acc, cur) => {
Expand Down

0 comments on commit 9b5a470

Please sign in to comment.