background preloader

Reactive Extensions

Facebook Twitter

Make Async Your Buddy With Reactive Extensions. For a long time, good folks like Matt Podwysocki have extolled the virtues of Reactive Extensions (aka Rx) to me. It piqued my interest enough for me to write a post about it, but that was the extent of it. It sounded interesting, but it didn’t have any relevance to any projects I had at the time. Fortunately, now that I work at GitHub I have the pleasure to work with an Rx Guru, Paul Betts, on a project that actively uses Rx. And man, is my mind blown by Rx. Hits Me Like A Hurricane What really blew me away about Rx is how it allows you to handle complex async interactions declaratively. The way I describe it to folks is to think of how the IEnumerable and IEnumerator are involved when iterating over an enumerable. Hear that? The-future Rx has a tendency to twist and contort the mind in strange ways.

Here’s a simple example that helps demonstrate the power of Rx. I modified it in two ways for my needs. Handling Resize Events So let’s think about this a bit. That’s it! We forgot something.


Samples. Make Async Your Buddy With Reactive Extensions. Time Handling. C# async/await makes reactive testing expressive! Isolating asynchronous behavior isn't always possible, especially when it comes to creating integration tests. However, with the new async/await features in c# 5, testing reactive interfaces is a breeze and can be very expressive. FileSystemWatcher and events FileSystemWatcher is commonly used to monitor for changes to a directory. It's been around since .Net 1.1. Whenever I work with event based interfaces, I prefer to wrap them with an Observable. Testing this often requires an integration test. Here's some code to convert the FileSystemWatcher.Changed event to an observable: IObservable<FileSystemEventArgs> Changed = Observable .FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h => Watcher.Changed += h, h => Watcher.Changed -= h) .Select(x => x.EventArgs); I'm using this to create an ObservableFileSystemWatcher adapter.

The following code is captured in this gist, look at the revision history to see the changes between each of the following samples. Designing a test. React 2014 : Lee Campbell & Matt Barrett - Event Driven User Interfaces. Introduction to Rx. Creating ViewModels with ReactiveObject. I've decided to start re-publishing the ReactiveUI documentation in blog post format over the next few weeks, so that more people get eyes on it. To get a sneak preview for what's coming next, head to the docs PR on GitHub At the core of every MVVM framework is the ViewModel - while this class is the most interesting aspect of the MVVM pattern, it is also the most misunderstood. Properly reasoning about what a ViewModel is and is not, is crucial to correctly applying the MVVM pattern. The Zen of The ViewModel Most UI frameworks have applied almost zero thought to unit testing when their framework was designed, or those concerns were deemed as out-of-scope.

So, since UI classes are untestable, our new goal is to put as much of our interesting code, into a class that represents the View, but is just a regular class we can create. This class is called the ViewModel, and it is the Model of a View. ViewModels are Reusable Common Mistakes and Misconceptions Read-Write Properties Output Properties. Reactive Extensions at Thursday Night. The symptoms of this bug If you use the Silverlight version of ReactiveUI, you will currently encounter a lot of issues that appear to be the classic InvalidOperationException, where a Silverlight object is being accessed by a background thread – it might look something like this: at System.Windows.Threading.Dispatcher.VerifyAccess() at System.Windows.DependencyObject.GetValue(DependencyProperty dp) at System.Windows.Controls.Primitives.ButtonBase.get_Command() at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute() at System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged(Object sender, EventArgs e) at ReactiveUI.Xaml.ReactiveCommand.

You’ll also see the message in the Console output, "WPF Rx.NET DLL reference not added - using Event Loop" How to fix it The easiest way to fix this is by adding this block to your App.xaml.cs: protected override void OnStartup(StartupEventArgs e) { RxApp.DeferredScheduler = DispatcherScheduler.Instance; base.OnStartup(e); } What’s New.


