Reactive Extensions for JavaScript

Post on 22-Jan-2018

2.338 views 1 download

Transcript of Reactive Extensions for JavaScript

1 Slalom ConfidentialSlalom Confidential

Reactive Extensions for JavaScript

@JimWooleyhttp://www.ThinqLinq.com

Consultant

2 Slalom Confidential

Reactive Framework

“Rx is a library for composing asynchronous andevent-based operations using

observable collections.”

3 Slalom Confidential

Reactive Framework

“RxJs is a library for composing asynchronous andevent-based operations using

observable prototypes.”

4 Slalom Confidential

Observable Collection - Common Uses

5 Slalom Confidential

Flavors of Rx

6 Slalom Confidential

IEnumerator vs. IObserver

public interface IEnumerator{

object Current { get; }bool MoveNext();void Reset();

}

public interface IObserver<T>

void OnCompleted

void OnError Exception exception

void OnNext value

7 Slalom Confidential

LINQ for Javascript?

C# Query Syntaxvar query = from c in Customers

where c.City == "Chattanooga“select c.Name;

C# Lambda Syntaxvar query = Customers

.Where(c => c.City == "Chattanooga")

.Select(c => c.Name);

JavaScriptvar query = Customers

.Where(function (c) { return c.City === “Chattanooga” ; })

.Select(function (c) { return c.Name; });

8 Slalom Confidential

USING RX TO QUERY IN JAVASCRIPTdemo

9 Slalom Confidential

Rx Observable Pattern

var obs = ...;

// query?

var sub = obs.Subscribe(onNext : v => DoSomething(v),onError : e => HandleError(e),onCompleted : () => HandleDone);

sub.Dispose();

10 Slalom Confidential

RxJs Observable Pattern

var obs = ...;

// query?

var sub = obs.Subscribe(function(v) { DoSomething(v); },function(e) { HandleError(e); },function() { HandleDone(); });

sub.Dispose();

11 Slalom Confidential

var geo = Rx.Observable.Create(function (observer) { var watchId = navigator.geolocation.watchPosition(

function (pos) { observer.OnNext(pos.coords); },function (err) { observer.OnError(err); }

);

return function () {navigator.geolocation.clearWatch(watchId);

};});

var d = geo.Subscribe(function(pos) { alert(pos); });

d.Dispose();

Observable.Create

8675309

8675309

12 Slalom Confidential

Time

RX.Observable.Prototype

Query Flow Generator

SelectLet

Where

TakeSkip

GroupBy

StartsWithMerge

MergeObservableConcat

ZipCombineLatest

Switch

SelectManyTakeWhileTakeUntilSkipUntil

Prune

FlowScan

DistinctUntilChangedReplay

SkipLastTakeLast

DoCatchFinally

OnErrorResumeNext

TimeIntervalRemoveInterval

RemoveTimestampBufferWithTimeBufferWithCount

Throttle

SampleRepeatRetry

TimeoutDelay

SubscribeDematerializeAsObservsable

Publish

13 Slalom Confidential

Factory Generators

Rx.Observable.Range(0,5);Rx.Observable.Return(“This is a single value”);Rx.Observable.FromArray(*“a”, “b”, “c”+);Rx.Observable.Timer(1000,2000);Rx.Observable.Create(function(subscriber) { return function() { unsub() }});Rx.Observable.FromDOMEvent(Document.GetElementById(“image”),

“mousemove”);Rx.Observable.FromJQuery($(“div,, span”), “click”);$(“div, span”).ToObservable(“click”);Rx.Obsevable.XmlHttpRequest(url);

14 Slalom Confidential

DEMOGenerating Observables

15 Slalom Confidential

Combining streams

SelectMany

Merge

Zip Amb

CombineLatest

TakeUntil

TakeWhile Combine

Switch

StartWith

Concat

SkipWhile

16 Slalom Confidential

SelectMany

Ox

fx fx fx

Oy

Oy

Oy

Oy

var ObservableY = ObservableX.SelectMany(function(x) { SomeGenerator(x); });

17 Slalom Confidential

TakeUntil

oX

oY

oX

18 Slalom Confidential

Observable Events

First Class Objects Standardize event model Disposable Supports DI Mockable

19 Slalom Confidential

COMPOSING WITH EVENTSdemo

20 Slalom Confidential

Asynchronous Service Requests

21 Slalom Confidential

ASYNCHRONOUS SERVICESdemo

22 Slalom Confidential

Why RxJs?

Reuse LINQ knowledge Coordination of events/streams Standardized events State machine management Async time related operations Gesture Detection Web Sockets? ???

23 Slalom Confidential

Further Information

Rx Data Developer Center: http://msdn.microsoft.com/data/gg577609

Reactive Extensions Team Blog: http://blogs.msdn.com/rxteam

Channel9: http://channel9.msdn.com/Tags/Rx

Matt Podwysocki’s Blog:http://weblogs.asp.net/podwysocki/archive/tags/Reactive+Framework/default.aspx

24 Slalom Confidential

Call to Action

Get the bits• Shipped in Windows Phone 7 ROM• Install with NuGet• Download at MSDN Data Developer Center

Join the Community (MSDN forums)http://social.msdn.microsoft.com/Forums/en-US/rx

25 Slalom Confidential

Jim Wooley@jimwooley

http://www.ThinqLinq.com