Switch
Rx.switcher generates several independent output streams. 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.
The below example shows setting up a switcher and consuming the output streams. Changes are directed to one of two output reactives based on whether the number is odd or even.
rxS
will be an object consisting of several reactives:
Note the reactives are labelled by the property key. These labels are whatever is used when providing the predicates to Rx.switcher
.
We can subscribe to each reactive, or do whatever else:
If we call switcherSource.set()
with an odd number, this will be routed to a reactive labelled odd
, or otherwise it will be routed to the even
reactive.
By default the switcher only sends values to the first matching predicate. We can provide match: all
as an option to send values to all matching predicates.
Example
Here’s a more practical example of switcher
. In this case, detecting single or double-clicks.