Skip to content

Day 09 Rx.Net part 3

Kobi Hari edited this page Nov 10, 2020 · 2 revisions

Day 09 - Reactive X - Part 3

Projects:

Advanced Rx.Net Advanced Rx operators and examples

Side Effects, Logging and Debugging

  • We used the The Do Operator to create log and allow debugging
    • We noticed that Do does not actually do anything until the observable is subscribed to
    • We understood the difference between Do and Subscribe

Observable Temperature

  • We used subjects as HUB and created a demo where the subject turns a cold observable to hot
  • We distinctify between the two roles that the subject serves when used as a hub
    • It publishes the same events to all observers
    • It "activates" the source by subscribing to it
  • We got familiar with the Publish operator which serves like a Subject
    • Publish turns a cold observable to hot
    • The actual subscription is deffered until the Connect method is called
    • You can use Publish(initialValue) to create a BehaviorPublisher that behaves like a BehaviorSubject
    • You can disconnect using the IDisposable returned from the Connect method
    • You can use PublishLast to create a publisher that acts like AsyncSubject
  • We used the .Publish().Refcount() to create a publisher that automatically connects to source when the first observer subscribes to it
  • We used the Multicast operator that is similar to Publish except that we can provide our own subject to be used as hub.
  • We turned a Hot Observable into Cold by using the Replay Operator

Observable Aggregation

  • We saw how to use Sum, Count, Max, ToList - All reactive!!!
  • We saw that they return a Scalar Observable - an observable that returns a single value
  • We learned how to use Aggregate to create a general aggregator
  • We saw the Scan Operator and realized it is just like Aggregate except it returns sub results after each source event.

Creating 2'nd order observables

  • We used the Select Operator to create an Observable of Observables
  • We saw that we can also use GroupBy and Window to transfer a flat observable into second order one.
  • We saw that Buffer is similiar to Window except that it returns IObservable<IEnumerable<T>>

Flattening 2'nd order observables

  • We used Merge to collect all events from all observables into a single observable
  • We used Switch to collect events from the latest observable while switching between them
  • We used Concat to collect events sequentially from one observable after another.

Partitioning and Combining

  • We saw the The Zip Operator that combines 2 (or more) observables by matching events with the same index and combining them togher using a projection method
  • We saw the CombineLatest Operator and realized it is like Zip except that it yields a combined event whenever either operand yields an event.
  • We saw the WithLatestFrom Operator which is like CombineLatest but not symmetrical, so it only yields events when the first observable yields them.

Stock Market Example

  • We have created an example to using Rx, where we get a stream of "stock market values" for a collection of stocks and we need to notify when one stock starts to change drastically
  • We have converted the initial notifications (of collection of stocks) to notifications of single stock value using SelectMany
  • We have grouped notifications of the same stocks using GroupBy
  • We then filtered each "stock observable" using DistinctUntilChanged so that we only see notification when each stock actually changes
  • We used Buffer to accumulate each notification with the previous notification of the same stock and then filtered out notifications where the change was not large enough