Volt Volta Quo Quo Vadi Vadis? s? Erik Meijer Wes Dyer - - PowerPoint PPT Presentation

volt volta quo quo vadi vadis s erik meijer
SMART_READER_LITE
LIVE PREVIEW

Volt Volta Quo Quo Vadi Vadis? s? Erik Meijer Wes Dyer - - PowerPoint PPT Presentation

Volt Volta Quo Quo Vadi Vadis? s? Erik Meijer Wes Dyer Jeffrey van Gogh Bart de Smet Matthew Podwysocki Fu Fundam damentally entally ch chan ange ge th the way e way yo you th think ink ab abou out t co coor ordin


slide-1
SLIDE 1

Volt Volta Quo Quo Vadi Vadis? s?

slide-2
SLIDE 2

Erik Meijer Wes Dyer Jeffrey van Gogh Bart de Smet Matthew Podwysocki

slide-3
SLIDE 3

Fu Fundam damentally entally ch chan ange ge th the way e way yo you th think ink ab abou

  • ut

t co coor

  • rdin

dinating ating an and d

  • r
  • rch

chestrating estrating as asyn ynchrono chronous us an and d eve event nt-based based pro rogramming ramming

What? hat?

slide-4
SLIDE 4

Be Beca cause use th the e es esse senc nce e of

  • f Clo

loud, d, Web Web, , Mo Mobi bile le is is as asyn ynchrono chronous us co comp mputation utations s

Why? hy?

slide-5
SLIDE 5

By By sho showing wing th that at as asyn ynchrono chronous us an and d eve event-based based co comp mputation utations s ar are e ju just st push sh-based based co colle llect ctions ions

How? w?

slide-6
SLIDE 6

An And d yo you ha u have ve th the e ch chan ance ce to to win win so some me $$ $$$

slide-7
SLIDE 7

Rea eactiv ctive Interact teractiv ive

Program in control Environment in control Progr gram am Envir vironment

  • nment

1 2 1 2

slide-8
SLIDE 8

A co A conc ncrete rete co collection lection of

  • f po

poke ker r co coins ns

enumerable collection interface

  • bservable

collection interface

implements

slide-9
SLIDE 9

Enumerable collections

interface IEnumerable<out T> { IEnumerator<T> GetEnumerator() } interface IEnumerator<out T>: IDisposable { bool MoveNext() T Current{ get; } // throws Exception }

slide-10
SLIDE 10

Enu numera merable ble co colle llection ction of ch f chips ips

GetEnumerator()

1 2

MoveNext() Current

3

Consumer pu pull lls successive elements from The collection

true/false

slide-11
SLIDE 11

Ob Obse servabl rvable e co colle llection ction of ch f chips ps

Producer pu pushe hes successive elements from The collection

Subscribe

1 2

OnCompleted() OnNext

3

slide-12
SLIDE 12

!(p||q) == (!p)&&(!q) !(p&&q) == (!p)||(!q)

Dua uality lity in n Mat ath h an and Eng ngineer ineering ing

slide-13
SLIDE 13

Dua uality lity in n Mat ath h an and Eng ngineering ineering

R = R1+…+Rn 1/R = 1/R1+…+1/Rn 1/C = 1/C1+…+1/Cn C = C1+…+Cn

slide-14
SLIDE 14

Dua uality lity in n Mat ath h an and Eng ngineering ineering

slide-15
SLIDE 15

Dualize Enumerable collections

interface IEnumerable<out T> { IEnumerator<T> GetEnumerator() } interface IObservable<out T> { IDisposable Subscribe(IObserver<T> o) }

slide-16
SLIDE 16

Dualize Enumerable collections

interface IEnumerator<out T> { bool MoveNext() // throws Exception T Current{ get; } } interface IObserver<in T> { void OnCompleted(bool done) T OnNext{ set; } void OnError(Exception e) }

slide-17
SLIDE 17

Observable collections

interface IObservable<out T> { IDisposable Subscribe(IObserver<T> o) } interface IObserver<in T> { void OnDone(bool done) T OnNext{ set; } void OnError(Exception e) }

slide-18
SLIDE 18

Observable collections

interface IObservable<out T> { IDisposable Subscribe(IObserver<T> o) } interface IObserver<in T> { void OnCompleted() void OnNext(T value) void OnError(Exception e) }

slide-19
SLIDE 19

Iterator Subject Observer

intimately related

slide-20
SLIDE 20

interface Iterable<T> { Iterator<T> iterator() } interface Iterator<T> { T next() // throws NoSuchElementException boolean hasNext() void remove() }

Ja Java a It Iterable rable & It & Itera erator tor Ob Observ ervable able/Observer /Observer

class Observable { void addObserver(Observer o); void deleteObserver(Observer o); void deleteObservers(); int countObservers(); void notifyObservers(); void notifyObservers(Object arg); void setChanged(); void clearChanged(); boolean hasChanged(); } public interface Observer { void update(Observable o, Object arg); }

slide-21
SLIDE 21

IEnume umerab rable/ le/IEn IEnume umerat rator

