Slide 1

Slide 1 text

Thinking Reactive in

Slide 2

Slide 2 text

Ivan Jovanovic Senior software engineer @ Welltok ivanjov.com @ivanjov96

Slide 3

Slide 3 text

What is Reactive programming?

Slide 4

Slide 4 text

–André Staltz “Reactive programming is programming with asynchronous data streams.”

Slide 5

Slide 5 text

What is Data Stream?

Slide 6

Slide 6 text

“Data stream is an endless sequence of digital signals (data packets)”

Slide 7

Slide 7 text

Data stream 10 20 30 40 50 60

Slide 8

Slide 8 text

In JavaScript, Reactive programming is abstraction of all async processes into the data streams

Slide 9

Slide 9 text

buffer(clickStream.throttle(500ms))

Slide 10

Slide 10 text

map(‘get size of the buffer’) 1 2 3

Slide 11

Slide 11 text

filter(number >= 2) 2 3 1 2 3

Slide 12

Slide 12 text

Why do I need this reactive thing?

Slide 13

Slide 13 text

• External service calls • Better performance • Highly testable • Abstraction over all async processes • Predictable code • Extendable code

Slide 14

Slide 14 text

Observable Observable is official terminology for the stream

Slide 15

Slide 15 text

Reactive programming in JS • RxJS - the most popular, biggest • MostJS - best performance • Bacon.js • XStream - the smallest

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

• Data flow • Side effects • Functional programming React cons

Slide 18

Slide 18 text

+

Slide 19

Slide 19 text

+ +

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

André Staltz Creator of Cycle.js

Slide 22

Slide 22 text

–Cycle.js official website “A functional and reactive JavaScript framework for predictable code”

Slide 23

Slide 23 text

• Data flow • Side effects • Functional programming Cycle.js pros

Slide 24

Slide 24 text

Features • Functional (just write pure functions) • Reactive • Testable • Composable • Explicit dataflow • Made for large codebases

Slide 25

Slide 25 text

Image taken from cycle.js.org

Slide 26

Slide 26 text

run(app, drivers);

Slide 27

Slide 27 text

Drivers - side effects • HTTP • DOM • WebSockets • Local storage driver • HTML5 Notifications driver • …

Slide 28

Slide 28 text

Cycle.js official packages • DOM - collection of drivers that work with DOM; it has DOM driver and HTML driver, based on snabdom virtual DOM library • History - driver for History API • HTTP - driver for HTTP requests, based on superagent • Isolate - function for making scoped dataflow components • Most-run - `run` function for apps made with `most` • Run - `run` function for apps made with `xstream` • RxJS-run - `run` function for apps made with `rxjs`

Slide 29

Slide 29 text

Model-View-Intent

Slide 30

Slide 30 text

Simple counter app

Slide 31

Slide 31 text

import xs from 'xstream'; import { run } from '@cycle/run'; import { div, button, p, makeDOMDriver } from '@cycle/dom'; function main(sources) { const action$ = xs.merge( sources.DOM.select('.decrement').events('click').map(ev => -1), sources.DOM.select('.increment').events('click').map(ev => +1) ); const count$ = action$.fold((acc, x) => acc + x, 0); const vdom$ = count$.map(count => div([ button('.decrement', 'Decrement'), button('.increment', 'Increment'), p('Counter: ' + count) ]) ); return { DOM: vdom$, }; } run(main, { DOM: makeDOMDriver('#main') });

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

function main(sources) { const action$ = xs.merge( sources.DOM.select('.decrement').events('click').map(ev => -1), sources.DOM.select('.increment').events('click').map(ev => +1) ); const count$ = action$.fold((acc, x) => acc + x, 0); const vdom$ = count$.map(count => div([ button('.decrement', 'Decrement'), button('.increment', 'Increment'), p('Counter: ' + count) ]) ); return { DOM: vdom$, }; }

Slide 34

Slide 34 text

Intent const action$ = xs.merge( sources.DOM.select('.decrement').events('click').map(ev => -1), sources.DOM.select('.increment').events('click').map(ev => +1) );

Slide 35

Slide 35 text

Model const count$ = action$.fold((acc, x) => acc + x, 0);

Slide 36

Slide 36 text

View const vdom$ = count$.map(count => div([ button('.decrement', 'Decrement'), button('.increment', 'Increment'), p('Counter: ' + count) ]) );

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

View + CSS const style = { backgroundColor: 'blue', width: '60px', height: '60px' } const vdom$ = count$.map(count => div([ button('.decrement', { style }, ’Decrement’), button('.increment', { style }, ’Increment’), p('Counter: ' + count) ]) );

Slide 39

Slide 39 text

Handling HTTP requests

Slide 40

Slide 40 text

import { makeHTTPDriver } from ‘@cycle/http'; function main(sources) { const request$ = xs.periodic(1000) .mapTo({ url: 'http://localhost:3000', category: 'api', }); const vdom$ = sources.HTTP.select('api') .flatten() .map(res => res.body) .startWith({ response: ‘’ }) .map(result => div([ h2('.label', `Response from the server: ${result.response}`) ]) ); return { DOM: vdom$, HTTP: request$ }; } run(main, { DOM: makeDOMDriver('#main'), HTTP: makeHTTPDriver() });

Slide 41

Slide 41 text

function main(sources) { const request$ = xs.periodic(1000) .mapTo({ url: 'http://localhost:3000', category: 'api', }); const vdom$ = sources.HTTP.select('api') .flatten() .map(res => res.body) .startWith({ response: ‘’ }) .map(result => div([ h2('.label', `Response: ${result.response}`) ]) ); return { DOM: vdom$, HTTP: request$ }; }

Slide 42

Slide 42 text

const request$ = xs.periodic(1000) .mapTo({ url: 'http://localhost:3000', category: 'api', });

Slide 43

Slide 43 text

const vdom$ = sources.HTTP.select('api') .flatten() .map(res => res.body) .startWith({ response: '' }) .map(result => div([ h2('.label', `Response: ${result.response}`) ]) );

Slide 44

Slide 44 text

Working with components

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

import isolate from '@cycle/isolate'; const sources = {DOM: sources.DOM}; const childComponent = isolate(Component)(sources); const childComponentVDom$ = childComponent.DOM; const childComponentValue$ = childComponent.value;

Slide 47

Slide 47 text

Cycle.js Cons • Community • Learning new paradigm • Some apps don’t need to be reactive

Slide 48

Slide 48 text

Conclusion • Old paradigm with the new usage • Not for everyone • Perfect for real time apps that require high performance • Testable, predictable code • One interface for all async processes

Slide 49

Slide 49 text

Next? https://cycle.js.org/

Slide 50

Slide 50 text

Thank you Blog ivanjov.com Twitter @ivanjov96