streams2: Writable only emit 'finish' once
authorisaacs <i@izs.me>
Wed, 12 Dec 2012 08:59:54 +0000 (00:59 -0800)
committerisaacs <i@izs.me>
Fri, 14 Dec 2012 18:52:28 +0000 (10:52 -0800)
lib/_stream_writable.js

index 00702ca..7364d3a 100644 (file)
@@ -190,7 +190,8 @@ function onwrite(stream, er) {
       cb();
   }
 
-  if (state.length === 0 && (state.ended || state.ending)) {
+  if (state.length === 0 && (state.ended || state.ending) &&
+      !state.finished && !state.finishing) {
     // emit 'finish' at the very end.
     state.finishing = true;
     stream.emit('finish');
@@ -245,7 +246,7 @@ Writable.prototype.end = function(chunk, encoding) {
   state.ending = true;
   if (chunk)
     this.write(chunk, encoding);
-  else if (state.length === 0) {
+  else if (state.length === 0 && !state.finishing && !state.finished) {
     state.finishing = true;
     this.emit('finish');
     state.finished = true;