Slide 1

Slide 1 text

Functional Reactive Programming in Javascript

Slide 2

Slide 2 text

whoami Stratos Pavlakis Workable UI Tech Lead [email protected] th3hunt FRP newbie!

Slide 3

Slide 3 text

what’s FRP?

Slide 4

Slide 4 text

let’s begin with

Slide 5

Slide 5 text

http://www.reactivemanifesto.org/ Responsive Message Driven Resilient Elastic

Slide 6

Slide 6 text

sweet dreams

Slide 7

Slide 7 text

so better start with

Slide 8

Slide 8 text

reactive paradigm var b = 1, c = 1; var a = b + c; // a == 2 b = 10; ● // a == ? ● // imperative paradigm => a == 2 ● // reactive paradigm => a == 11

Slide 9

Slide 9 text

front end development is it synchronous or asynchronous?

Slide 10

Slide 10 text

● User Input ● AJAX ● Web Sockets / SSE ● Web Workers ● Animations ● Cross origin frame communication ● Updating the DOM we deal with

Slide 11

Slide 11 text

● User Input ● AJAX ● Web Sockets / SSE ● Web Workers ● Animations ● Cross origin frame communication ● Updating the DOM most are async!

Slide 12

Slide 12 text

tools we use

Slide 13

Slide 13 text

callbacks var el = document.getElementById("my-button"); el.addEventListener("click", function () { console.log(‘my button was clicked’); });

Slide 14

Slide 14 text

promises $http(endpoint1) .get({q: ‘frp’}) .then(function (data) { console.log(‘We got data back from ajax %s’, data); }) .then(function (data) { return $http(endpoint2).get({id: data.id}); })

Slide 15

Slide 15 text

generators in ES7 async(function main() { var result1 = await request( "http://endpoint.1" ); var data = JSON.parse( result1 ); var result2 = await request( "http://endpoint.2?id=" + data.id ); var resp = JSON.parse( result2 ); console.log( "Value: " + resp.value ); })

Slide 16

Slide 16 text

problems ● callback hell ● try / catch (except for generators) ● memory leaks ● reduced composability

Slide 17

Slide 17 text

event driven programming we react to events

Slide 18

Slide 18 text

is reason about event streams what we’re really trying to do

Slide 19

Slide 19 text

any number of values Array over any amount of time f(time) / async an event stream would be

Slide 20

Slide 20 text

first class citizen of FRP observable

Slide 21

Slide 21 text

Observer Iterator Gang of Four - Design Patterns ES6 EventEmitter

Slide 22

Slide 22 text

array VS event

Slide 23

Slide 23 text

array === collection

Slide 24

Slide 24 text

events === collection

Slide 25

Slide 25 text

collections are iterable

Slide 26

Slide 26 text

observable === collection + time

Slide 27

Slide 27 text

observable API var subscription = myObservable.subscribe(function (val) { console.log(‘Next: %s’, val); });

Slide 28

Slide 28 text

from the EventEmitter? so… how is that different

Slide 29

Slide 29 text

we know when it’s done var subscription = myObservable.subscribe( onNext, onError, onCompleted ); like promises

Slide 30

Slide 30 text

we got set operators ● map ● flatMap ● reduce ● merge ● concat ● zip

Slide 31

Slide 31 text

more operators ● debounce ● buffer ● skipUntil ● flatMapLatest ● combineLatest ● switch ● retry

Slide 32

Slide 32 text

observables can model ● mouse clicks ● key presses ● scrolling ● animations ● AJAX Polling, Web Sockets ● timers ● even… constants

Slide 33

Slide 33 text

operators http://rxmarbles.com/

Slide 34

Slide 34 text

filter

Slide 35

Slide 35 text

debounce

Slide 36

Slide 36 text

distinctUntilChanged

Slide 37

Slide 37 text

takeUntil

Slide 38

Slide 38 text

example please!

Slide 39

Slide 39 text

mousedown.flatMap((md) => { var startX = md.offsetX, startY = md.offsetY; return mousemove.map((mm) => { return { left: mm.clientX - startX, top: mm.clientY - startY }; }).takeUntil(mouseup); }).subscribe((pos) => { dragTarget.style.top = pos.top + 'px'; dragTarget.style.left = pos.left + 'px'; }); drag & drop

Slide 40

Slide 40 text

Autocomplete yes I know, the classic example

Slide 41

Slide 41 text

requirements ● filter queries ● throttle requests ● retry (overcome network glitches) ● avoid duplicate requests ● match results to latest query ● abort no longer valid requests

Slide 42

Slide 42 text

keyup => results var keyPress = $('#search').keyupAsObservable(); .keyPress.map((ev) => { return ev.target.value; }) .filter((text) => { return text.length > 3; }) .debounce(500) .distinctUntilChanged() .flatMapLatest(search.retry(3).takeUntil(keyPress)) .map((d) => { return d.response[1]; }) .subscribe(showResults, showError); throttling no duplicate requests filtering match/abort response/results

Slide 43

Slide 43 text

gets even better

Slide 44

Slide 44 text

things we can do ● cancel (can’t do with Promises) ● be lazy until a subscriber subscribes (cold) ● setup datasource on first subscription ● teardown datasource on disposal

Slide 45

Slide 45 text

not convinced yet?

Slide 46

Slide 46 text

observable future ● TC39 proposal to add to ES7 ○ https://github.com/zenparsing/es-observable ● Angular 2 first class support ● ReactJS first class support

Slide 47

Slide 47 text

http://victorsavkin.com/post/108837493941/better-support-for-functional-programming-in

Slide 48

Slide 48 text

Rx in production

Slide 49

Slide 49 text

still... ● steep learning curve ● old habits die hard ● tricky to work with classic MV* ● poor/difficult documentation (is getting better)

Slide 50

Slide 50 text

libraries

Slide 51

Slide 51 text

● Rx.js (port of Reactive Extensions to JS) ● Bacon.js ● Kefir (faster bacon :)

Slide 52

Slide 52 text

origins

Slide 53

Slide 53 text

Microsoft Research ● A Brief Introduction to ActiveVRML - Conan Elliott ● Functional Reactive Animations - Conan Elliott & Paul Hudak

Slide 54

Slide 54 text

resources ● Fran Tutorial - http://conal.net/fran/tutorial.htm ● Simply Reactive - http://conal.net/papers/simply-reactive/ ● Reactive Extensions - https://github.com/Reactive-Extensions/RxJS ● BaconJS - https://baconjs.github.io/ ● Async JavaScript with Reactive Extensions (Jafar Husain) ○ https://www.youtube.com/watch?v=XRYN2xt11Ek ● RxJS at Modern Web UI (Ben Lesh) ○ https://www.youtube.com/watch?v=yk_6eU3Hcwo ● http://www.slideshare.net/stefanmayer13/functional-reactive-programming-with-rxjs ● https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 ● RxJSKoans - https://rxkoans.codeplex.com/ ● RxMarbles - http://rxmarbles.com/ ● Reactive programming and MVC (Aaron Stacy) ○ http://aaronstacy.com/writings/reactive-programming-and-mvc/

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Questions?