Created
October 13, 2014 13:56
-
-
Save almost/6a944e50221ed8881a58 to your computer and use it in GitHub Desktop.
This file contains 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 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