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
const HooksMixin = require('hooks-mixin');
class Class {
async save() {
this.processHooks('pre-save');
await this.write();
await this.processHooksAsync('saved');
return this;
}
};
HooksMixin(Class);
const instance = new Class();
instance.hook('pre-save', (instance) => {
});
instance.save();
You can add many hooks:
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();
Remove hook callback
const preSaveHookFunction = () => console.info('=)');
instance.hook('pre-save', preSaveHookFunction);
instance.save();
instance.removeHook('pre-save', preSaveHookFunction);
instance.save();
Frontend
Just import
or require
from build/index.js
.