-
-
Save kevinsimper/49c164aa2ff861863430 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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