@poppinss/hooks
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -25,2 +25,6 @@ import { IocResolverContract } from '@adonisjs/fold'; | ||
/** | ||
* Adds the resolved handler to the actions set | ||
*/ | ||
private addResolvedHandler; | ||
/** | ||
* Returns a boolean whether a handler has been already registered or not | ||
@@ -42,2 +46,7 @@ */ | ||
/** | ||
* Merges hooks of a given hook instance. To merge from more than | ||
* one instance, you can call the merge method for multiple times | ||
*/ | ||
merge(hooks: Hooks): void; | ||
/** | ||
* Executes the hook handler for a given action and lifecycle | ||
@@ -44,0 +53,0 @@ */ |
@@ -48,2 +48,14 @@ "use strict"; | ||
/** | ||
* Adds the resolved handler to the actions set | ||
*/ | ||
addResolvedHandler(lifecycle, action, handler) { | ||
const handlers = this.hooks[lifecycle].get(action); | ||
if (handlers) { | ||
handlers.add(handler); | ||
} | ||
else { | ||
this.hooks[lifecycle].set(action, new Set([handler])); | ||
} | ||
} | ||
/** | ||
* Returns a boolean whether a handler has been already registered or not | ||
@@ -63,10 +75,3 @@ */ | ||
add(lifecycle, action, handler) { | ||
const handlers = this.hooks[lifecycle].get(action); | ||
const resolvedHandler = this.resolveHandler(handler); | ||
if (handlers) { | ||
handlers.add(resolvedHandler); | ||
} | ||
else { | ||
this.hooks[lifecycle].set(action, new Set([resolvedHandler])); | ||
} | ||
this.addResolvedHandler(lifecycle, action, this.resolveHandler(handler)); | ||
return this; | ||
@@ -96,2 +101,18 @@ } | ||
/** | ||
* Merges hooks of a given hook instance. To merge from more than | ||
* one instance, you can call the merge method for multiple times | ||
*/ | ||
merge(hooks) { | ||
hooks.hooks.before.forEach((actionHooks, action) => { | ||
actionHooks.forEach((handler) => { | ||
this.addResolvedHandler('before', action, handler); | ||
}); | ||
}); | ||
hooks.hooks.after.forEach((actionHooks, action) => { | ||
actionHooks.forEach((handler) => { | ||
this.addResolvedHandler('after', action, handler); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Executes the hook handler for a given action and lifecycle | ||
@@ -109,2 +130,3 @@ */ | ||
else { | ||
this.ensureResolver(); | ||
await this.resolver.call(handler, undefined, data); | ||
@@ -111,0 +133,0 @@ } |
{ | ||
"name": "@poppinss/hooks", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "A no brainer hooks module for execute before/after lifecycle hooks", | ||
@@ -35,4 +35,4 @@ "main": "build/index.js", | ||
"@adonisjs/fold": "^6.3.5", | ||
"@adonisjs/mrm-preset": "^2.3.6", | ||
"@types/node": "^14.0.14", | ||
"@adonisjs/mrm-preset": "^2.3.7", | ||
"@types/node": "^14.0.20", | ||
"commitizen": "^4.1.2", | ||
@@ -49,3 +49,3 @@ "cz-conventional-changelog": "^3.2.0", | ||
"mrm": "^2.3.3", | ||
"np": "^6.2.5", | ||
"np": "^6.3.1", | ||
"prettier": "^2.0.5", | ||
@@ -52,0 +52,0 @@ "ts-node": "^8.10.2", |
@@ -24,2 +24,3 @@ <div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/ | ||
- [clear(lifecycle: 'before' | 'after', action?: string)](#clearlifecycle-before--after-action-string) | ||
- [merge (hooks: Hooks): void](#merge-hooks-hooks-void) | ||
@@ -120,2 +121,15 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
#### merge (hooks: Hooks): void | ||
Merge hooks from an existing hooks instance. Useful during class inheritance. | ||
```ts | ||
const hooks = new Hooks() | ||
hooks.add('before', 'save', function () {}) | ||
const hooks1 = new Hooks() | ||
hooks1.merge(hooks) | ||
await hooks1.exec('before', 'save', []) | ||
``` | ||
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/hooks/master.svg?style=for-the-badge&logo=circleci | ||
@@ -122,0 +136,0 @@ [circleci-url]: https://circleci.com/gh/poppinss/hooks "circleci" |
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
14548
200
144