weighted-emitter

1.0.3 • Public • Published

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

Package Sidebar

Install

npm i weighted-emitter

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • orgun109uk