event-emitter-grouped

Emit events in serial or parallel with support for synchronous and asynchronous listeners

npm install event-emitter-grouped
173 downloads in the last day
663 downloads in the last week
2 862 downloads in the last month

Event Emitter Grouped

Build Status NPM version Dependency Status Development Dependency Status
Gittip donate button Flattr donate button PayPayl donate button BitCoin donate button

Emit events in serial or parallel with support for synchronous and asynchronous listeners

Install

Node

  • Use: require('event-emitter-grouped')
  • Install: npm install --save event-emitter-grouped

Usage

// Importer
var EventEmitterGrouped = require('event-emitter-grouped').EventEmitterGrouped;

// Instantiate a new instance
var emitter = new EventEmitterGrouped();

// Bind an asynchronous event
emitter.on('hello', function(next){
    console.log('\tasync started');
    setTimeout(function(){
        console.log('\tasync finished');
        next();
    }, 1000);
});

// Bind a synchronous event
emitter.on('hello', function(){
    console.log('\tsync started and finished');
});

// Bind a prioritized event
vipListener = function(){
    console.log('\tvip started and finished');
};
vipListener.priority = 1;
emitter.on('hello', vipListener);

// Emit the events in serial (one after the other in a waiting fashion)
console.log('hello in serial started');
emitter.emitSerial('hello', function(err){
    console.log('hello in serial finished');

    // Emit the events in parallel (all at once)
    console.log('hello in parallel started');
    emitter.emitParallel('hello', function(err){
        console.log('hello in parallel finished');
    });
});

/* Outputs:
hello in serial started
    vip started and finished
    async started
    async finished
    sync started and finished
hello in serial finished
hello in parallel started
    vip started and finished
    async started
    sync started and finished
    async finished
hello in parallel finished
*/

EventEmitterGrouped, extends events.EventEmitter

  • getListenerGroup(eventName, args..., next?) - returns a TaskGroup where each listener is a task, ordered by the highest priority listeners first
    • eventName is the event that we should get the listeners for
    • args... is an optional set of arguments that should be passed to the listeners when they are executed
    • next is an optional completion callback that will fire once all the tasks/listeners have compeleted
  • off - alias for events.EventEmitter.prototype.removeListener
  • emitSerial(eventName, args..., next?) - fetch the listener group and execute it in serial
  • emitParallel(eventName, args..., next?) - fetch the listener group and execute it in parallel

History

Discover the change history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

Gittip donate button Flattr donate button PayPayl donate button BitCoin donate button

Contributors

These amazing people have contributed code to this project:

Become a contributor!

License

Licensed under the incredibly permissive MIT license

Copyright © 2013+ Bevry Pty Ltd us@bevry.me (http://bevry.me)
Copyright © 2011-2012 Benjamin Lupton b@lupton.cc (http://balupton.com)

npm loves you