1.0.3 • Published 7 years ago

weighted-emitter v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

Build Status Build Status npm version

NPM

Weighted Emitter

This utility is a basic event emitter, and allows sorting the listener callbacks by weight.

Installation

$ npm install weighted-emitter

Usage

const WeightedEmitter = require('weighted-emitter');
const emitter = new WeightedEmitter();

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push('world');
    next();
});

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push(this.args.message1);
}, -10);

emitter.emit('event', {message1: 'hello'}, function (err, result) {
    console.info(result);
    /**
     * Result = ['hello', 'world'];
     */
});

Listeners can also be removed;

emitter.un('event', callback);
// Or to remove all listeners of an event;
emitter.un('event');

Testing

A mocha test suite has been provided and can be run by:

$ npm test