In a first blog post on the topic “event composition with Rx” I presented operators that take several event input streams and create a single output streams, whose elements contain values from each input event (“multi-valued” event composition operators). This second blog post instead treats “single-valued” event composition. Multiple streams go into those composition operators and one single stream comes out, where each element of the output stream matches exactly one event/value from any input stream!
Example solution, PPT slides and general information
In general, all introductory information on composition, description of the “multi-valued” event composition operators and more bits-and-pieces can be found in my first blog post.
Merge does serialization of all input event streams by firing an event everytime one of the input streams fires. The event/value of the output stream will be the same as the event/value on the input stream that has fired.
The following code shows the simple usage of
var composition = Observable.Merge(stream1, stream2, stream3);
And the following example flow diagram shows the behavior of the operator under this code. Note how the incoming events are written to the output stream in exactly the same order as they come in:
Amb comes from ambiguous and has the following semantics. If you combine several input event streams with
Amb, the output stream will only contain the events from the input stream that fires first. The other input streams are not taken into account afterwards. That’s a kind of winner-takes-all-strategy and could be valuable in some situations.
Amb can be used as follows:
var composition = stream1.Amb(stream2).Amb(stream3);
And the following diagram shows an example composition situation. Note that stream 1 fires first and consequently
Amb only forwards events from this stream:
Concat does (the name says it all) concatenation of the input event streams. But wait: how can this work? In fact
Concat only works when you have finite event streams! On infinite streams,
Concat watches the very first stream forever and the other streams don’t come to their turn. Thus use
Concat on finite event streams, when serialization of the input streams in the right order is crucial.
The following code shows the use of
Concat on three finite input streams:
var composition = stream1.Concat(stream2).Concat(stream3);
And here’s the concatenation done by this code in an example situation (the square brackets illustrate finite streams):
The operator name says it all:
Until forwards events from an input stream to the output stream until an event on a second stream occurs.
The following code shows the usage:
var composition = stream1.Until(stream2);
And the following diagram shows the behavior of
Until on 2 streams based on this code:
Again the operator name speaks for itself:
WaitUntil does not forward events from an input stream to an output stream until an event on a second stream occurs. From then on every event of the first input stream will be forwarded.
Here’s some code that shows how
var composition = stream1.WaitUntil(stream2);
And the following diagram shows
WaitUntil‘s behavior based on this code in an example situation:
That’s it again. I hope you finde this information useful. As in the blog post on multi-valued event composition, I want to encourage you to give me active feedback and to improve the examples and descriptions of the composition operators above. Thanks!