Home

Awesome

hook-emitter

NPM Version NPM Downloads

Promised-based chained event emitter with ability to create hooks around functions.

Installation

npm i hook-emitter --save

Examples

Async listener example:

import HookEmitter from 'hook-emitter';

const emitter = new HookEmitter();

emitter.on('sum', (x, y) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('the sum of ' + x + ' + ' + y + ' = ' + (x + y));
            resolve();
        }, 100);
    });
});

// emit and wait for all listeners to be called
await emitter.emit('sum', 3, 7);

Hook example:

const emitter = new HookEmitter();

const hookedSum = emitter.hook('sum', (x, y) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            // x = 6, y = 14
            resolve(x + y);
        }, 100);
    });
});

emitter.on('sum', function (x, y) {
    console.log('doubling x and y');
    this.args[0] *= 2;
    this.args[1] *= 2;
});

emitter.on('sum', async (x, y, next) => {
	console.log('doubling result after sum function has been called');
	const r = await next();
	r.result *= 2;
	return r;
});

const result = await hookedSum(3, 7);
console.log('The sum of (6 + 14) * 2 = ' + result);

Chaining multiple hooked functions example:

const emitter = new HookEmitter();

await emitter.hook('step1', () => {
    console.log('step 1');
})();

await emitter.hook('step2', () => {
    console.log('step 2');
})();

await emitter.hook('step3', () => {
    console.log('step 3');
})();

API

Constructor

The HookEmitter constructor takes no arguments.

Properties

events

A Map object of event names to arrays of listener functions. This can be iterated over using a for-of loop.

Methods

on(event, listener)

on(event, priority=0, listener)

Adds an event listener. Returns this.

once(event, listener)

once(event, priority=0, listener)

Adds an event listener that will only be called once. Returns this.

off(event, listener)

Removes an event listener. Returns this.

emit(event, ...args)

Emits one or more events. Returns a Promise.

hook(event, ctx, fn)

Creates a function hook. Returns a Function which when called returns a Promise.

Hook listeners are passed the same input arguments plus a next() callback. For example, if the hooked function accepts two arguments x and y, then the listeners will be called with x, y, and next. A listener only needs to call next() if it wishes be invoked after the hooked function has been called.

Listener functions are called in the context of the hook event meaning they can access:

emitter.on('foo', function (x, y, next) {
	console.log('event type:', this.type);
	console.log('args:', this.args);
	console.log('fn:', this.fn);

	// you can modify the args like this:
	this.args = [y, x];
});

link(emitter, prefix)

Links another HookEmitter to this instance. Useful if you have a class that extends a HookEmitter, then another HookEmitter that you want to receive the exact same events.

unlink(emitter)

Unlinks another HookEmitter from this instance. It does the opposite of link().

License

MIT