1.3.0 • Published 3 years ago

hooks-mixin v1.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

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() {
    // 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:

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

const preSaveHookFunction = () => console.info('=)');
instance.hook('pre-save', preSaveHookFunction);

instance.save();
// =)
instance.removeHook('pre-save', preSaveHookFunction);
instance.save();
// no “=)”

Frontend

Just import or require from build/index.js.

1.3.0

3 years ago

1.2.2

4 years ago

1.2.1

5 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago