Introduction to Reactive Extensions
Upcoming SlideShare
Loading in...5

Introduction to Reactive Extensions






Total Views
Views on SlideShare
Embed Views



1 Embed 1 1


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Who am I?Rx was developed by the Cloud Programmability Team
  • - Composing gives us a hint at the Linq style fluent API
  • Async and event-based programs. Recent initiatives including TPL, async/await etc
  • You can’t pass an event aroundThe syntax is very unique and requires cleaning up of subscriptionsYou can’t compose events into new events easily.
  • Lets look at an ordinary collection in .NetNotice in IEnumerable we FETCH an EnumeratorMoveNext pulls the next value from the collection synchronously into Current. It could have just as easily returned the current value from MoveNextReset is an oddity of historyWe are done when there are no more items and MoveNext returns true
  • Observables turn the whole thing on it’s headNotice in Iobservable we get FED an ObserverOnNext pushed the next value from onto the collection asynchronously. OnError occurs when an exception has happened.OnCompleted happens when there are no more items
  • Observable.Return(42).Subscribe(Console.WriteLine)Observable.Range(0,20). Subscribe(Console.WriteLine)Observable.Generate( 0,i => i < 20, i => i+1, i => i*i) .Subscribe(Console.WriteLine);Observable.Generate( 0,i => i < 20, i => i+1, i => i*i) .Subscribe(Console.WriteLine);var sub = Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(Console.WriteLine);var sub = Observable.Interval(TimeSpan.FromSeconds(1)).Take(3).Subscribe(Console.WriteLine);Observable.Interval(TimeSpan.FromSeconds(1)).Take(3).Subscribe(Console.WriteLine, e => { }, () => Console.WriteLine("Complete"));
  • Observable.Range(0, 20).Where(x => x % 2 ==0).Subscribe(Console.WriteLine)Observable.Range(0, 20).Where(x => x % 2 ==0).Select(x => x*x).Subscribe(Console.WriteLine)Observable.Range(0, 20).Zip(Observable.Interval(TimeSpan.FromSeconds(1)), (x,y) => x).Where(x => x % 2 == 0).Subscribe(Console.WriteLine);
  • From Blank AutoCompleteShow project structureCreate search based off property changed event handler** How would we do this with Rx?**Create propertyChanges, searchTextChanges and subscribeRun** What about blocking the UI thread?**Add doSearch, select many and subscriptionRun ** What about the exception?**ObserveOnDispatcherRun** What about too many searches?**Throttle** What about adding support for pressing enter?**TextBoxEnterCommand (Merge)** What about the duplicates?**DistinctUntilChanged** What about results coming back out of sequence?** (Temporarily remove throttle to show)TakeUntil
  • Show codeRunIncrease throttle time

Introduction to Reactive Extensions Introduction to Reactive Extensions Presentation Transcript

  • 1 Introduction to Reactive ExtensionsPeter GoodmanAn Introduction to ReactiveExtensions
  • 2 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
  • 3 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
  • 4 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
  • 5 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
  • 6 Introduction to Reactive ExtensionsEvents in .Netform1.MouseMove += (sender, args) => { if (args.Location.X == args.Location.Y) // I’d like to raise another event};form1.MouseMove -= /* what goes here? */
  • 7 Introduction to Reactive ExtensionsCollections are Enumerablesinterface IEnumerable<out T>{ IEnumerator<T> GetEnumerator();}interface IEnumerator<out T> : IDisposable{ bool MoveNext(); T Current { get; } void Reset();}
  • 8 Introduction to Reactive ExtensionsWhat if events were collections?  CollectionMove Next Move Next Move Next Move Next Move Next Move Next  Event Stream TIMEOnNext OnNext OnNext OnNext OnNext OnNext
  • 9 Introduction to Reactive ExtensionsObservablesinterface IObservable<out T>{ IDisposable Subscribe(IObserver<T> observer);}interface IObserver<in T>{ void OnNext(T value); void OnError(Exception ex); void OnCompleted();}
  • 10 Introduction to Reactive Extensions Summary Push vs Pull ApplicationInteractive MoveNext Reactive Got next? OnNext Have next! IEnumerable<T> IObservable<T> IEnumerator<T> IObserver<T> Environment
  • 11 Introduction to Reactive ExtensionsCreating Observables - Primitive OnCompleted .Empty<int>() new int[0] OnNext .Return(42) new[] { 42 } OnError .Throw<int>(ex) Throwing iterator .Never<int>() Iterator that got stuck Notion of time
  • 12 Introduction to Reactive ExtensionsCreating Observables - Range OnNext(0) OnNext(1) OnNext(2) .Range(0, 3) yield 0 yield 1 yield 2 .Range(0, 3)
  • 13 Introduction to Reactive Extensions Generating values and Subscribing A variant with time notion Hypothetical anonymous exists (GenerateWithTime) iterator syntax in C#o = Observable.Generate( e = new IEnumerable<int> { 0, for (int i = 0; i => i < 10, i < 10; i => i + 1, i++) i => i * i yield return i * i;); }; Asynchronous Synchronouso.Subscribe(x => { foreach (var x in e) { Console.WriteLine(x); Console.WriteLine(x);}); }
  • 14 Introduction to Reactive ExtensionsSubscribingIObservable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted();});IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); });
  • 15 Introduction to Reactive ExtensionsDEMOGenerating events and subscribing to them
  • 16 Introduction to Reactive ExtensionsObservable Querying Observables are sources of data  Data is sent to you (push based)  Extra (optional) notion of time. Hence we can query over them// Producing an IObservable<Point> using Selectvar from in Observable MouseEventArgs select .Location// Filtering for the first bisector using Wherevar from in where select
  • 17 Introduction to Reactive ExtensionsDEMOQuerying, Composition and introducing Concurrency
  • 18 Introduction to Reactive ExtensionsIntroducing Asynchrony An Asynchronous operation can be thought of as an Observable that returns a single value and completes. FromAsync  Takes an APM method pair (BeginExecute, EndExecute) and creates an Observable ToAsync  Takes a method and creates an Observable (Like TPL)
  • 19 Introduction to Reactive ExtensionsIntroducing Concurrency Many operators in Rx introduce Concurrency  Throttle  Interval  Delay  BufferWithTime Generally they provide an overload to supply a Scheduler  ImmediateScheduler – Static Immediate  CurrentThreadScheduler – Placed on a queue for the current thread  NewThreadScheduler – Spawn a new Thread  DispatcherScheduler - Silverlight  TaskPoolScheduler - TPL  ThreadPoolScheduler
  • 20 Introduction to Reactive Extensions Concurrency and Synchronization• var Observable.Return Scheduler.ThreadPool " "• .ObserveOnDispatcher() " "
  • 21 Introduction to Reactive ExtensionsDEMOSynchronization
  • 22 Introduction to Reactive ExtensionsRx for JavaScript (RxJS) Parity with Rx for .NET  Set of operators  Taming asynchronous JS JavaScript-specific bindings  jQuery  ExtJS  Dojo  Prototype  YUI3  MooTools  Bing APIs
  • 23 Introduction to Reactive ExtensionsDEMORx for JavaScript
  • 24 Introduction to Reactive ExtensionsReactive ExtensionsQuestions?
  • 25 Introduction to Reactive ExtensionsResources MSDN Bart de Smet / Rx (Channel 9) Reactive UI Pushqa / SignalR
  • 26 Introduction to Reactive ExtensionsContact Twitter: @petegoo