Skip to content

Combines multiple event emitters into a single emitter

License

Notifications You must be signed in to change notification settings

kevinmstephens/kilt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#kilt

Combines multiple event emitters into a single emitter.

Build Status

Lead Maintainer - Emily Rose

Usage

Installation:

$ npm install --save kilt

Instantiate a kilt instance with the event emitters to combine and listen for events on the new kilt instance. Kilt derives from EventEmitter and may also be used to emit events.

var Events = require('events');
var Kilt   = require('kilt');

var emitter1 = new Events.EventEmitter();
var emitter2 = new Events.EventEmitter();

var kilt = new Kilt([emitter1, emitter2]);

// A single handler for both emitters.
kilt.on('hello', function (data) {
    console.log(data);
});

// Emit events from multiple emitters.
emitter1.emit('hello', 'Hello from emitter1');
emitter2.emit('hello', 'Hello from emitter2');

// Emit a event using kilt.
kilt.emit('hello', 'Hello from Kilt');

Output:

Hello from emitter1
Hello from emitter2
Hello from kilt

Methods

Kilt([emitter(s)])

Kilt constructor which accepts optional emitters to manage.

var kilt = new Kilt();
... = new Kilt(emitter);
... = new Kilt([emitter]);
... = new Kilt([emitter1, emitter2]);
addEmitter(emitter)

Add an emitter for kilt to manage.

var emitter = new Events.Emitter();
kilt.addEmitter(emitter);
on(type, listener)

Attach a listener to all emitters with the specified type.

kilt.on('example', function (data) {
    console.log(data);
});
once(type, listener)

Attach a listener to all emitters with the specified type that will only fire once.

kilt.once('example', console.log.bind(console, '"example" event emitted once with data:'));
removeListener(type, listener)

Remove the specified listener.

var emitter = new Events.Emitter();
var listener = function () {
    return;
};

kilt.addEmitter(emitter);

// Attach listener.
kilt.on('example', listener);

// Remove listener.
kilt.removeListener('example', listener);
removeAllListeners([type])

Remove all listeners. Optionally, you may specify the type of listeners to remove.

// Attach listeners.
kilt.on('example', ...);
kilt.on('example', ...);
kilt.on('example', ...);
kilt.on('other', ...);

// Only remove listeners of a specific type.
kilt.removeAllListeners('example');

// Remove all listeners.
kilt.removeAllListeners();
emit(type[, data])

Emit the specified event with the specified, optional data.

var emitter = new Events.Emitter();
var kilt = new Kilt(emitter);

// Attach listener.
kilt.on('example', console.log.bind(console, '"example" event emitted with data:'));

// Emit event on kilt.
kilt.emit('example', 'emitted');

About

Combines multiple event emitters into a single emitter

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%