teambition / merge2

Merge multiple streams into one stream in sequence or parallel (~119M/month downloads).
MIT License
170 stars 14 forks source link

merge2 sometimes misses streams on('end') events #1

Closed mo22 closed 9 years ago

mo22 commented 9 years ago

in some scenarios, especially with gulp.src() resolving to an empty stream, merge2 misses the on('end') event.

The problem is solved by adding the on('end') immediately on addStream:

  function addStream() {
    for (var i=0; i<arguments.length; i++) {
        (function(stream) {
            function onend() {
                stream.removeListener('merge2UnpipeEnd', onend);
                stream.removeListener('end', onend);
                console.log('stream ended', stream._id);
                if (stream._merge2_onend) {
                    stream._merge2_onend();
                }
                stream._merge2_finished = true;
            }
            stream.on('merge2UnpipeEnd', onend);
            stream.on('end', onend);
        })(arguments[i]);
    }
...
    function pipe(stream) {
      if (stream._merge2_finished) {
        console.log('pipe', stream._id, 'already finished');
        next();
      } else {
        console.log('pipe', stream._id, 'go');
        stream._merge2_onend = function() {
          console.log('pipe', stream._id, 'end');
          next();
        }
        stream.pipe(mergedStream, {end: false});
      }
/*
      function onend() {
        stream.removeListener('merge2UnpipeEnd', onend);
        stream.removeListener('end', onend);
        next();
      }
      stream.on('merge2UnpipeEnd', onend);
      stream.on('end', onend);
      stream.pipe(mergedStream, {end: false});
*/
    }
island205 commented 9 years ago

+1

mo22 commented 9 years ago

found it that in my case it had a different issue and merge() work fine. sorry

zensh commented 9 years ago

@mo22 merge2 merge stream in sequence(one by one),I should pause streams waiting for pipe.