reactive programming with cycle.js

33
Reactive programming with Cycle.js

Upload: luca-mezzalira

Post on 09-Jan-2017

585 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Reactive programming with cycle.js

Reactive programming with

Cycle.js

Page 2: Reactive programming with cycle.js

HELLO!

I am Luca MezzaliraSolutions Architect @ Massive Interactive

Google Developer Expert on Web Technologies

Community Manager of London JavaScript User Group

Page 3: Reactive programming with cycle.js

AGENDA

▸ Reactive programming

▸ Model - View - Intent

▸ Cycle.js

Page 4: Reactive programming with cycle.js

1.REACTIVE PROGRAMMING

Page 5: Reactive programming with cycle.js

PROGRAMMING PARADIGMSFUNCTIONALIt is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time.

IMPERATIVEIt is a programming paradigm that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates.

REACTIVEIt is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.

Page 6: Reactive programming with cycle.js

Imperative vs Reactive

Page 7: Reactive programming with cycle.js

REACTIVE PROGRAMMING IS PROGRAMMING WITH ASYNCHRONOUS DATA STREAMS

Page 8: Reactive programming with cycle.js

IS REACTIVE PROGRAMMING REALLY SO INTERESTING?

Page 9: Reactive programming with cycle.js

OBSERVER PATTERNThe observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

Page 10: Reactive programming with cycle.js

ITERATOR PATTERNIn object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers

Page 11: Reactive programming with cycle.js

RxJS

Page 12: Reactive programming with cycle.js

“BACK-PRESSURE? WHAT?!Main obstacle when you approach for the first time Reactive Programming is the vocabulary.

My first suggestion is to familiarise with the domain, a lot of concepts become way easier if you understand what they mean before you start to work with them.

streams, hot observables, cold observables, marbles diagram, back-pressure, operators...

Page 13: Reactive programming with cycle.js

STREAMSA stream is a sequence of ongoing events ordered in time. It can emit three different things: a value (of some type), an error, or a "completed" signal.

EVERYTHING CAN BE A STREAM

Page 14: Reactive programming with cycle.js

COLD OBSERVABLESCold observables start running upon subscription: the observable sequence only starts pushing values to the observers when subscribe is called.

Values are also not shared among subscribers.

Page 15: Reactive programming with cycle.js

HOT OBSERVABLESWhen an observer subscribes to a hot observable sequence, it will get all values in the stream that are emitted after it subscribes.

The hot observable sequence is shared among all subscribers, and each subscriber is pushed the next value in the sequence.

Page 16: Reactive programming with cycle.js

OPERATORS▸ Creating Observables▸ Transforming Observables ▸ Filtering Observables▸ Combining Observables▸ Error Handling Operators▸ Mathematical Operators▸ Conditional Operators▸ Connectable Observables

Operators

reactivex.io/documentation/operators.html

Page 17: Reactive programming with cycle.js

MARBLES DIAGRAM

rxmarbles.com

Page 18: Reactive programming with cycle.js

2.MODEL VIEW INTENT

Page 19: Reactive programming with cycle.js

Architectures Timeline

90s

MVP

80s

MVC

2005

MVVM

2013

FLUX

2009

DCI

2015

MVI

Page 20: Reactive programming with cycle.js

VIEW INTENT

MODEL

RENDERER

futurice.com/blog/reactive-mvc-and-the-virtual-dom

Page 21: Reactive programming with cycle.js

MVI RULES▸ A module shouldn’t control

any other module (controller in MVC)

▸ The only shared part between modules are observables

▸ Intent is a component with only one responsibility: It should interpret what the user is trying to do in terms of model updates, and export these "user intentions" as events

Page 22: Reactive programming with cycle.js

VIEWInput: data events from the Model.

Output: a Virtual DOM rendering of the model, and raw user input events (such as clicks, keyboard typing, accelerometer events, etc).

VIEW

RENDERER

Page 23: Reactive programming with cycle.js

INTENTInput: raw user input events from the View.

Output: model-friendly user intention events.

INTENT

Page 24: Reactive programming with cycle.js

MODELInput: user interaction events from the Intent.

Output: data events.

MODEL

Page 25: Reactive programming with cycle.js

MVI PROs▸ Better separation of concern▸ Monodirectional flow▸ Dependency Injection▸ Applications become easier

to test (in particular views)▸ All the states live inside the

Model and the View is state-agnostic

Page 26: Reactive programming with cycle.js

3.Cycle.js

Page 27: Reactive programming with cycle.js

Cycle.jsCycle’s core abstraction is your application as a pure function main() where inputs are read effects (sources) from the external world and outputs (sinks) are write effects to affect the external world.

These side effects in the external world are managed by drivers: plugins that handle DOM effects, HTTP effects, etc.

Page 28: Reactive programming with cycle.js

CORE CONCEPTS▸ Objects▸ Functions▸ Drivers▸ Helpers

Page 29: Reactive programming with cycle.js

DRIVERSDrivers are functions that listen to Observable sinks (their input), perform imperative side effects, and may return Observable sources (their output).

http://cycle.js.org/drivers.html

Cycle.js DOMDriver

Page 30: Reactive programming with cycle.js

Cycle.js in action

Page 31: Reactive programming with cycle.js

thenewstack.io/developers-need-know-mvi-model-view-intent

Page 32: Reactive programming with cycle.js

Why not usingReactive Programming

in any project then?

Page 33: Reactive programming with cycle.js

THANKS EVERYONE

Q&A@lucamezzalira

www.lucamezzalira.com

www.londonjs.uk

[email protected]