Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hooks-mixin

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hooks-mixin

A small mixin for classes. Adds hooks ability to your classes

  • 1.1.0
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

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 “=)”

FAQs

Package last updated on 10 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc