emit-stream3
Turn event emitters into streams and streams into event emitters. Streams3 version of emit-stream.
Usage
Borrowing from the original README
, let's write a server that streams
an event emitter's events to clients:
const emitStream = const JSONStream = const net = const EventEmitter = EventEmitter const server = { const ev = return net}server { const ev = let x = 0 return ev}
Then re-constitute the event emitters on the client:
const emitStream =const net =const stream = netconst ev =evev
Outputting:
$ node example/emit.js
x = 0
x = 1
x = 2
x = 3
# ping: 1346116850523
x = 4
x = 5
^C
API
const emitStream =
emitStream(x)
If x
is a stream, returns an event emitter from emit.toStream(x)
.
Otherwise, it returns a stream from emit.fromStream(x)
.
emitStream.toStream(emitter)
Return a stream from the EventEmitter emitter
.
The 'data'
emitted by this stream will be an array. Serialization is
up to you. I recommend JSONStream
for most purposes.
emitStream.fromStream(stream)
Return an EventEmitter from stream
.
The 'data'
written to this stream should be an array, like
JSONStream creates.
Installation
With npm do:
npm install emit-stream3