Slide 1

Slide 1 text

Reactive Extensions and ReactiveUI @xpaulbettsx Tuesday, December 27, 11

Slide 2

Slide 2 text

A Model-View-ViewModel framework for WPF, Silverlight, and Windows Phone... ...that integrates the Reactive Extensions for .NET. What is ReactiveUI Tuesday, December 27, 11

Slide 3

Slide 3 text

Write completely non-blocking UIs whose interactions are described in a declarative fashion, and be able to test long-running timelines instantly using virtual time Why is that cool? Tuesday, December 27, 11

Slide 4

Slide 4 text

Not just limited to XAML! • ReactiveUI core works anywhere • ...but the Mono compiler wasn’t havin’ it • With some hacking, works on MonoTouch Tuesday, December 27, 11

Slide 5

Slide 5 text

Code in XAML code-behind is untestable - let’s move as much of our behavior into a separate object that we can test, and abstract away commands from how they’re invoked (button clicks) MVVM Tuesday, December 27, 11

Slide 6

Slide 6 text

Functional Reactive Programming Tuesday, December 27, 11

Slide 7

Slide 7 text

LINQ is Awesome • Core of LINQ is the sequence (IEnumerable) • Operators create a “pipeline” - you put stuff in, get stuff out • new[] {1,2,3,4} .Where(x => x % 2 == 0) .Select(x => x * 10); >>> [20, 40] Tuesday, December 27, 11

Slide 8

Slide 8 text

A Sequence is just some stuff in a particular order (We’ll come back to this later!) Tuesday, December 27, 11

Slide 9

Slide 9 text

Functional Reactive Programming Tuesday, December 27, 11

Slide 10

Slide 10 text

We can no longer write synchronous software in an async world. - Abraham Lincoln Tuesday, December 27, 11

Slide 11

Slide 11 text

someWindow.MouseMove += OnMouseMove; You’ve been doing Reactive Programming for years! Tuesday, December 27, 11

Slide 12

Slide 12 text

• Events aren’t composable • Creating a “DoubleClicked” event from the MouseDown and MouseUp events should be easy, but it’s not. ...it’s just always sucked. Tuesday, December 27, 11

Slide 13

Slide 13 text

What is an Event? • If I watch “KeyUp” - what comes out of that? • Event arguments: “M”, “o”, “n”, “o” • Hmmm, that looks like some stuff in a particular order. Tuesday, December 27, 11

Slide 14

Slide 14 text

Tuesday, December 27, 11

Slide 15

Slide 15 text

IObservable • We need a new interface though for Observable Sequences. • If IEnumerable is a “list”, IObservable is a “Future List” • IObservable has exactly one method: IDisposable Subscribe(IObserver subscriber) Tuesday, December 27, 11

Slide 16

Slide 16 text

IObserver • The equivalent to IEnumerator • Three methods = three things an Observable can do: void OnNext(T value); void OnError(Exception ex); void OnCompleted(); Tuesday, December 27, 11

Slide 17

Slide 17 text

Subject • Subjects are Observables we can control by-hand - they are the List of Rx. • LinqPad Demo! Tuesday, December 27, 11

Slide 18

Slide 18 text

IObservable as a Future • IObservable also can practically represent a Future (i.e. like Task), by returning an IObservable of length 1 and completing • Remember this, it’s important! Tuesday, December 27, 11

Slide 19

Slide 19 text

Rx Async Methods • Just like TPL async methods always return Task, Rx async methods always return IObservable public IObservable AsyncAdd(int a, int b) { return Observable.Start(() => return a+b); } Tuesday, December 27, 11

Slide 20

Slide 20 text

Using Async Methods // Block until the async call comes back var result = asyncCall(17).First(); // Get the result async asyncCall(42).Subscribe(x => /* Do Stuff */); // Let’s try a list var results = new[]{17, 42, 87}.ToObservable() .SelectMany(x => asyncCall(x)) .ToList().First(); Tuesday, December 27, 11

Slide 21

Slide 21 text

Demo! Tuesday, December 27, 11

Slide 22

Slide 22 text

LINQ-to-Events Tuesday, December 27, 11

Slide 23

Slide 23 text

Finally, we get to RxUI • RxUI gives you three main things • An object that can be Observed (ReactiveObject) • An ICommand which implements IObservable • A collection that can be Observed (ReactiveCollection) Tuesday, December 27, 11

Slide 24

Slide 24 text

Property change notifications are Events. Events => Observables. Tuesday, December 27, 11

Slide 25

Slide 25 text

RxUI is all about converting Properties to Observables, and Observables into Properties. Tuesday, December 27, 11

Slide 26

Slide 26 text

Output Properties Tuesday, December 27, 11

Slide 27

Slide 27 text

public ColorPickerViewModel() { IObservable colorValues = this.WhenAny( x => x.Opacity, x.Brightness, x.Hue, (opacity, brightness, hue) => Color.FromHSV(opacity.Value, brightness.Value, hue.Value)); colorValues.Subscribe(x => Console.WriteLine("Color is set to: {0}", x)); colorValues.ToProperty(this, x => x.FinalColor); } Tuesday, December 27, 11

Slide 28

Slide 28 text

Another Example Tuesday, December 27, 11

Slide 29

Slide 29 text

• This app consists of three things: An input (the query) An output (the array of images) A command (the “Search” button) Our ViewModel Tuesday, December 27, 11

Slide 30

Slide 30 text

ReactiveCommand SearchButton { get; protected set;} string SearchText { get; protected set;} List ImagesToDisplay { get; set; } searchButton .Select(x => this.SearchText) .Where(x => String.IsNotNullOrEmpty(x)) .SelectMany(x => fetchImagesFromBingSearch(x)) .ToProperty(this, x => x.ImagesToDisplay); Thinking Functionally Tuesday, December 27, 11

Slide 31

Slide 31 text

Testing with Virtual Time Tuesday, December 27, 11

Slide 32

Slide 32 text

Thanks! http://www.reactiveui.net Tuesday, December 27, 11