Skip to content

Instantly share code, notes, and snippets.

@kevinsimper
Forked from anonymous/gist:e64ac6f54c28b29d982e
Last active August 29, 2015 14:16
Show Gist options
  • Save kevinsimper/49c164aa2ff861863430 to your computer and use it in GitHub Desktop.
Save kevinsimper/49c164aa2ff861863430 to your computer and use it in GitHub Desktop.
var stream = require('stream');
var timer = new stream.Readable({
read: function() {
var self = this;
console.log('called');
setTimeout(function() {
if(!timer.isPaused()){
self.push('ping\n');
console.log('pushed');
}
}, 100);
}
});
timer.pipe(process.stdout);
timer.on('data', function(){
console.log('data', arguments)
})
timer.on('readable', function(){
console.log('readable', arguments)
})
timer.on('close', function(){
console.log('close', arguments)
})
timer.on('error', function(){
console.log('error', arguments)
})
timer.on('end', function(){
console.log('end', arguments)
})
setTimeout(function() {
timer.unpipe(process.stdout);
timer.pause()
}, 500);
$ node streamtest.js
called
ping
data { '0': <Buffer 70 69 6e 67 0a> }
called
pushed
ping
data { '0': <Buffer 70 69 6e 67 0a> }
called
pushed
ping
data { '0': <Buffer 70 69 6e 67 0a> }
called
pushed
ping
data { '0': <Buffer 70 69 6e 67 0a> }
called
pushed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment