Skip to content

Instantly share code, notes, and snippets.

@Floby
Last active August 29, 2015 14:14
Show Gist options
  • Save Floby/453805aca40e23900a0f to your computer and use it in GitHub Desktop.
Save Floby/453805aca40e23900a0f to your computer and use it in GitHub Desktop.
var util = require('util');
var stream = require('stream');
var Readable = stream.Readable;
var Writable = stream.Writable;
var map = require('through2-map');
util.inherits(Producer, Readable);
function Producer () {
if(!(this instanceof Producer)) return new Producer;
Readable.call(this, {objectMode: true});
var prev = 0;
var curr = 1;
this._read = function () {
var next = prev + curr;
this.push(curr);
prev = curr;
curr = next;
}
}
util.inherits(Consumer, Writable);
function Consumer (delay) {
if(!(this instanceof Consumer)) return new Consumer(delay);
Writable.call(this, {objectMode: true});
this._write = function (number, encoding, onWritten) {
setTimeout(function () {
console.log(number);
onWritten();
}, delay);
};
}
Producer()
.pipe(map.obj(function (value) { return value * 2 }))
.pipe(Consumer(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment