streams2: Fix duplex no-half-open logic
authorisaacs <i@izs.me>
Tue, 13 Nov 2012 07:28:56 +0000 (23:28 -0800)
committerisaacs <i@izs.me>
Fri, 14 Dec 2012 01:00:29 +0000 (17:00 -0800)
lib/_stream_duplex.js

index 30b4692..ef74c34 100644 (file)
@@ -47,38 +47,17 @@ function Duplex(options) {
   if (options && options.allowHalfOpen === false)
     this.allowHalfOpen = false;
 
-  this.once('finish', onfinish);
   this.once('end', onend);
 }
 
-// the no-half-open enforcers.
-function onfinish() {
-  // if we allow half-open state, or if the readable side ended,
-  // then we're ok.
-  if (this.allowHalfOpen || this._readableState.ended)
-    return;
-
-  // mark that we're done.
-  this._readableState.ended = true;
-
-  // tell the user
-  if (this._readableState.length === 0)
-    this.emit('end');
-  else
-    this.emit('readable');
-}
-
+// the no-half-open enforcer
 function onend() {
   // if we allow half-open state, or if the writable side ended,
   // then we're ok.
   if (this.allowHalfOpen || this._writableState.ended)
     return;
 
-  // just in case the user is about to call write() again.
-  this.write = function() {
-    return false;
-  };
-
   // no more data can be written.
-  this.end();
+  // But allow more writes to happen in this tick.
+  process.nextTick(this.end.bind(this));
 }