1.0.0 • Published 7 years ago
stream-observer v1.0.0
stream-observer
Listen for data events on a readable stream without triggering flowing
mode.
Why?
Normally a readable stream will start to emit data events (i.e. enter
"flowing mode") right after you attach the first data event listener.
But sometimes it's desirable to attach a data event listener without
the side effect of enabling flowing mode.
E.g. if you want to subscribe to the content of a stream but are not in
charge of when the stream starts flowing - say for instance if you hand
the stream off to someone else that are not attaching their own data
event listener right away.
This module gives you that ability.
Installation
npm install stream-observer --saveUsage
const fs = require('fs')
const streamObserver = require('stream-observer')
const stream = fs.createReadStream(__filename)
// Register observer to listen for data events once the stream starts
// flowing. Callback will be called with the data chunks.
streamObserver(stream, function (chunk) {
console.log(`stream produced ${chunk.length} bytes of data`)
})
// Wait a little before starting to read data from the stream.
setTimeout(function () {
stream.pipe(process.stdout)
}, 1000)API
fn = streamObserver(stream, callback)
Arguments:
stream- the readable stream you wish to observecallback- the callback will be attached to thedataevent of thestreamwithout triggering flowing mode
Returns a function that you can call if you want to observer to stop observing.
License
1.0.0
7 years ago