  • r

pro roto toty typical pical inte terf rface ace for r inte teractiv ractive e colle lectio ctions ns and inte teractiv ractive e pro rogra rams ms IObserv servable able/IOb /IObserv server er pro roto toty typical pical inte terf rface ace for r

  • b
  • bse

serv rvable able co colle lections ctions an and d re reactiv tive, e, asynch nchro ronou nous s & & ev even ent-ba based sed pro rogra rams ms

slide-22
SLIDE 22

Bu Buy y en enumera umerable ble co coll llection ections ge get t ob

  • bserva

servable ble co coll llections ections for

  • r fre

ree! e! Bo Both th co collection llection in inte terfaces rfaces su supp pport

  • rt th

the e LI LINQ NQ St Standar andard d Qu Quer ery y Oper perators ators

slide-23
SLIDE 23

Sa Samp mples les

Drag and Drop Dictionary Suggest (AJAX programming using LINQ queries) Clock (MVC programming using LINQ queries) Twitter on Bing maps Animation

slide-24
SLIDE 24
slide-25
SLIDE 25

Html Format(this string[] entries) {…} [RunAtOrigin()] IObservable<string[]> Suggest (this Dictionary dict, string prefix, int count) {…} var inputValues = from i in Input.GetKeyUp() select i.Value; var q = from s in inputValues from r in Dictionary.Suggest(s, 10) select r.Format(); q.Subscribe(html => { output.InnerHtml = html; });

Di Dict ctionary

  • nary Sug

Suggest gest in n C# C#

Combines event stream from UI with async call across network

slide-26
SLIDE 26

r ra r ray ray ra ⊑ ⊒

slide-27
SLIDE 27

var q = from s in inputValues from r in Dictionary.Suggest(s, 10) select r.Format(); var q = inputValues.Throttle(500). Publish(_inputValues => from s in _inputValues from r in Dictionary.Suggest(s, 10) .TakeUntil(_inputValues) select r.Format());

Pree eemp mption tion ope perator ator (Este stere relle) lle)

slide-28
SLIDE 28

var input = document.getElementById("input"); var results = document.getElementById("results"); var changed = Rx.Observable.FromHtmlEvent(input, "keyup") .Select(function(){ return input.value }) .Where(function(value){ return value != ""; });

In In Ja JavaS aScript cript

slide-29
SLIDE 29

changed.Throttle(250) .Select(function(what) { return query(what); }) .Switch() .Select(function(results) { return formatAsHtml(results); }) .Subscribe(function(html) { results.innerHTML = html; }, function(error){ results.innerHTML = "&gt;error retrieving results&lt;"; });

Sw Switch itch ins nste tead ad of Que f Query ry Sy Synt ntax ax

slide-30
SLIDE 30

Twitte itter r on Bi n Bing ngMa Maps ps

slide-31
SLIDE 31

jQuery.fn.ToObservable = function(eventType, eventData) { return Rx.Observable .FromJQuery(this, eventType, eventData); } Array.prototype.toObservable = function() { return Rx.Observable.FromArray(this); }; Rx.Observable.FromJQueryEvent(someJQueryObject, “mousemove”)  someJQueryObject.ToObservable(“mousemove”)

JQ JQue uery ry Bindi nding ng

slide-32
SLIDE 32

Rx.Observable.Interval(10000) .Select(function() { return searchTwitter("#mix10"); }) .Switch() .Select(function(result) { return JSON.parse(result.responseText); }) .SelectMany(function(data) { return data.results.toObservable(); }) .Where(function(data) { return data.geo != null; }) .Subscribe( function(data) { var lat = data.geo.coordinates[0]; var lon = data.geo.coordinates[1]; addPushPin(map, data.id, data.created_at, lat, lon, data.profile_image_url, data.from_user, data.text); }, function(error) { alert(error); }); });