Using Reactive Extensions for Streaming Data from Database - About My Code. You have probably heard about Reactive Extensions, a library from Microsoft that greatly simplifies working with asynchronous data streams and allows to query them with Linq operators. There are many different scenarios where using rx results in a much more simple and flexible code. This post demonstrates how to use reactive extensions for loading data from database asynchronously in chunks. The Problem Recently I had to load data from sqlite database in grid view but the query was taking long time as there were hundreds of thousands of rows and the query was doing like %search term% filtering. The customer wanted to run the query in background and load data in chunks as they became available similar to sql server management studio. Solution without Reactive Extensions The most straightforward way to solve the problem is to call the method that reads the data in background thread and send the data to main thread when there are enough rows.

Reactive Extensions – Simplifying the Solution. Asynchronous Composition with the Reactive Extensions. Increasingly, users have come to expect that applications they use continue to respond while the application processes information in the background. To achieve the best responsiveness, you need to build applications to be as asynchronous as possible. In the past, creating asynchronous operations consisted of passing callback delegates or lambda expressions to indicate what action to take when an action completes.

These can lead to an unmanageable mess of spaghetti code. To remedy this situation, the Microsoft Cloud Programmability Group created a set of libraries known as the Reactive Extensions (Rx). Rx provides a LINQ-like syntax to declaratively compose increasingly complex asynchronous operations over “observable” collections. These collections can be lists of known objects, values received through events, sensor readings, or responses from Web service requests. IEnumerable vs. When working with groups of data, you need to perform the same operations repeatedly. Setting up the Game. Using Reactive Extensions in Silverlight. This article is compatible with the latest version of Silverlight.

This is part 1 of the series “Reactive Extensions in Silverlight”. 1. Introduction One of the coolest features which is part of .NET Framework and also available for Silverlight applications is the RX Framework. Take a look at the next code snippet: How many times have you done this in your application? What we have here is an implementation of the Iterator pattern (IEnumerable\IEnumerator). Now, if we take a look at the RX style collection, they are push collections. We are handling the MouseMove event. On one side – we have the Iterator pattern ( IEnumerable<T>\IEnumerator<T>) and on the other side we have the Observer pattern (IObservable<T>\IObserver<T>).

Ok, But what can be done with the RX Framework? Probably, each time I am playing with a new technology, the part that is most difficult for me is to put the technology into practice. Asynchronous programming. Enough words, let’s take a look at some demos. 2. P => 3. 4. A Generic Class for Wrapping Asynchronous Begin/End Operations, Using Reactive Extensions for .NET (Rx) Download source code - 118 KB Introduction One of the patterns you will come across as a .NET programmer is the Begin/End pattern for making asynchronous function calls. The purpose of the pattern is to allow long-running operations to execute on a different thread than the calling thread, leaving the calling thread free (non-blocked) to continue execution.

This is an important technique for building responsive GUIs, as well as for making remote calls effectively (whether you are calling a WCF service, using .NET remoting, accessing some REST-based Web Service, etc.). In case you haven't seen it before, it looks like this: IAsyncResult BeginOperation(...some number of parameters as input, AsyncCallback callback, object state); SomeResult EndOperation(IAsyncResult); While the BeginInvoke/EndInvoke pattern is powerful, it is awkward and non-intuitive to work with, especially if the EndOperation portion returns a value.

Background Using the code Example 1 (Consuming a REST Web Service) History. Rx - Reactive Extensions. A[nother] Simpler Tutorial for Reactive Extensions | notabeta. Years ago, I wrote a small console application in VB6 that simulated a digital circuit by using gates. It was a great way to learn OOP, because each class directly corresponded with a real-life entity, and the behavior was something slightly more useful than dogs barking or cows mooing. Wiring up some events and a basic input form, I was able to simulate the results of fairly complex circuits. The problematic part of this is that because of the event model, it took forever to calculate, and hundreds of lines of code. Fast-forward to last year when I heard about Reactive Extensions for .NET, a true implementation of a push-style observer pattern built on top of IEnumerable<>. Most Rx tutorials read something like “Ok, you have some Xs and Ys. Ok, let’s take an AND gate. An AND Gate takes two input bits, each a 0 or 1, and outputs a 1 only if both inputs are 1.

So, what does Rx do that will help us here? Rx requires us to rethink the way inputs occur.

Addon Libraries