reactive programming with rx (based on bit.ly/caxkpk )

38
1 Reactive Programming with Rx (based on http:// bit.ly/cAxKPk ) Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Upload: emera

Post on 21-Mar-2016

49 views

Category:

Documents


2 download

DESCRIPTION

Reactive Programming with Rx (based on http:// bit.ly/cAxKPk ). Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010. Iterable vs Observable. Iterable : sequence I can iterate over and pull values - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

1

Reactive Programming with Rx (based on http://bit.ly/cAxKPk)

Ras Bodik, Thibaud Hottelier, James IdeUC Berkeley

CS164: Introduction to Programming Languages and Compilers Fall 2010

Page 2: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Iterable vs Observable

Iterable: sequence I can iterate over and pull values

Observable: sequence that notifies when a new value is added (push)

These two are dual

2

Page 3: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

1) Observable and Observer objectsData source:

var answer = Rx.Observable.Return(42);Listener:

var observer = Rx.Observer.Create( function (x) { document.write("The answer is " + x); } );

Connecting the two:answer.Subscribe(observer);

3

Page 4: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

The same code packaged in a web page

<script type="text/javascript"> function iExploreRx() { var answer = Rx.Observable.Return(42); var observer = Rx.Observer.Create( function (x) { document.write("The answer is " + x); } ); answer.Subscribe(observer); } </script><body> <button onclick="javascript:iExploreRx()">Tell me the answer</button> </body>

4

Page 5: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

2) Observable sequencesFirst, let’s set up the listener:

var source = null; // we’ll try various sequences here

var subscription = source.Subscribe( function (next) { $("<p/>").html("OnNext: "+next).appendTo("#content"); }, function (exn) { $("<p/>").html("OnError: "+exn).appendTo("#content"); }, function () { $("<p/>").html("OnCompleted").appendTo("#content"); });

5

Page 6: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Empty sequencevar source = Rx.Observable.Empty();

Produces the output

OnCompleted

6

Page 7: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Sequence with a terminating notification

var source = Rx.Observable.Throw("Oops!");

Running this code produces the following output:

OnError: Oops

7

Page 8: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Single-element sequence

var source = Rx.Observable.Return(42); Running this code produces the following output:

OnNext: 42 OnCompleted

8

Page 9: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

We are now done with trivial sequences

var source = Rx.Observable.Range(5, 3); Running this code produces the following output:

OnNext: 5 OnNext: 6 OnNext: 7 OnCompleted

9

Page 10: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

A for-loop like sequence generator var source = Rx.Observable.GenerateWithTime( 0, // initial value of iterator variable function(i) { return i < 5; }, // test function(i) { return i + 1; }, // incr function(i) { return i * i; }, // value function(i) { return i * 1000; });

Last function computes how many ms to wait between generated values (here, 1, 2, 3, … seconds)

10

Page 11: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

3) Events as data sourcesIn JavaScript $(document).ready(function () { $(document).mousemove(function (event) { $("<p/>").text("X: " + event.pageX+" Y: " + event.pageY) .appendTo("#content"); }); });

In Rx $(document).ready(function () { $(document).toObservable("mousemove").Subscribe(function(event){ $("<p/>").text("X: " + event.pageX+" Y: " + event.pageY) .appendTo("#content"); }); });

11

Page 12: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

4) Projection and filteringIn event-handler programming, you’d write:

function handleMouseMove(event) { // FILTER some events if (event.pageX === event.pageY) { // Only respond to events for mouse moves // over the first bisector of the page. } } function handleKeyUp(event) { // PROJECT the event’s val var text = $(event.target).val(); // And now we can forget about the rest // of the event object’s data... }

12

Page 13: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

The same in Rxvar moves = $(document).toObservable("mousemove") .Select(function(event) { return { pageX : event.pageX, pageY : event.pageY }; }); var input = $("#textbox").toObservable("keyup") .Select(function(event) { return $(event.target).val(); });

13

Page 14: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Now we can subscribe to moves and input

var movesSubscription = moves.Subscribe(function (pos) { $("<p/>").text("X: " + pos.pageX + " Y: " + pos.pageY) .appendTo("#content"); });

var inputSubscription = input.Subscribe(function (text) { $("<p/>").text("User wrote: " + text) .appendTo("#content"); });

14

Page 15: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

CompositionWe were able to compose observable sequences thanks to the first-class nature of sequences

“First-class” means sequences are values which can be stores, passed, and we can define operators for them, such as the filter operator Where.

15

Page 16: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Removing duplicate eventsDOM raises a keyup event even when the text in textbox does not change. For example, if you select a letter and change it to the same letter. This causes duplicate events:

16

Page 17: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Rx solution

var input = $("#textbox").toObservable("keyup") .Select(function (event) { return $(event.target).val();}) .DistinctUntilChanged(); var inputSubscription = input.Subscribe(function (text) { $("<p/>").text("User wrote: " + text) .appendTo("#content"); });

17

Page 18: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Do, Throttle, and Timestampvar input = $("#textbox").toObservable("keyup") .Select(function (event) { return $(event.target).val(); }) .Timestamp() .Do(function(inp) { var text = "I: " + inp.Timestamp + "-" + inp.Value; $("<p/>").text(text).appendTo("#content"); }) .RemoveTimestamp() .Throttle(1000) .Timestamp() .Do(function(inp) { var text = "T: " + inp.Timestamp + "-" + inp.Value; $("<p/>").text(text).appendTo("#content"); }) .RemoveTimestamp() .DistinctUntilChanged(); 18

Page 19: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Throttle A timer is used to let an incoming message age for the specified duration, after which it can be propagated further on.

If during this timeframe another message comes in, the original message gets dropped on the floor and substituted for the new one that effectively also resets the timer.

Can be used to suppress the number of requests sent to a web service.

19

Page 20: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

6) Asynchrony with the serverLet’s implement instant search for Wikipedia:

