From e03bc472f0b4a50ede3a0e0be7de9e5d12bb85a9 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 22 Feb 2013 16:45:22 -0800 Subject: [PATCH] stream: Start out in sync=true state The Readable and Writable classes will nextTick certain things if in sync mode. The sync flag gets unset after a call to _read or _write. However, most of these behaviors should also be deferred until nextTick if no reads have been made (for example, the automatic '_read up to hwm' behavior on Readable.push(chunk)) Set the sync flag to true in the constructor, so that it will not trigger an immediate 'readable' event, call to _read, before the user has had a chance to set a _read method implementation. --- lib/_stream_readable.js | 8 +++++++- lib/_stream_transform.js | 5 +++++ lib/_stream_writable.js | 6 ++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 2d67bb22fe..a78cd40aab 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -51,7 +51,13 @@ function ReadableState(options, stream) { this.ended = false; this.endEmitted = false; this.reading = false; - this.sync = false; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, becuase any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + this.onread = function(er, data) { onread(stream, er, data); }; diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index b83fedb626..0ee5a5030e 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -125,6 +125,11 @@ function Transform(options) { // start out asking for a readable event once data is transformed. this._readableState.needReadable = true; + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + this.once('finish', function() { if ('function' === typeof this._flush) this._flush(ts.output, function(er) { diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 9fe4c85bd3..d3b5d7494d 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -73,8 +73,10 @@ function WritableState(options, stream) { this.writing = false; // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. - this.sync = false; + // or on a later tick. We set this to true at first, becuase any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; // a flag to know if we're processing previously buffered items, which // may call the _write() callback in the same tick, so that we don't