Download - Reactive Extensions
![Page 1: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/1.jpg)
REACTIVE EXTENSIONS
![Page 2: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/2.jpg)
PULL…
• Most programming today is based on a “pull” model
• Ask a data source for an item, wait for the item to be returned, and then process it
• Our app is active the entire time
• makes the request,
• waits for the result (usually a blocking operation),
• controls the speed at which we process the result
![Page 3: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/3.jpg)
…VS PUSH
• The “push” model aims to make our applications less concerned with the data flow
• Our app asks a data source to notify us if it has data we can use
• The data source is responsible for the notification and sending of data
• Our app is free to continue on its way, and is simply notified if there’s new data
• AKA Observer Pattern
![Page 4: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/4.jpg)
REACTIVE EXTENSIONS (RX)
• Some neat baked-in interfaces, extension methods, and static classes for implementing Pull-based (observable pattern) systems
• Originally announced on Nov 17, 2009
• Included in .NET v4.0
![Page 5: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/5.jpg)
IOBSERVABLE<T>
• Interface representing a collection that you can “observe”
• IDisposable Subscribe(IObserver<Type> observer) only interface method
![Page 6: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/6.jpg)
IOBSERVER<T>
• Interface representing something that can observe an observable
• void OnNext(T data) – called by observable when the next piece of data is ready
• void OnError(Exception ex) – called by observable when an error occurs
• void OnCompleted() – called by observable when the pull operation has completed
![Page 7: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/7.jpg)
EXAMPLE!
• Twitter Firehose API Example
![Page 8: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/8.jpg)
OBSERVABLE STATIC CLASS
• IObserable/IObserver are all well and good, but we really could’ve done that without Rx
• Observable/Observer provide static methods to create Observables/Observers without class definitions
• Provides a whole bunch of options
• Separate install (can be installed from NuGet)
![Page 9: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/9.jpg)
OBSERVABLE
• Example Code!
![Page 10: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/10.jpg)
SCHEDULER
• By default, Observable will automatically schedule itself on a certain thread, by default it uses TaskPool
• You can control this by passing in a scheduler
• ImmediateScheduler, CurrentThreadScheduler, DispatcherScheduler, NewThreadScheduler, TaskPoolScheduler, ThreadPoolScheduler
• Example Code!
![Page 11: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/11.jpg)
RX AND LINQ
• Rx adds a few new LINQ extension methods, as well as uses the existing ones
• Example Code!
![Page 12: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/12.jpg)
OBSERVABLE SUBJECT
• Subject is a proxy class you can use to wrap a non-pull system and create an observable
• Implements both IObservable<T> and IObserver<T>
• Example Code!
![Page 13: Reactive Extensions](https://reader036.vdocuments.us/reader036/viewer/2022082808/554ebdc0b4c9053c4b8b478b/html5/thumbnails/13.jpg)
IQBSERVABLE
• Combines LINQ’s Queryable and Rx’s Observable functionality
• Queryable – allows you to create a query client side using LINQ, and pass that query to a data source (server, database, web service, etc)
• Observable – instead of blocking until the data comes back, will just let you know when it gets the data