stream: Guarantee ordering of 'finish' event

In synchronous Writable streams (where the _write cb is called on the
current tick), the 'finish' event (and thus the end() callback) can in
some cases be called before all the write() callbacks are called.

Use a counter, and have stream.Transform rely on the 'prefinish' event
instead of the 'finish' event.

This has zero effect on most streams, but it corrects an edge case and
makes it perform more deterministically, which is a Good Thing.
This commit is contained in:
isaacs 2013-05-08 12:54:29 -07:00
parent 8a407f58b9
commit c38ce9bc0a
3 changed files with 53 additions and 8 deletions

View file

@ -125,7 +125,7 @@ function Transform(options) {
// sync guard flag.
this._readableState.sync = false;
this.once('finish', function() {
this.once('prefinish', function() {
if ('function' === typeof this._flush)
this._flush(function(er) {
done(stream, er);