You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get an event when you're being sent data or asked for it.
About
This is just a simple thing that tells you when _read and _write have been called, saving you the trouble of writing this yourself. You receive two events reading and writing-- no magic is performed.
This works well with duplexify or lazy streams, so you can wait until you know you're being used as a stream to do something asynchronous, such as fetching an API token.
Use
$ npm install --save stream-events
varstream=require('stream')varstreamEvents=require('stream-events')varutil=require('util')functionMyStream(){stream.Duplex.call(this)streamEvents.call(this)}util.inherits(MyStream,stream.Duplex)MyStream.prototype._read=function(chunk){console.log('_read called as usual')this.push(newBuffer(chunk))this.push(null)}MyStream.prototype._write=function(){console.log('_write called as usual')}varstream=newMyStreamstream.on('reading',function(){console.log('stream is being asked for data')})stream.on('writing',function(){console.log('stream is being sent data')})stream.pipe(stream)
Using with Duplexify
varduplexify=require('duplexify')varstreamEvents=require('stream-events')varfs=require('fs')vardup=streamEvents(duplexify())dup.on('writing',function(){// do something asyncdup.setWritable(/*writable stream*/)})fs.createReadStream('file').pipe(dup)
About
Get an event when you're being sent data or asked for it.