react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / stream-combiner2 / index.js
80540 viewsvar duplexer = require('duplexer2')1var through = require('through2')23module.exports = function () {4var streams5if(arguments.length == 1 && Array.isArray(arguments[0])) {6streams = arguments[0]7} else {8streams = [].slice.call(arguments)9}10return combine(streams)11}1213module.exports.obj = function () {14var streams15if(arguments.length == 1 && Array.isArray(arguments[0])) {16streams = arguments[0]17} else {18streams = [].slice.call(arguments)19}20return combine(streams, { objectMode: true })21}222324function combine (streams, opts) {2526for (var i = 0; i < streams.length; i++)27streams[i] = wrap(streams[i], opts)2829if(streams.length == 0)30return through(opts)31else if(streams.length == 1)32return streams[0]3334var first = streams[0]35, last = streams[streams.length - 1]36, thepipe = duplexer(first, last)3738//pipe all the streams together3940function recurse (streams) {41if(streams.length < 2)42return43streams[0].pipe(streams[1])44recurse(streams.slice(1))45}4647recurse(streams)4849function onerror () {50var args = [].slice.call(arguments)51args.unshift('error')52thepipe.emit.apply(thepipe, args)53}5455//es.duplex already reemits the error from the first and last stream.56//add a listener for the inner streams in the pipeline.57for(var i = 1; i < streams.length - 1; i ++)58streams[i].on('error', onerror)5960return thepipe61}6263function wrap (tr, opts) {64if (typeof tr.read === 'function') return tr65if (!opts) opts = tr._options || {}66var input = through(opts), output = through(opts)67input.pipe(tr).pipe(output)68var dup = duplexer(input, output)69tr.on('error', function (err) { dup.emit('error', err) });70return dup;71}727374