{"id":584,"date":"2009-11-28T15:25:08","date_gmt":"2009-11-28T14:25:08","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=584"},"modified":"2010-07-10T10:21:02","modified_gmt":"2010-07-10T09:21:02","slug":"rx-event-composition-single-valued","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/rx-event-composition-single-valued","title":{"rendered":"Rx: Event composition &#8211; single-valued"},"content":{"rendered":"<p>In a <a href=\"http:\/\/www.minddriven.de\/?p=563\">first blog post<\/a> on the topic &#8222;event composition with Rx&#8220; I presented operators that take several event input streams and create a single output streams, whose elements contain values from each input event (&#8222;<a href=\"http:\/\/www.minddriven.de\/?p=563\">multi-valued<\/a>&#8220; event composition operators). This second blog post instead treats \u201csingle-valued\u201d 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 <em>any<\/em> input stream!<\/p>\n<h3>Example solution, PPT slides and general information<\/h3>\n<p>In general, all introductory information on composition, description of the &#8222;multi-valued&#8220; event composition operators and more bits-and-pieces can be found in my <a href=\"http:\/\/www.minddriven.de\/?p=563\">first blog post<\/a>.<\/p>\n<p>I&#8217;ve created a SL3 demo project where you can experiment with each composition operator. You can find the <strong>sourcecode<\/strong> (SL3 VS2008 project) <a href=\"http:\/\/www.minddriven.de\/stuff\/RxEventComposition.zip\">here<\/a> and the <strong>live demo on Windows Azure<\/strong> <a href=\"http:\/\/rxcomposition.cloudapp.net\/\">here<\/a>.<\/p>\n<p>The PPT slides that contain all diagrams of the composition operators can be downloaded <a href=\"http:\/\/www.minddriven.de\/stuff\/RxEventComposition.ppt\">here<\/a>.<br \/>\nI would be glad if you could leave my name and\/or the blog URL (<a href=\"..\/\">www.minddriven.de<\/a>) on every diagram.<\/p>\n<h3>Merge()<\/h3>\n<p>The operator <code>Merge<\/code> 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.<\/p>\n<p>The following code shows the simple usage of <code>Merge<\/code>:<\/p>\n<pre class=\"brush:csharp\">\r\n    var composition = \r\n        Observable.Merge(stream1, stream2, stream3);\r\n<\/pre>\n<p>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:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-587\" title=\"Rx Event Composition - Merge\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Merge.png\" alt=\"Rx Event Composition - Merge\" width=\"650\" height=\"228\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Merge.png 650w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Merge-300x105.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<h3>Amb()<\/h3>\n<p><code>Amb<\/code> comes from <em>ambiguous<\/em> and has the following semantics. If you combine several input event streams with <code>Amb<\/code>, 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&#8217;s a kind of <em>winner-takes-all<\/em>-strategy and could be valuable in some situations.<\/p>\n<p><code>Amb<\/code> can be used as follows:<\/p>\n<pre class=\"brush:csharp\">\r\n    var composition = \r\n       stream1.Amb(stream2).Amb(stream3);\r\n<\/pre>\n<p>And the following diagram shows an example composition situation. Note that stream 1 fires first and consequently <code>Amb<\/code> only forwards events from this stream:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-588\" title=\"Rx Event Composition - Amb\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Amb.png\" alt=\"Rx Event Composition - Amb\" width=\"600\" height=\"244\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Amb.png 600w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Amb-300x122.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h3>Concat()<\/h3>\n<p><code>Concat<\/code> does (the name says it all) <em>concatenation<\/em> of the input event streams. But wait: how can this work? In fact <code>Concat<\/code> <em>only<\/em> works when you have <em>finite<\/em> event streams! On <em>infinite<\/em> streams, <code>Concat<\/code> watches the very first stream forever and the other streams don&#8217;t come to their turn. Thus use <code>Concat<\/code> on finite event streams, when serialization of the input streams in the right order is crucial.<\/p>\n<p>The following code shows the use of <code>Concat<\/code> on three finite input streams:<\/p>\n<pre class=\"brush:csharp\">\r\n    var composition = \r\n        stream1.Concat(stream2).Concat(stream3);\r\n<\/pre>\n<p>And here&#8217;s the concatenation done by this code in an example situation (the square brackets illustrate finite streams):<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-590\" title=\"Rx Event Composition - Concat\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Concat.png\" alt=\"Rx Event Composition - Concat\" width=\"695\" height=\"244\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Concat.png 695w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Concat-300x105.png 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n<h3>Until()<\/h3>\n<p>The operator name says it all: <code>Until<\/code> forwards events from an input stream to the output stream <em>until<\/em> an event on a second stream occurs.<\/p>\n<p>The following code shows the usage:<\/p>\n<pre class=\"brush:csharp\">\r\n    var composition = \r\n        stream1.Until(stream2);\r\n<\/pre>\n<p>And the following diagram shows the behavior of <code>Until<\/code> on 2 streams based on this code:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-591\" title=\"Rx Event Composition - Until\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Until.png\" alt=\"Rx Event Composition - Until\" width=\"625\" height=\"173\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Until.png 625w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_Until-300x83.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/p>\n<h3>WaitUntil()<\/h3>\n<p>Again the operator name speaks for itself: <code>WaitUntil<\/code> does <em>not<\/em> forward events from an input stream to an output stream <em>until<\/em> an event on a second stream occurs. From then on every event of the first input stream will be forwarded.<\/p>\n<p>Here&#8217;s some code that shows how <code>WaitUntil<\/code> works:<\/p>\n<pre class=\"brush:csharp\">\r\n    var composition = \r\n        stream1.WaitUntil(stream2);\r\n<\/pre>\n<p>And the following diagram shows <code>WaitUntil<\/code>&#8217;s behavior based on this code in an example situation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-592\" title=\"Rx Event Composition - WaitUntil\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_WaitUntil.png\" alt=\"Rx Event Composition - WaitUntil\" width=\"625\" height=\"214\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_WaitUntil.png 625w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/11\/Rx_Composition_WaitUntil-300x102.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>That&#8217;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!<\/p>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.minddriven.de%2f%3fp%3d584\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.minddriven.de%2f%3fp%3d584\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a first blog post on the topic &#8222;event composition with Rx&#8220; I presented operators that take several event input streams and create a single output streams, whose elements contain values from each input event (&#8222;multi-valued&#8220; event composition operators). This second blog post instead treats \u201csingle-valued\u201d event composition. Multiple streams go into those composition operators &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/rx-event-composition-single-valued\" class=\"more-link\"><span class=\"screen-reader-text\">Rx: Event composition &#8211; single-valued<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[198,197,181,195,179,178],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-dot-net","tag-combining","tag-composition","tag-events","tag-reactive-extensions-for-net","tag-reactive-framework","tag-rx"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":8,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":930,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/584\/revisions\/930"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}