0.1.9 • Published 5 years ago
iterate-evented-stream v0.1.9
Iterate Evented Stream
Create an AsyncIterable from an evented stream.
Usage Examples
All code snippets below work in an async context.
Custom EventEmitter
In order for an EventEmitter to be a valid EventTarget,
It must listen to 3 required events:
('data', string)('error', Error)('close')
It must provide 2 required methods for each event:
addListenerremoveListener
Iteration stops when the
EventEmitteremits'close'or'error'- If it emits
'close', iteration is considered completed. - If it emits
'error', iteration throws the emitted error.
- If it emits
const EventEmitter = require('events')
const iterate = require('iterate-evented-stream')
const emitter = new EventEmitter()
setTimeout(() => emitter.emit('data', '100 ms'), 100)
setTimeout(() => emitter.emit('data', '200 ms'), 200)
setTimeout(() => emitter.emit('data', '300 ms'), 300)
setTimeout(() => emitter.emit('data', '400 ms'), 400)
setTimeout(() => emitter.emit('close'), 500)
for await (const chunk of iterate(emitter)) {
console.log(chunk)
}Expected Result:
100 ms
200 ms
300 ms
400 msChild Process
NOTE: Replace foo, bar and baz below with commands that exist.
const { spawn } = require('child_process')
const combine = require('combine-evented-stream')
const iterate = require('iterate-evented-stream')
const cps = [
'foo',
'bar',
'baz'
].map(program => spawn(program))
const stream = combine(cps)
for await (const { stream, chunk } of iterate(stream)) {
console.log({
pid: stream.pid,
data: chunk
})
}
console.log('done.')