StreamEmitter
This module takes the easy interface of EventEmitters and replaces callbacks with streams. After wrapping a source emitter you can turn any events related to it into streams, and can pipe streams to emit events on data.
Installing
npm install --save streamemitter
API
StreamEmitter([eventemitter])
Constructs a new StreamEmitter and wraps over
the given eventemitter
and uses it for creating streams.
If no eventemitter is provided, one is created behind the scenes
StreamEmitter#on(event)
Returns a readable stream which receives data every
time event
is emitted on the eventemitter
StreamEmitter#emit(event)
Returns a writable stream which emits any data passed in on the eventemitter with the given event.
StreamEmitter#synchronize
A duplex stream that can be used for synchronizing instances of StreamEmitter
Note: Currently only events that are emitted using the StreamEmitter#emit
method can be synchronized. The alternative would require that the eventemitter
's
emit
method gets patched, which may not always be desirable.
Example
var StreamEmitter = StreamEmitter;var EventEmitter = EventEmitter;var events = ;var streams = events;// Relay all `message` events to STDOUTstreams;// Relay all `message` events into `message2` using the writable stream from emitstreams;// Relay all `message2` events to STDOUT as wellstreams;var times = 5;var interval =;
Synchronization example
var StreamEmitter = StreamEmitter;var StreamArray = ;var stdout = ;var e1 = ;var e2 = ;e1synchronize;e2;;