grncdr / merge-stream

Merge multiple streams into one interleaved stream
MIT License
214 stars 16 forks source link

How should I use the merge-stream if the second stream has a dependence with the first one? #29

Closed NextSeason closed 5 years ago

NextSeason commented 7 years ago

My requirement is that I want to replace some strings with texts which I will read them from some files, but before doing this, I need to do some changes to the files which will be read and inject into other files. So I want to merge the two streams together and the second one has to wait for the finish of the first one.

What shall I do?

stevemao commented 7 years ago

Same as https://github.com/grncdr/merge-stream/issues/28?

alexanderby commented 7 years ago

I've tried to use several packages for ordering streams, but none worked for me. I've tried to modify merge-stream but failed. The problem is that some streams participate in multiple merged streams (e.g. the same stream is used for creating a file and passing that file into concatenation stream).

const PassThrough = require('readable-stream/passthrough');
const merge = function (/*streams...*/) {
  var sources = []
  var output  = new PassThrough({objectMode: true})

  output.setMaxListeners(0)

  output.add = add
  output.isEmpty = isEmpty

  output.on('unpipe', end)

  Array.prototype.slice.call(arguments).forEach(add)

  output.pause()
  next()

  return output

  function next () {
    if (isEmpty()) {
      end()
      return
    }

    var source = sources.shift()
    source.once('end', next.bind(null))
    source.once('error', output.emit.bind(output, 'error'))
    source.pipe(output, {end: false})
    source.resume()
  }

  function add (source) {
    if (Array.isArray(source)) {
      source.forEach(add)
      return this
    }

    source.pause()
    sources.push(source)
    return this
  }

  function isEmpty () {
    return sources.length == 0;
  }

  function end (source) {
    sources.splice(0)
    if (output.readable) { output.end() }
  }
}

@NextSeason This may work in your case. If nobody see an error in my code, I will have to follow the Grunt way, create files with one task and after that concatenate them with another task.

stevemao commented 5 years ago

Still waiting for @NextSeason to respond. Comment and we can reopen this.