Skip to content

Instantly share code, notes, and snippets.

@isaacs isaacs/p.js
Created Feb 22, 2013

Embed
What would you like to do?
var assert = require('assert');
var stream = require('stream');
var r = new stream.Readable({ encoding: 'ascii' });
// at the first read, no data is available yet.
var s = [['','',''],['a','b','c'],['d','e','f'],['g','h','i']];
r._read = function(n, cb) {
console.error('_READ', s[0] ? s[0].join('') : 'EOF');
var l = s.shift();
if (l) {
r.push(l[0]);
r.push(l[1]);
r.push(l[2]);
} else {
cb(null, null);
}
};
var ended = false;
r.on('end', function() {
ended = true;
});
assert.equal(r.read(), null);
assert.equal(r.read(), 'abc');
r.push('1');
r.push('2');
r.push('3');
assert.equal(r.read(), '123def');
assert.equal(r.read(), 'ghi');
assert.equal(r.read(), null);
process.on('exit', function() {
assert(ended);
console.log('ok');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.