Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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

1,973 views

Published on

An introduction to the Reactive Extensions (Rx) framework by Microsoft. Rx provides a simpler event model for handling streams of events in asynchronous and event based programs

Published in: Technology, Business
  • Be the first to comment

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

×