From 6421c51a0e9a0ee58acb93739615836c8657c877 Mon Sep 17 00:00:00 2001 From: Tim Kuijsten Date: Thu, 10 Sep 2015 21:03:27 +0200 Subject: [PATCH 1/3] async: test if readable stream is non-blocking --- test/5.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/5.js diff --git a/test/5.js b/test/5.js new file mode 100644 index 0000000..7c27bc9 --- /dev/null +++ b/test/5.js @@ -0,0 +1,27 @@ +var test = require('tape') + , streamify = require('..') + , concat = require('concat-stream') +; + +test('async, non-blocking', function(t) { + var s = streamify(['1', '2', '3']); + + var i = 0; + s.on('data', function(item) { + i++; + }); + + process.nextTick(function() { + t.ok(i === 0 || i === 1, 'should have emitted one item at most in the first tick'); + process.nextTick(function() { + t.ok(i === 1 || i === 2, 'should have emitted one or two items in the second tick'); + process.nextTick(function() { + t.ok(i === 2 || i === 3, 'should have emitted two or three items in the thrid tick'); + process.nextTick(function() { + t.equal(3, i, 'should have emitted all three items in the fourth tick'); + t.end(); + }); + }); + }); + }); +}); From e76b88583bcdd1f52f2d104113604aca3a707d55 Mon Sep 17 00:00:00 2001 From: Tim Kuijsten Date: Thu, 10 Sep 2015 21:06:01 +0200 Subject: [PATCH 2/3] make readable stream non-blocking (async) --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6cc782e..d80c8ca 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,10 @@ function StreamArray(list) { StreamArray.prototype = Object.create(Readable.prototype, {constructor: {value: StreamArray}}); StreamArray.prototype._read = function(size) { - this.push(this._i < this._l ? this._list[this._i++] : null); + var self = this; + process.nextTick(function() { + self.push(self._i < self._l ? self._list[self._i++] : null); + }); }; module.exports = function(list) { From 46d4dfeacdb2f632a260e044a83f069ba3abc4d6 Mon Sep 17 00:00:00 2001 From: Tim Kuijsten Date: Thu, 10 Sep 2015 21:20:19 +0200 Subject: [PATCH 3/3] fix issue with covert --- test/5.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/5.js b/test/5.js index 7c27bc9..2947436 100644 --- a/test/5.js +++ b/test/5.js @@ -12,11 +12,11 @@ test('async, non-blocking', function(t) { }); process.nextTick(function() { - t.ok(i === 0 || i === 1, 'should have emitted one item at most in the first tick'); + t.equal(0, i, 'should emit the first item after this code'); process.nextTick(function() { - t.ok(i === 1 || i === 2, 'should have emitted one or two items in the second tick'); + t.equal(1, i, 'should emit the second item after this code'); process.nextTick(function() { - t.ok(i === 2 || i === 3, 'should have emitted two or three items in the thrid tick'); + t.equal(2, i, 'should emit the third item after this code'); process.nextTick(function() { t.equal(3, i, 'should have emitted all three items in the fourth tick'); t.end();