Your SlideShare is downloading. ×
0
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) =...
7                                      Introduction to Reactive ExtensionsCollections are Enumerablesinterface IEnumerable...
8                                                            Introduction to Reactive ExtensionsWhat if events were collec...
9                                    Introduction to Reactive ExtensionsObservablesinterface IObservable<out T>{    IDispo...
10                                                                   Introduction to Reactive Extensions              Summ...
11                                    Introduction to Reactive ExtensionsCreating Observables - Primitive                 ...
12                                Introduction to Reactive ExtensionsCreating Observables - Range              OnNext(0)  ...
13                                        Introduction to Reactive Extensions Generating values and Subscribing   A varian...
14                                                Introduction to Reactive ExtensionsSubscribingIObservable<int> o = Obser...
15                                       Introduction to Reactive ExtensionsDEMOGenerating events and subscribing to them
16                                               Introduction to Reactive ExtensionsObservable Querying Observables are s...
17                                      Introduction to Reactive ExtensionsDEMOQuerying, Composition and introducing Concu...
18                                              Introduction to Reactive ExtensionsIntroducing Asynchrony An Asynchronous...
19                                                   Introduction to Reactive ExtensionsIntroducing Concurrency Many oper...
20                                            Introduction to Reactive Extensions    Concurrency and Synchronization•    v...
21                  Introduction to Reactive ExtensionsDEMOSynchronization
22                                 Introduction to Reactive ExtensionsRx for JavaScript (RxJS) Parity with Rx for .NET  ...
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) Rea...
26                            Introduction to Reactive ExtensionsContact pete@petegoo.com http://blog.petegoo.com Twitt...
Upcoming SlideShare
Loading in...5
×

Introduction to Reactive Extensions

764

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
764
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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 =&gt; i &lt; 20, i =&gt; i+1, i =&gt; i*i) .Subscribe(Console.WriteLine);Observable.Generate( 0,i =&gt; i &lt; 20, i =&gt; i+1, i =&gt; 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 =&gt; { }, () =&gt; Console.WriteLine(&quot;Complete&quot;));
  • Observable.Range(0, 20).Where(x =&gt; x % 2 ==0).Subscribe(Console.WriteLine)Observable.Range(0, 20).Where(x =&gt; x % 2 ==0).Select(x =&gt; x*x).Subscribe(Console.WriteLine)Observable.Range(0, 20).Zip(Observable.Interval(TimeSpan.FromSeconds(1)), (x,y) =&gt; x).Where(x =&gt; 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
  • Transcript of "Introduction to Reactive Extensions"

    1. 1. 1 Introduction to Reactive ExtensionsPeter GoodmanAn Introduction to ReactiveExtensions
    2. 2. 2 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
    3. 3. 3 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
    4. 4. 4 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
    5. 5. 5 Introduction to Reactive ExtensionsWhat are Reactive Extensions?
    6. 6. 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. 7. 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. 8. 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. 9. 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. 10. 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. 11. 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. 12. 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. 13. 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. 14. 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. 15. 15 Introduction to Reactive ExtensionsDEMOGenerating events and subscribing to them
    16. 16. 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. 17. 17 Introduction to Reactive ExtensionsDEMOQuerying, Composition and introducing Concurrency
    18. 18. 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. 19. 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. 20. 20 Introduction to Reactive Extensions Concurrency and Synchronization• var Observable.Return Scheduler.ThreadPool " "• .ObserveOnDispatcher() " "
    21. 21. 21 Introduction to Reactive ExtensionsDEMOSynchronization
    22. 22. 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. 23. 23 Introduction to Reactive ExtensionsDEMORx for JavaScript
    24. 24. 24 Introduction to Reactive ExtensionsReactive ExtensionsQuestions?
    25. 25. 25 Introduction to Reactive ExtensionsResources MSDN Bart de Smet / Rx (Channel 9) Reactive UI Pushqa / SignalR
    26. 26. 26 Introduction to Reactive ExtensionsContact pete@petegoo.com http://blog.petegoo.com Twitter: @petegoo
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×