26
Reacting With ReactiveUI Allen Greaves IntelliTect

Reacting with ReactiveUI

Embed Size (px)

Citation preview

Page 1: Reacting with ReactiveUI

Reacting With ReactiveUIAllen Greaves

IntelliTect

Page 2: Reacting with ReactiveUI

System.Reactive ReactiveUI

Technologies

Can be found on NuGet Rx-Main

Can be found on NuGet reactiveui-xaml

Page 3: Reacting with ReactiveUI

Namespaces using System.Reactive using System.Reactive.Linq using System.Reactive.Concurrency using ReactiveUI using ReactiveUI.Xaml

Page 4: Reacting with ReactiveUI

Demo

Page 5: Reacting with ReactiveUI

ToObservable() Converts an enumerable sequence to an observable

sequence Behaves like ToList()

Page 6: Reacting with ReactiveUI

SelectMany( x => … ) Projects a sequence into observable sequences Then flattens these sequences into a sequence

Page 7: Reacting with ReactiveUI

Observable.Start( () => … ) Invokes a function asynchronously

Page 8: Reacting with ReactiveUI

Subscribe( x => … ) Bread and butter of Reactive Kicks off processing of the observable sequence Behaves like ForEach( x => … )

Page 9: Reacting with ReactiveUI

ObserveOnDispatcher() Marshals asynchronous observables onto the dispatcher

thread Behaves like CurrentDispatcher.BeginInvoke() Necessary for WPF

Page 10: Reacting with ReactiveUI

ReactiveObjectReactiveValidatedObject Provides useful extensions ViewModels as ReactiveObjects Validation

[ValidatesViaMethod(…)]

Page 11: Reacting with ReactiveUI

Demo

Page 12: Reacting with ReactiveUI

Select( x => … ) Projects a sequence of elements Behaves like enumerable Select( x => … ) Does not flatten

Page 13: Reacting with ReactiveUI

Observable.Defer( () => … ) Defers function invocation Without it, Select() will go ahead and project all

elements

Page 14: Reacting with ReactiveUI

Merge( … ) Merges many sequences into one sequence Limits the number of in-flight threads Used with Observable.Defer

Page 15: Reacting with ReactiveUI

Other Fun Extensions! Delay( … )

Delays the sequence Buffer( … )

Batches input Interval( … )

Sequence of elements occurring on a set interval Throttle( … )

Ignores values during a specified TimeSpan Timestamp( … )

Timestamps elements in a sequence

Page 16: Reacting with ReactiveUI

Non-Observable a = b + c

a is set to the sum of b and c If b or c updates then a remains the same

Page 17: Reacting with ReactiveUI

Observable a = b + c

a is set to the sum of b and c If b or c updates then a is reevaluated

Page 18: Reacting with ReactiveUI

public interface IObservable<out T>{ IDisposable Subscribe(IObserver<T> observer);}

public interface IObserver<in T>{ void OnNext(T value); void OnError(T error); void OnComplete();}

Page 19: Reacting with ReactiveUI

Demo

Page 20: Reacting with ReactiveUI

this.WhenAny( x => … ) Makes dealing with PropertyChanged notifications easier First real look at events as a sequence

Page 21: Reacting with ReactiveUI

Where( x => … ) Filters elements of a sequence

Page 22: Reacting with ReactiveUI

Demo

Page 23: Reacting with ReactiveUI

new ReactiveCommand( … ) Subscribes to an IObservable for CanExecute()

Makes heavy operations easier Subscribe to the IObservable to receive command elements

Page 24: Reacting with ReactiveUI

UpdateCommand = new ReactiveCommand( this.WhenAny( x => x.FileNames, x => x.Value ) .Select(item => item != null && item.Any()) );

UpdateCommand.Subscribe( (x) => … );

Page 25: Reacting with ReactiveUI

What do you get? Loosely coupled code

Everything is built by hooking up observables Properties can be dumb Prevents having to put code in the getters and setters of bound

properties Easier and more powerful event handling

Handling the PropertyChanged event is a mess Unit Testing

Since our code is much more loosely coupled we can test easier Scheduler

Page 26: Reacting with ReactiveUI

Resources Reactive Extension Guides

http://msdn.microsoft.com/en-us/data/gg577611 101 samples

http://rxwiki.wikidot.com/101samples#toc25 ReactiveUI – Paul Betts

http://www.reactiveui.net/