event-emitter-grouped

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

Event Emitter Grouped


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

NPM

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

Browserify

  • Use: require('event-emitter-grouped')
  • Install: npm install --save event-emitter-grouped
  • CDN URL: //wzrd.in/bundle/event-emitter-grouped@2.4.3

Ender

  • Use: require('event-emitter-grouped')
  • Install: ender add event-emitter-grouped
// 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

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

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

These amazing people are maintaining this project:

  • Benjamin Lupton b@lupton.cc (https://github.com/balupton)

No sponsors yet! Will you be the first?

These amazing people have contributed code to this project:

Become a contributor!

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)