Skip to content

Instantly share code, notes, and snippets.

@almost
Created October 13, 2014 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almost/6a944e50221ed8881a58 to your computer and use it in GitHub Desktop.
Save almost/6a944e50221ed8881a58 to your computer and use it in GitHub Desktop.
var Readable = require('stream').Readable;
var util = require('util');
function Source(options, generator) {
if (typeof options === 'function') {
generator = options;
options = {};
}
this._generator = generator();
Readable.call(this, options);
}
util.inherits(Source, Readable);
Source.prototype._read = function () {
while (true) {
// console.log('GET NEXT');
var next = this._generator.next();
// console.log('NEXT WAS', next);
var shouldContinue = this.push(next.value);
if (next.done) {
this.push(null);
break;
} else if (!shouldContinue) {
break;
}
}
};
new Source(function* () {
yield "hello";
yield "word";
yield "boom";
}).pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment