Rx - for beginners (part 4): anonymous observer handler
Rx - for beginners (part 4): anonymous observer handler
this post is the 4th in a series of posts about the new Reactive Framework (Rx).
the series TOC can found here.
this post will focus on anonymous observer handler.
the code sample for this post can be download from here.
Anonymous Observer
as we mention on the previous posts, IObserver is used as a callback interface
which can be subscribe to the IObservable,
but this is a bit of overkill, when what we subscribe is relatively small (like Console.Write).
wouldn't it be nice to use Lamda expression instead?
doesn't we want to have something similar to button1.Click += (s,e)=>Console.WriteLine(…); .
IObservable extensions
using Lamda expression syntax in order to subscribe delegate into the IObservable is
available because IObservable got the following extension options for its Subscribe method
(under the hood we are speaking about extension methods).
- Action<TSource> onNext
- Action<TSource> onNext, Action onCompleted
- Action<TSource> onNext, Action<Exception> onError
- Action<TSource> onNext, Action<Exception> onError, Action onCompleted
so assuming we got IObservable instance called foo, the following code is perfectly OK:
- IDisposable unsubscribe = foo.Subscribe(value => Console.WriteLine(value));
for simple subscription only for the on next callback.
subscribing both for on next and on error will look as follow:
- IDisposable unsubscribe = foo.Subscribe(
- value => Console.WriteLine(value),
- exc => Console.WriteLine(exc));
what is actually happens is that under the hood the subscribe method creating
instance of IObserver, attaching the delegate, and hand it to the IObservable subscribe method.
Code Sample
I took the previous post's sample, which look as follow:
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- var obs = new Observer();
- var observable = new FakeObservableFeeder();
- IDisposable unsubscribe = observable.Subscribe(obs);
- Console.ReadKey();
- unsubscribe.Dispose();
- }
- private class Observer : IObserver<int>
- {
- void IObserver<int>.OnCompleted()
- {
- Console.WriteLine("Done");
- }
- void IObserver<int>.OnError(Exception exception)
- {
- Console.WriteLine("Error: " + exception.Message);
- }
- void IObserver<int>.OnNext(int value)
- {
- Console.WriteLine(value);
- }
- }
- }
you can see at line 11, that we implementing IObserver with code one line
for each methods (Console.WriteLine ).
and at line 7, we subscribing this observer into the IObservable instance.
Code Sample using anonymous observer
this can be match simpler using anonymous observer, as shown in the following snippet:
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- var observable = new FakeObservableFeeder();
- // subscribing handlers (Anonymous observer)
- IDisposable unsubscribe = observable.Subscribe(
- value => Console.WriteLine(value),
- exc => Console.WriteLine(exc),
- () => Console.WriteLine("Done"));
- Console.ReadKey();
- unsubscribe.Dispose();
- }
- }
definitely less code was written.
you can see the subscription at line 7.
- line 8, is handling the on next action.
- line 9, is handling the on error action.
- line 10, is handling the on complete action.
Summary
what we have seen is that simple action can be subscribed using anonymous observer handlers.
תגים של Technorati:
IObservable,
IObserver,
Rx