Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont drain source while drain in progress #1

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ class PullDuplexStream extends Duplex {
}

drainPull() {
// Ensure misbehaving streams wait for a previous drain to finish before
// letting them pull again.
if (this.busy) {
return;
}
this.busy = true;

const self = this;

this.source(null, function next(end, data) {
self.busy = false;
if (end instanceof Error) {
return self.emit('error', end);
}
Expand All @@ -28,6 +36,7 @@ class PullDuplexStream extends Duplex {
}

if (self.push(data)) {
self.busy = true;
self.source(null, next);
}
});
Expand Down
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ test.cb('source pause/resume', (t) => {
readable.on('end', () => t.end());
});

test.cb('source ensure drain not called while drain in progress', (t) => {
const values = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const readable = toStream.readable(pull(
pull.values(values),
pull.asyncMap((value, cb) => {
setTimeout(() => {
// pass through value with delay
cb(null, value);
}, 10);
})
));

const timer = setInterval(() => {
readable.resume();
}, 5);

const output = [];

readable.on('data', (c) => {
output.push(c);
});

readable.once('end', () => {
clearInterval(timer);
t.deepEqual(output, values, 'End called after all values emitted');
t.end();
});
});

test.cb('sink basic', (t) => {
t.plan(1);

Expand Down