Skip to content

Commit

Permalink
Merge pull request #225 from ibash/fix_nil_and_async_consumer
Browse files Browse the repository at this point in the history
Don't pause consumer if the value is nil. Fixes #173.
  • Loading branch information
vqvu committed Feb 11, 2015
2 parents 1964531 + 0f81418 commit f23493f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,8 @@ Stream.prototype.consume = function (f) {
next_called = false;
f(err, x, push, next);
async = true;
if (!next_called) {
// Don't pause if x is nil -- as next will never be called after
if (!next_called && x !== nil) {
s.pause();
}
};
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,27 @@ exports['async consume'] = function (test) {
});
};

exports['consume - push nil async (issue #173)'] = function (test) {
test.expect(1);
_([1, 2, 3, 4]).consume(function(err, x, push, next) {
if (err !== null) {
push(err);
next();
}
else if (x === _.nil) {
_.setImmediate(push.bind(this, null, x));
}
else {
push(null, x);
next();
}
})
.toArray(function (xs) {
test.same(xs, [1, 2, 3, 4]);
test.done();
});
};

exports['passing Stream to constructor returns original'] = function (test) {
var s = _([1,2,3]);
test.strictEqual(s, _(s));
Expand Down

0 comments on commit f23493f

Please sign in to comment.