hooks-mixin
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -91,5 +91,5 @@ /** | ||
Class.prototype.removeHook = removeHook; | ||
Class.prototype.__processHooks = processHooks; | ||
Class.prototype.__processHooksAsync = processHooksAsync; | ||
Class.prototype.processHooks = processHooks; | ||
Class.prototype.processHooksAsync = processHooksAsync; | ||
return Class; | ||
}; |
@@ -14,4 +14,4 @@ /*global test expect jest */ | ||
expect(instance).toHaveProperty('removeHook'); | ||
expect(instance).toHaveProperty('__processHooks'); | ||
expect(instance).toHaveProperty('__processHooksAsync'); | ||
expect(instance).toHaveProperty('processHooks'); | ||
expect(instance).toHaveProperty('processHooksAsync'); | ||
expect(instance).not.toHaveProperty('__hooks'); | ||
@@ -29,3 +29,3 @@ instance.hook('test', () => {}); | ||
instance.hook('test', mockHook); | ||
instance.__processHooks('test'); | ||
instance.processHooks('test'); | ||
expect(mockHook).toHaveBeenCalledTimes(2); | ||
@@ -44,3 +44,3 @@ }); | ||
}); | ||
instance.__processHooks('test', 1, 2, 3); | ||
instance.processHooks('test', 1, 2, 3); | ||
expect(called).toBe(true); | ||
@@ -56,3 +56,3 @@ }); | ||
}); | ||
instance.__processHooks('test'); | ||
instance.processHooks('test'); | ||
expect(called).toBe(true); | ||
@@ -92,3 +92,3 @@ }); | ||
instance.test = async function() { | ||
await this.__processHooksAsync('test'); | ||
await this.processHooksAsync('test'); | ||
expect(called).toBe(TIMES); | ||
@@ -95,0 +95,0 @@ done = true; |
{ | ||
"name": "hooks-mixin", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A small mixin for classes. Adds hooks ability to your classes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# hooks-mixin | ||
A small mixin for classes. Adds hooks ability to your classes | ||
## Install | ||
### Yarn | ||
``` | ||
yarn add hooks-mixin | ||
``` | ||
### NPM | ||
``` | ||
npm i hooks-mixin | ||
``` | ||
## Use | ||
### Add and call | ||
```javascript | ||
const HooksMixin = require('hooks-mixin'); | ||
class Class { | ||
async save() { | ||
// sync hook call | ||
this.processHooks('pre-save'); | ||
await this.write(); | ||
// async hook call | ||
await this.processHooksAsync('saved'); | ||
return this; | ||
} | ||
}; | ||
HooksMixin(Class); | ||
const instance = new Class(); | ||
// add hook | ||
instance.hook('pre-save', (instance) => { | ||
// instance — is an instance this | ||
// things with instance | ||
}); | ||
instance.save(); | ||
// will call pre-save and saved hooks | ||
``` | ||
You can add many hooks: | ||
```javascript | ||
instance.hook('pre-save', () => console.info('One')); | ||
instance.hook('pre-save', () => console.info('Two')); | ||
instance.hook('saved', () => console.info('Three')); | ||
instance.hook('saved', () => console.info('Four')); | ||
instance.save(); | ||
// One, Two, Three, Four | ||
``` | ||
### Remove hook callback | ||
```javascript | ||
const preSaveHookFunction = () => console.info('=)'); | ||
instance.hook('pre-save', preSaveHookFunction); | ||
instance.save(); | ||
// =) | ||
instance.removeHook('pre-save', preSaveHookFunction); | ||
instance.save(); | ||
// no “=)” | ||
``` |
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
8305
66