Socket
Socket
Sign inDemoInstall

hookies

Package Overview
Dependencies
1
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hookies

Simple hooks for your JavaScript objects


Version published
Maintainers
1
Install size
1.36 MB
Created

Readme

Source

Build Status Code Climate Test Coverage Dependency Status

hookies

Hookies is a very simple object specific publish/subscribe library. Hookies allows you to create "as many as you need" independent objects that will enjoy it's own on|off and trigger methods. Let me give you an example:

import { Hooks } from 'hookies';

class Cat extends Hooks {
  constructor(name) {
    super();
    this.name = name;
  }
}

class Mouse extends Hooks {
  constructor(name) {
    super();
    this.name = name;
  }
}

const tom = new Cat('Tom');
const jerry = new Mouse('Jerry');

// tom subscribes to `detect-mouse` event.
tom.on('detect-mouse', function (mouse) {
  console.log(this.name + ' has detected ' + mouse.name);

  mouse.hookies.trigger('detected-by-cat', this);
});

jerry.on('detected-by-cat', function (cat) {
  console.log(this.name + ' runs away, because ' + cat.name + ' is chasing him.');
});

tom.trigger('detect-mouse', jerry);

// This will produce following output:
//
// Tom has detected Jerry
// Jerry runs away, because Tom is chasing him.

Installation

As simple as:

npm install hookies

Usage

import { Hooks } from 'hookies';

const myHookie = new Hooks();

// Second argument can be optionally and object which will represent `this`
// inside a callback
myHookie.on('foo', { name: 'John' }, function () {
    console.log(this.name, arguments);
});

myHookie.trigger('foo', 1, 2, 3);
// John [1, 2, 3]

Callback functions are executed asynchronously by default, but you can force them to run synchronously too:

import { Hooks } from 'hookies';

const myHookie = new Hookies.Hooks();

myHookie.on('foo', { name: 'John' }, function () {
  console.log(this.name, arguments);
});

myHookie.trigger({
  name: 'foo',
  sync: true, // run synchronously
  // you can overwrite `this` inside callback function whenever you need to
  context: { name: 'Bob' }
}, 1, 2, 3);

console.log('I am sync');

myHookie.trigger('foo', 1, 2, 3);

console.log('I am async');

// Bob [1, 2, 3]
// I am sync
// I am async
// John [1, 2, 3]

License

The MIT License (MIT) - See file 'LICENSE' in this project

Copyright © 2016 Jiri Chara. All Rights Reserved.

FAQs

Last updated on 28 Sep 2016

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc