Browse Source

stream: Remove unnecessary nextTick usage in Writable

Fix #4928
v0.9.12-release
isaacs 12 years ago
parent
commit
e0cec37d50
  1. 36
      lib/_stream_writable.js

36
lib/_stream_writable.js

@ -240,31 +240,35 @@ function onwrite(stream, er) {
if (er)
onwriteError(stream, state, sync, er, cb);
else {
if (!finishMaybe(stream, state)) {
if (state.length === 0 && state.needDrain)
onwriteDrain(stream, state);
var finished = finishMaybe(stream, state);
if (!state.bufferProcessing && state.buffer.length)
clearBuffer(stream, state);
}
if (!finished && !state.bufferProcessing && state.buffer.length)
clearBuffer(stream, state);
if (sync)
process.nextTick(cb);
else
cb();
if (sync) {
process.nextTick(function() {
afterWrite(stream, state, finished, cb);
});
} else {
afterWrite(stream, state, finished, cb);
}
}
}
function afterWrite(stream, state, finished, cb) {
if (!finished)
onwriteDrain(stream, state);
cb();
}
// Must force callback to be called on nextTick, so that we don't
// emit 'drain' before the write() consumer gets the 'false' return
// value, and has a chance to attach a 'drain' listener.
function onwriteDrain(stream, state) {
process.nextTick(function() {
if (state.needDrain) {
state.needDrain = false;
stream.emit('drain');
}
});
if (state.length === 0 && state.needDrain) {
state.needDrain = false;
stream.emit('drain');
}
}

Loading…
Cancel
Save