Sa Same pa patter tern n as as di dictionary ctionary sug ugge gest st

slide-33
SLIDE 33

dX dY

Drag ag & Dr Drop p On On Wind ndow

  • ws

s Phone hone 7

slide-34
SLIDE 34

var W = … control to be dragged …; var mouseDowns = from md in W.GetMouseDown() select true; var mouseUps = from mu in W.GetMouseUp() select false; var mouseClicks = mouseDowns.Merge(mouseUps); var mouseMoves = from mm in W.GetMouseMove() select new{ mm.X, mm.Y }; var mouseDiffs = from diff in mouseMoves.Skip(1).Zip(mouseMoves) select new { dX = diff.First.X – diff.Second.X , dY = diff.First.Y – diff.Second.Y }; var mouseDrag = from leftdown in mouseClicks from delta in mouseDiffs where leftdown select delta; mouseDrag.Subscribe(delta => { … move W by delta … });

The e code e …

slide-35
SLIDE 35

var mouseDiffs = from diff in mouseMoves.Skip(1).Zip(mouseMoves) select new { dX = diff.First.X – diff.Second.X , dY = diff.First.Y – diff.Second.Y };

Si Side de-Effects Effects

var mouseDiffs = mouseMoves.Let(_mousemoves => from diff in _mouseMoves.Skip(1).Zip(_mouseMoves) select new { dX = diff.First.X – diff.Second.X , dY = diff.First.Y – diff.Second.Y });

slide-36
SLIDE 36

Model del View ew

IObserver IObservable

MVC MVC

slide-37
SLIDE 37

function startClock() { var timeModel = getTime(); var analogView = getAnalogClock(); timeModel.Subscribe(analogView); }

slide-38
SLIDE 38

function getTime() { return Rx.Observable.Interval(100) .Select(function() { var date = new Date(); var result = { hours : date.getHours(), minutes : date.getMinutes(), seconds : date.getSeconds(), }; return result; }); }

Th The Mod e Model el

slide-39
SLIDE 39

function getAnalogClock() { var hour = document.getElementById("hour"); var minute = document.getElementById("minute"); var second = document.getElementById("second"); var clock= new Rx.Observer( function(now) { hour.src = "hour" + Math.floor(now.hours*5 % 60 - now.minutes/12) + ".png"; status = hour.src; minute.src = "minute" + now.minutes + ".png"; second.src = "second" + now.seconds + ".png"; }); return clock; }

Th The View e View

slide-40
SLIDE 40

Ani Animations mations

slide-41
SLIDE 41 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Rx for JavaScript Rocks!</title> <script src="rx.js" type="text/javascript"></script> <script type="text/javascript"> function tester() { var mouseMove = Rx.Observable.FromHtmlEvent(document, "mousemove"); var text = "time flies like an arrow"; var container = document.getElementById("container"); for (var i = 0; i < text.length; i++) { (function(i) { var s = document.createElement("span"); s.innerHTML = text.charAt(i); s.style.position = "absolute"; container.appendChild(s); mouseMove.Delay(i * 100).Subscribe(function(mouseEvent) { s.style.top = mouseEvent.clientY + "px"; s.style.left = mouseEvent.clientX + i * 10 + 15 + "px"; }); })(i); } } </script> </head> <body onload="tester()" style="font-family: Consolas, monospace; overflow: hidden"> <div id="container"></div> </body> </html>
slide-42
SLIDE 42
slide-43
SLIDE 43

Follow Us On Twitter

#RxNET #RxJS (#Rx is filled with a lot of prescription garbage not related to our library :))

slide-44
SLIDE 44

Download

slide-45
SLIDE 45

Questions & Comments

emeijer@microsoft.com

slide-46
SLIDE 46

Hack the system

I pr prefe fer r IObservab vable/I le/IOb Observe erver I pr prefe fer r Ie Ienume merab rable le/I /IEnume Enumerato rator