From 907e39e5abc301fa54e144c5c151b79c3affb788 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 14 Apr 2016 21:22:04 -0700 Subject: wiring - move to obj-multiplex --- app/scripts/lib/obj-multiplex.js | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/scripts/lib/obj-multiplex.js (limited to 'app/scripts/lib/obj-multiplex.js') diff --git a/app/scripts/lib/obj-multiplex.js b/app/scripts/lib/obj-multiplex.js new file mode 100644 index 000000000..333b6061f --- /dev/null +++ b/app/scripts/lib/obj-multiplex.js @@ -0,0 +1,41 @@ +const through = require('through2') + +module.exports = ObjectMultiplex + + +function ObjectMultiplex(opts){ + opts = opts || {} + // create multiplexer + var mx = through.obj(function(chunk, enc, cb) { + var name = chunk.name + var data = chunk.data + var substream = mx.streams[name] + if (!substream) { + console.warn("orphaned data for stream " + name) + } else { + substream.push(data) + } + return cb() + }) + mx.streams = {} + // create substreams + mx.createStream = function(name) { + var substream = mx.streams[name] = through.obj(function(chunk, enc, cb) { + mx.push({ + name: name, + data: chunk, + }) + return cb() + }) + mx.on('end', function() { + return substream.emit('end') + }) + if (opts.error) { + mx.on('error', function() { + return substream.emit('error') + }) + } + return substream + } + return mx +} -- cgit