Functional Reactive Javascript
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2369643/Rx_Logo_S.png)
Aliaksei Bahachuk
Reactivity is modern, but What is it?
Reactive System
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378368/f019d0d8b3858d471df05b1986666166.png)
Reactive Programming
Programming paradigm oriented around data flows and the propagation of change (wiki)
Excel
be reactive
a := 1;
b := 2;
c := a + b; //c === 3
a := 3; //c === 5
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378439/1.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2374572/stream.gif)
Values that vary over time aka Observable Streams
Principles of FRP
-
Functional programming
-
Clear State
-
Behaviour/signal/cell/stream
-
Event/Property
-
Switching
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2372595/AIimQ8C.jpg)
Streams are everywhere
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378610/logo-578x270.png)
Streams are everywhere
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378601/ng2-logo.png)
Streams are everywhere
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2369643/Rx_Logo_S.png)
Java
JavaScript
.Net
Ruby
Swift
C++
Clojure
Scala
Let's create a bicycle
Let's create a bicycle!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378439/1.png)
Let's create a bicycle!
let ReactiveDeps = {
funcs: new Map(),
vars: new Map(),
activeID: null
};
Let's create a bicycle!
function Merge(fn, args) {
...
function obsFunc() {
ReactiveDeps.activeID = _id;
let res = fn.apply(this, args);
ReactiveDeps.activeID = null;
return res;
};
...
}
Let's create a bicycle!
function Merge(fn, args) {
...
stream = new Stream(obsFunc())
ReactiveDeps.funcs.set(_id, function() {
stream.set(obsFunc());
});
return stream;
}
Let's create a bicycle!
let Stream = class {
...
get() {
let activeID = ReactiveDeps.activeID
if (activeID) {
ReactiveDeps.vars.get(activeID).push(this);
}
return this._value;
}
...
}
Let's create a bicycle!
let Stream = class {
...
set(val) {
this._value = val;
for (let entry of ReactiveDeps.vars) {
if (entry[1].indexOf(this) > -1) {
ReactiveDeps.funcs.get(entry[0])();
}
}
if (this._onChange) {
this._onChange(this._value);
}
}
...
}
Let's create a bicycle!
let Stream = class {
constructor(val) {
this._value = val;
this._onChange = null;
}
...
subscribe(event, fn) {
if (event === 'change') {
this._onChange = fn;
}
}
}
Let's create a bicycle!
let a = new Stream(1);
let b = new Stream(2);
let c = Merge(function(a, b) {
return a.get() + b.get();
}, [a, b]);
c.subscribe('change', function(val) {
document.querySelector('#summ').innerHTML = val;
});
setInterval(function() {
a.set(a.get() + 1);
}, 1000);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378439/1.png)
Let's create a bicycle!
Bicycle is ready!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2370288/old-bike2.jpeg)
Let's improve our bicycle!
Stream Improvements: State and Status
-
isDisposed
-
isCompleted
-
isStopped
-
hasError
-
hasWarnings
-
...
Stream Improvements: Dependencies
Stream Improvements: Operators
-
Map
-
Filter
-
Scan
-
CombineLatest
-
StartWith
-
...
Result of Improvements
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2374945/zmulticlickstream.png)
Result of Improvements
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2383056/sin_top6.jpg)
Bicycle is ready!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2374954/middle_1399958086658.jpg)
Problems(
Complexity
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378713/5fa6b81fbe1146508c79f1b2e486aef2.jpg)
Integration with imperative part of code
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378733/Incompatible-software-10.6-news350.jpg)
Cascade Updating
Cascade Updating
Cascade Updating/Exceptions
Memory
![](https://s3.amazonaws.com/media-p.slid.es/uploads/471017/images/2378808/75db58fb-e4ef-4230-bdad-ffed3db05af1.jpg)
...FRP is so bad?
Problems FRP solves
-
Data Manipulation
-
Events handling
-
Callback Hell
-
Promise Hell
-
Comet/Async Manipulation
-
Race Conditions
-
Complex State
-
Tests
![](https://s3.amazonaws.com/media-p.slid.es/uploads/280009/images/2157336/cat1.png)
Deep Dive
-
FRP
-
RxJS, Bacon, Kefir and others
-
MeteorJS
Questions?
FRP
By Aliaksei Bahachuk
FRP
An overview of FRP
- 1,093