Function switcher

Switcher generates several output streams, labelled according to the values of cases. Values from source are fed to the output streams if their associated predicate function returns true.

In this way, we can split one input stream into several output streams, each potentially getting a different subset of the input.

With options, you can specify whether to send to multiple outputs if several match, or just the first (default behaviour).

The below example shows setting up a switcher and consuming the output streams.

// Initialise a reactive number, starting at 0
const switcherSource = Reactive.number(0);
// Set up the switcher
const x = Reactive.switcher(switcherSource, {
even: v => v % 2 === 0,
odd: v => v % 2 !== 0
});
// Listen for outputs from each of the resulting streams
x.even.on(msg => {
log(`even: ${msg.value}`);
});
x.odd.on(msg => {
log(`odd: ${msg.value}`);
})
// Set new values to the number source, counting upwards
// ...this will in turn trigger the outputs above
setInterval(() => {
switcherSource.set(switcherSource.last() + 1);
}, 1000);

If source closes, all the output streams will be closed as well.