Makes a new channel that receives the values put into
all the given channels (which is an array of channels).
The value produced by a merged channel is a wrapper object
that has three fields - “chan” giving the channel that
produced the value, “val” giving the value and “ix”
giving the index of the channel in the array that produced
the value. If any of the source channels callback with
an err (which is never supposed to happen) or “null” value
(which can happen), the channel will cease to send its output
to the merged channel.
Channel.merge = function (channels) {
var channel = new Channel();
function piper(ch, i) {
function writer(err, value) {
channel.put(new MergedChannelValue(i, ch, err, value), reader);
}
function reader(err, value) {
ch.take(writer);
}
reader(null, null);
}
channels.forEach(piper);
return channel;
};