20

Page 21: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Interface to Wikipedia in JS

21

$.ajax({ url: "http://en.wikipedia.org/w/api.php", dataType: "jsonp", data: { action: "opensearch", search: "react", format: "json" }, success: function (data, textStatus, xhr) { $("#results").empty(); $.each(data[1], function (_, result) { $("#results").append("<li>" + result+"</li>"); }); }, error: function (xhr, textStatus, errorThrown) { $("#error").text(errorThrown); } });

Page 22: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

The same in Rx

$.ajaxAsObservable( { url: "http://en.wikipedia.org/w/api.php", dataType: "jsonp", data: { action: "opensearch", search: "react", format: "json" } });

22

Page 23: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

From a term to observable with search results

A function that creates an observable for a search term:

function searchWikipedia(term) { return $.ajaxAsObservable( { url: "http://en.wikipedia.org/w/api.php", dataType: "jsonp", data: { action: "opensearch", search: term, format: "json" } }) .Select(function (d) { return d.data[1]; }); }

23

Page 24: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Print the result of the searchvar searchObservable = searchWikipedia("react"); var searchSubscription = searchObservable.Subscribe( function (results) { $("#results").empty(); $.each(results, function (_, result) { $("#results").append("<li>"+result+"</li>"); }); }, function (exn) { $("#error").text(error); } ); 24

Page 25: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

7) The full thing (dealing w/ out-of-order msgs, too)

<body> <input id="searchInput" size="100" type="text"/> <ul id="results" /> <p id="error" /> </body>

25

Page 26: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

The header

<head> <title>Wikipedia Lookup</title> <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript" src="Scripts/rx.js"></script> <script type="text/javascript" src="Scripts/rx.jquery.js"></script>

26

Page 27: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Wikipedia interface (just like before)

function searchWikipedia(term) { return $.ajaxAsObservable( { url: "http://en.wikipedia.org/w/api.php", dataType: "jsonp", data: { action: "opensearch", search: term, format: "json" } }) .Select(function (d) { return d.data[1]; }); }

27

Page 28: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

The input, throttled$(document).ready(function () { var terms = $("#searchInput").toObservable("keyup") .Select(function (event) { return $(event.target).val(); }) .Throttle(250); // Time to compose stuff here...

});

28

Page 29: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

We want to achieve this composition

29

Page 30: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

How to compose our two components?1) Observable sequence of strings 2) Function from a string to an observable

sequence that contains an array with the results

var searchObservable = terms.Map(function (term) { return searchWikipedia(term); });

30

Page 31: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Why is this solution incorrect?input type:

Observable[string]type of searchWikipedia:

string Observable[Array[string]]hence Map’s result type:

Observable[Observable[Array[string]]while we want

Observable[Array[string]]hence we need to flatten the inner observable sequences

31

Page 32: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Correct colutionSelectMany: projects each value of an observable sequence to an observable sequence and flattens the resulting observable sequences into one sequence.

var searchObservable = terms .SelectMany(function (term) { return searchWikipedia(term); });

32

Page 33: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Bind results to a UIAs in Section 6.

33

Page 34: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Out of order sequences

34

Page 35: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Must suppress the older request

35

Page 36: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

.Select().Switch()

var searchObservable = terms .Select(searchWikipedia);

// Observable[Observable[…]] .Switch(); // Switch subscribes to latest sequence

36

Page 37: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Semantics of Switch

37

Page 38: Reactive Programming with Rx  (based on   bit.ly/cAxKPk )

Summary

From non-composable event handlers to composable observable sequences (streams)

Read the full tutorial, which contains one more interesting section, at http://bit.ly/cAxKPk

start reading on page 7 (Exercise 2)

38