/events-extra

NodeJS EventEmitter with some advanced features

Primary LanguageJavaScriptApache License 2.0Apache-2.0

events-extra

NodeJS EventEmitter with some advanced features

npm Build Status Coverage Status Dependency Status PRs Welcome license

You were my oppressor
And I, I have been programmed to obey
But now you are my handler
And I, I will execute your demands

Muse

Install

Add package into your project:

  • via npm: npm install --save events-extra
  • via yarn: yarn add event-extra

Usage

Include package into your sourse code.

const EventsExtra = require('events-extra');

or even:

import EventsExtra from 'events-extra';

EventsExtra - is sub-class of NodeJS EventEmitter class and inherits all properties and methods of original class.

But it also add some advanced instance methods:

  • emitAndWait
  • passthroughEvent

and static class methods:

  • passthroughEvent
  • passthroughEventAsync

emitAndWait

Emits event and wait for fullfilled or rejected promises returned by all listener handlers. Returns array of resolved values if all handlers returns fullfilled promises, or rejection reason of first rejected handler.

Example:

const EventsExtra = require('events-extra');

const myEmitter = new EventsExtra();

myEmitter.on('foo', (data) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve('Hello ' + data);
        }, 1000)
    });
});

myEmitter.emitAndWait('foo', 'World')
    .then((result) => {
        console.log(result); // ['Hello World']
    });

passthroughEvent

Appends listener to given emitter for given events, handles these events and triggers them itself.

Example:

const EventsExtra = require('events-extra');

const sourceEmitter = new EventsExtra();
const destEmitter = new EventsExtra();

destEmitter.passthroughEvent(sourceEmitter, 'foo');

destEmitter.on('foo', (data) => {
    console.log('Hello ' + data);
});

sourceEmitter.emit('foo', 'World');

passthroughEvent - also works with async events. Use emitAndWait istead of emit for triggering initial event on source emitter.

Example:

const EventsExtra = require('events-extra');

const sourceEmitter = new EventsExtra();
const destEmitter = new EventsExtra();

destEmitter.passthroughEvent(sourceEmitter, 'foo');

destEmitter.on('foo', (data) => {
    return Promise.resove('Hello ' + data);
});

sourceEmitter.emitAndWait('foo', 'World')
    .then((res) => {
        console.log(res); // [['Hello World']]
    });

passthroughEvent (class method)

Static method for passthough event(s) from one event emitter to another.

Example:

const EventsExtra = require('events-extra');

const sourceEmitter = new EventsExtra();
const destEmitter = new EventsExtra();

EventsExtra.passthroughEvent(sourceEmitter, destEmitter, ['foo', 'bar']);

destEmitter.on('foo', (data) => {
    console.log('Hello ' + data);
});

destEmitter.on('bar', (data) => {
    console.log('Goodbye ' + data);
});

sourceEmitter.emit('foo', 'World'); // Hello World
sourceEmitter.emit('bar', 'World'); // Goodbye World

passthroughEventAsync (class method)

Same as passthroughEvent but for async event handling.

Acknowledgement

This code is separate implementation of EventEmitter subclasses taken from:

repositories and different plugins for these tools.

Special thanks to: