events-merge
v1.2.1
Published
A small tool for merging EventEmitters.
Downloads
1
Maintainers
Readme
events-merge v1.2.1
A small utility for merging two+ EventEmitter
s together.
Installing
$ npm install --save events-merge
Testing
$ cd events-merge
$ npm install --only=dev
$ npm test
Getting Started
var merge = require('events-merge');
var Emitter = require('events').EventEmitter;
var counter = 0;
var base = new Emitter();
var emitters = [
new Emitter(),
new Emitter()
];
base.on('inc', () => { counter++ });
emitters[0].on('inc', () => { counter++ });
emitters[1].on('dec', () => { counter-- });
merge.to(base).emitters(emitters);
base.emit('inc'); // counter == 2
base.emit('inc'); // counter == 4
base.emit('dec'); // counter == 3
or simply
var merge = require('events-merge').merge;
// code code code...
merge(base, emitter1, emitter2, ...); // base is now merged with the given emitters.
Using the es6 flavor
merge
uses the es5 syntax by default.
If your node.js version does not support es6 syntax, you can overcome this problem by requiring events-merge/es5
lib instead.
var merge = require('events-merge/es6');
NOTICE that require('events-merge/es6').merge
shortcut DOESN'T work, so use the es6 flavor only with the oop api.
API
merge.set(key:String, value) : self
Sets a default option for the events-merge
module.
merge.set('overwrite', true);
merge.base(emitter:Emitter) : self
Sets a base emitter to merge other emitters to.
merge.base(emitter).emitters(emitter1, emitter2, ...);
merge.to(emitter:Emitter) : self
Alias to merge.base
.
merge.to(emitter).merge(emitter1, emitter2, ...);
merge.merge(...emitters:Emitter) : Emitter
merge.merge(emitters:Array) : Emitter
Merge the given emitters
(by array or by seperated param) to the base emitter. If not base emitter given, uses the first emitter in the given emitters
list.
var merge = require('events-merge').merge; merge(base, emitter1, emitter2, ...); // base is now merged with the given emitters.
merge.emitters(...emitters:Emitter) : Emitter
merge.emitters(emitters:Array) : Emitter
Alias for merge.merge
.
[getter] merge.overwrite
Sets the overwrite
flag to true
.
merge.overwrite.to(base).merge(emitter1, emitter2, ...);
[static] merge.eventNamesOf(emitter:Emitter) : Array
A helper method that returns list of event names that a given emitter
holds.
emitter.on('ev1', someMethod); emitter.on('ev2', anotherMethod); console.log(merge.eventNamesOf(emitter)); // prints ['ev1', 'ev2'].
LICENSE
Yo! Follow me on GitHub @amit3vr. You might find my shit useful someday.