Comparing version 0.6.0 to 0.7.0
@@ -6,6 +6,8 @@ import Emittery from 'emittery'; | ||
constructor(); | ||
onHook(event: string, handler: Hook): Promise<void>; | ||
removeHook(event: string, handler: Hook): Promise<void>; | ||
hook(event: string, data: any): Promise<void>; | ||
onHook(event: string, handler: Hook): void; | ||
removeHook(event: string, handler: Hook): void; | ||
hook(event: string, ...data: any[]): Promise<void>; | ||
get hooks(): Map<string, Hook[]>; | ||
getHooks(event: string): Hook[] | undefined; | ||
clearHooks(): void; | ||
} |
@@ -9,3 +9,3 @@ import Emittery from 'emittery'; | ||
// Adds a handler function for a specific event | ||
async onHook(event, handler) { | ||
onHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
@@ -20,3 +20,3 @@ if (eventHandlers) { | ||
// Removes a specific handler function for a specific event | ||
async removeHook(event, handler) { | ||
removeHook(event, handler) { | ||
const eventHandlers = this._hooks.get(event); | ||
@@ -31,3 +31,3 @@ if (eventHandlers) { | ||
// Triggers all handlers for a specific event with provided data | ||
async hook(event, data) { | ||
async hook(event, ...data) { | ||
const eventHandlers = this._hooks.get(event); | ||
@@ -37,4 +37,4 @@ if (eventHandlers) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
await handler(data); | ||
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-unsafe-argument | ||
await handler(...data); | ||
} | ||
@@ -53,3 +53,9 @@ catch (error) { | ||
} | ||
getHooks(event) { | ||
return this._hooks.get(event); | ||
} | ||
clearHooks() { | ||
this._hooks.clear(); | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "hookified", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Event and Middleware Hooks", | ||
@@ -5,0 +5,0 @@ "type": "module", |
<img src="site/logo.svg" alt="Hookified" height="400" align="center"> | ||
# Event and Middleware Hooks for Your Libraries | ||
# Async Event and Middleware Hooks | ||
@@ -13,6 +13,8 @@ [![tests](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml) | ||
- Emit Events via [Emittery](https://npmjs.com/package/emittery) | ||
- Middleware Hooks with data passing | ||
- Async Middleware Hooks for Your Methods | ||
- ESM and Nodejs 20+ | ||
- Maintained on a regular basis! | ||
Special thanks to [@sindresorhus](https://github.com/sindresorhus) for the [Emittery](https://npmjs.com/package/emittery) library. 🍻 | ||
## Installation | ||
@@ -35,3 +37,3 @@ ```bash | ||
async myMethodEmittingEvent() { | ||
await this.emit('message', 'Hello World'); | ||
await this.emit('message', 'Hello World'); //using Emittery | ||
} | ||
@@ -48,3 +50,69 @@ | ||
} | ||
``` | ||
You can even pass in multiple arguments to the hooks: | ||
```javascript | ||
import { Hookified } from 'hookified'; | ||
class MyClass extends Hookified { | ||
constructor() { | ||
super(); | ||
} | ||
async myMethodWithHooks() Promise<any> { | ||
let data = { some: 'data' }; | ||
let data2 = { some: 'data2' }; | ||
// do something | ||
await this.hook('before:myMethod2', data, data2); | ||
return data; | ||
} | ||
} | ||
``` | ||
## API | ||
Please see the [Emittery](https://npmjs.com/package/emittery) documentation for more information on the event emitter. | ||
### .onHook(eventName, handler) | ||
Subscribe to a hook event. | ||
### .removeHook(eventName) | ||
Unsubscribe from a hook event. | ||
### .hook(eventName, ...args) | ||
Run a hook event. | ||
### .hooks | ||
Get all hooks. | ||
### .getHooks(eventName) | ||
Get all hooks for an event. | ||
### .clearHooks(eventName) | ||
## Development and Testing | ||
Hookified is written in TypeScript and tests are written in `vitest`. To run the tests, use the following command: | ||
To setup the environment and run the tests: | ||
```bash | ||
npm i && npm test | ||
``` | ||
To contribute follow the [Contributing Guidelines](CONTRIBUTING.md) and [Code of Conduct](CODE_OF_CONDUCT.md). | ||
## License | ||
[MIT & © Jared Wray](LICENSE) | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9516
67
116