npm

Share private packages across your team with npm Orgs, now with simplified billing via the aws marketplace!Learn more »

houk

1.1.1 • Public • Published

Houk

Build Status install size npm version

A super simple event bus built for hook chains.

Installation

yarn add houk
# or using npm 
npm i houk

Example

import Houk from 'houk';
 
class MyClass extends Houk {
    constructor() {
        super();
    }
 
    async fire() {
        const value = 'Hello $name!';
 
        const newValue = await this.emit(
            'myEvent', // event name
            { user: 'Max' }, // will be available to the listener function as `this`
            value // will be passed as a parameter to the listener
        );
 
        console.log('All hooks ran, the new value is ', newValue);
    }
}
 
const myInstance = new MyClass();
myInstance.on('myEvent', function(value) {
    // Please note that `this` is not available in an arrow function!
    const { user } = this;
    return value.replace('$user', user);
});
 
myInstance.fire();

If you just want an open hook bus, you can use HoukBus, where all methods are public.

import { HoukBus } from 'houk';
 
const bus = new HoukBus();
 
bus.on('newUser', user => console.log(`Hello ${user}!`));
 
bus.emit('newUser', undefined, 'Max');
 
// --> Hello Max!

API

The same API applies to HoukBus as well, with the difference being that it's not an abstract class and all methods are public.

Houk.on

Listen to a particular event.

on(eventstring, fn: (...q: any) => any)void

Houk.off

Unregister an event listener. Returns true on success and false when the event listener didn't exist before.

off(eventstring, fn: (...q: any) => any)boolean

Houk.emit

Only available to deriving classes. Trigger all listeners of a particular event. thisArg will be available as this to the listener function (as long as it is not an arrow function), ...args will be passed along to the listener. Returns a promise containing the listener return value.

emit(
  eventstring,
  thisArg?: any,
  ...argsany
)Promise<any>

Houk.getListeners

Only available to deriving classes, returns a set of listener functions.

Keywords

none

install

npm i houk

Downloadsweekly downloads

43

version

1.1.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability