What is propagate?
The 'propagate' npm package is used to propagate events from one event emitter to another. This is particularly useful in scenarios where you want to forward events from one object to another seamlessly without manually emitting events from the second object.
What are propagate's main functionalities?
Event Propagation
This feature allows you to forward all events from one EventEmitter to another. In the provided code, events from 'emitter1' are propagated to 'emitter2', so when 'emitter1' emits an 'event', 'emitter2' also handles and logs the event.
const EventEmitter = require('events');
const propagate = require('propagate');
let emitter1 = new EventEmitter();
let emitter2 = new EventEmitter();
// Propagate all events from emitter1 to emitter2
propagate(emitter1, emitter2);
emitter1.on('event', () => console.log('Handled by emitter1'));
emitter2.on('event', () => console.log('Handled by emitter2'));
emitter1.emit('event'); // Logs from both emitter1 and emitter2
Other packages similar to propagate
eventemitter2
EventEmitter2 is an implementation of EventEmitter with additional features like namespaces, wildcards, and timesToListen. It offers more flexibility and functionality compared to 'propagate' which mainly focuses on forwarding events.
eventemitter3
EventEmitter3 is a high performance EventEmitter primarily focused on performance. While it does not offer direct event propagation like 'propagate', it is optimized for a high number of events and listeners, making it suitable for performance-critical applications.
propagate
Propagate events from one event emitter into another
Install
$ npm install propagate
Propagate
var ee1 = new EventEmitter();
var ee2 = new EventEmitter();
propagate(ee1, ee2);
ee2.on('event', function(a, b) {
console.log('got propagated event', a, b);
});
ee1.emit('event', 'a', 'b');
Unpropagate
You can unpropagate by ending the propagation like this:
var ee1 = new EventEmitter();
var ee2 = new EventEmitter();
var p = propagate(ee1, ee2);
p.end();
Only propagate certain events:
var ee1 = new EventEmitter();
var ee2 = new EventEmitter();
var p = propagate(['event1', 'event2'], ee1, ee2);
Propagate certain events as other events:
var ee1 = new EventEmitter();
var ee2 = new EventEmitter();
var p = propagate({
'event1': 'other-event1',
'event2': 'other-event2'
}, ee1, ee2);
License
MIT