Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in prepend functionality on hooks #32

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,66 @@ myClass.myMethodWithHooks();
console.log(myClass.hooks.length); // 0
```

## .prependHook(eventName, handler)

Subscribe to a hook event before all other hooks.

```javascript
import { Hookified } from 'hookified';

class MyClass extends Hookified {
constructor() {
super();
}

async myMethodWithHooks() Promise<any> {
let data = { some: 'data' };
// do something
await this.hook('before:myMethod2', data);

return data;
}
}

const myClass = new MyClass();
myClass.onHook('before:myMethod2', async (data) => {
data.some = 'new data';
});
myClass.preHook('before:myMethod2', async (data) => {
data.some = 'will run before new data';
});
```

## .prependOnceHook(eventName, handler)

Subscribe to a hook event before all other hooks. After it is used once it will be removed.

```javascript
import { Hookified } from 'hookified';

class MyClass extends Hookified {
constructor() {
super();
}

async myMethodWithHooks() Promise<any> {
let data = { some: 'data' };
// do something
await this.hook('before:myMethod2', data);

return data;
}
}

const myClass = new MyClass();
myClass.onHook('before:myMethod2', async (data) => {
data.some = 'new data';
});
myClass.preHook('before:myMethod2', async (data) => {
data.some = 'will run before new data';
});
```

## .removeHook(eventName)

Unsubscribe from a hook event.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hookified",
"version": "1.5.1",
"version": "1.6.0",
"description": "Event and Middleware Hooks",
"type": "module",
"main": "dist/node/index.cjs",
Expand Down
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ export class Hookified extends Eventified {
}
}

/**
* Adds a handler function for a specific event that runs before all other handlers
* @param {string} event
* @param {Hook} handler - this can be async or sync
* @returns {void}
*/
prependHook(event: string, handler: Hook) {
const eventHandlers = this._hooks.get(event);
if (eventHandlers) {
eventHandlers.unshift(handler);
} else {
this._hooks.set(event, [handler]);
}
}

/**
* Adds a handler that only executes once for a specific event before all other handlers
* @param event
* @param handler
*/
prependOnceHook(event: string, handler: Hook) {
const hook = async (...arguments_: any[]) => {
this.removeHook(event, hook);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return handler(...arguments_);
};

this.prependHook(event, hook);
}

/**
* Adds a handler that only executes once for a specific event
* @param event
Expand Down
45 changes: 45 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,49 @@ describe('Hookified', () => {
await hookified.hook('event', data);
expect(data.key).toBe('modified');
});

test('prepends hook to the beginning of the array', async () => {
const hookified = new Hookified();
const handlerData: string[] = [];

const handler = () => {
handlerData.push('modified2');
};

const handler2 = () => {
handlerData.push('modified1');
};

hookified.onHook('event', handler);
hookified.prependHook('event', handler2);
await hookified.hook('event');
expect(handlerData[0]).toBe('modified1');
expect(handlerData[1]).toBe('modified2');
});

test('prepends hook and creates new array', async () => {
const hookified = new Hookified();
const handlerData: string[] = [];

const handler = () => {
handlerData.push('modified1');
};

hookified.prependHook('event', handler);
await hookified.hook('event');
expect(handlerData[0]).toBe('modified1');
});

test('prepends a hook and removes it', async () => {
const hookified = new Hookified();
const handlerData: string[] = [];

const handler = () => {
handlerData.push('modified1');
};

hookified.prependOnceHook('event20', handler);
await hookified.hook('event20');
expect(hookified.hooks.get('event20')?.length).toBe(0);
});
});
Loading