The Reactive Extensions (Rx) Library is a set of extensions for the IObservable and IObserver interfaces that greatly simplifies the orchestration and composition of asynchronous functions and events.
The Reactive Extensions (Rx) Library is a set of extensions for the IObservable and IObserver interfaces that were added in .NET 4.0. The extensions allow for the use of LINQ to query IObservable collections as if they were IEnumerable collections. In addition Rx also greatly simplifies the orchestration and composition of asynchronous functions and events. It is this last bit that is most interesting. Imagine being able to easily observe multiple data services and perform operations on that data in real time. Another likely scenario may be composing a set of events to capture gestures in real-time.Before we dive too deep into Rx, let’s go over the basics of the IObservable and IObserver push interfaces that Rx extends.

The IObservable interface requires the implementation of one method:

IDisposable Subscribe(IObserver<T> observer).

The IObserver interface requires the implementation of the following three methods:

void OnCompleted()
void OnError(Exception error)
void OnNext(T value).

An easy way to think of IObservable and IObserver is that they are the dual of IEnumerable and IEnumerator. Whereas IEnumerable and IEnumerator are used for pulling a data collection, IObservable and IObserver are used for pushing a data collection.

A class that implements IObservable acts a collection of data that can be observed. A class that implements IObserver can subscribe to an observable collection and responds to the OnNext, OnError and OnCompleted events. The OnNext event is fired when more data is available, OnError if an exception occurs, and OnCompleted when there is no additional data.

Setting up Rx

The first thing you’ll need to do is download the Reactive Extensions from Microsoft. You can also obtain Rx through NuGet. Once you have Rx downloaded and installed, add a reference to System.Reactive and WindowsBase to your project and import the System, System.Linq and System.Reactive.Linq namespaces in the class where you’ll be using Rx.

Observing a Data Collection

Rx includes a handy ToObservable extension method onto IEnumerable that converts a typed collection to a typed Observable collection. This function is particularly useful for observing a fixed chunk of data.

First we create a standard collection to store our set of data to publish, in this case the first 1,000 digits in the Fibonacci sequence. The Fibonacci sequence is defined as 0, 1, and f(n-2) + f(n-1) for each subsequent digit.

List<ulong> fibonacciSeq = GenerateFibonnaci(1000);

Next convert the collection to an observable collection using ToObservable().

IObservable<int> fibonacciSource = fibonacciSeq.ToObservable();

Once the observable collection has been created, we can subscribe to the collection and perform a meaningful task. Rx adds several Subscribe extension methods onto IObservable that allow you to specify Action methods for each of the IObserver events.

Console.WriteLine("Press enter to subscribe");
Console.ReadLine();

 using (var fibonnacciSub = fibonacciSource.SubscribeOn(Scheduler.TaskPool)
     .Subscribe(
     (x) => Console.WriteLine(x),
     (Exception ex) => Console.WriteLine("Error received from source: {0}.", 	       ex.Message),
     () => Console.WriteLine("End of sequence.")
     )
 )
 {
     Console.WriteLine("Press enter again to unsubscribe from the service.");
     Console.ReadLine();
 }

Lastly add the GenerateFibonacci function that creates a finite Fibonacci sequence.

static List<ulong> GenerateFibonnaci(int sequenceLength)
{
     List<ulong> sequence = new List<ulong>();

     for (int i = 0; i < sequenceLength; i++)
     {
         if (i <= 1)
             sequence.Add((ulong)i);
         else
             sequence.Add(sequence[i - 2] + sequence[i - 1]);
     }

     return sequence;
 }

#reactive #java #code #visual-studio

Reactive Extensions: Just What the Doctor Ordered (Part 1)
1.20 GEEK