"In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change." - Wikipedia
"Reactive programming is programming with asynchronous data streams." - Andre Staltz
var a = 5;
var b = 10;
var c = a + b;
a = 10;
c == 20; // false
var a = 5;
var b = 10;
var c = a + b;
a = 10;
c == 20; // true
Instead of methods and fields, objects have reactions that automatically re-evaluate when the other reactions they depend on have been modified.
Functional Reactive Programming is a variant of Reactive Programming that follows Functional Programming principles such as referential transparency, and seeks to be purely functional.
Why I cannot say FRP but I just didReactiveX is more than an API, it's an idea and a breakthrough in programming. It has inspired several other APIs, frameworks, and even programming languages.
Set of libraries to compose asynchronous and event-based programs using observable collections and Array#extras style composition in JavaScript
RxJS 5 is a ground-up rewrite of RxJS that actually began development when RxJS was in 2.0. This new version of RxJS had three basic goals:
The essential concepts in RxJS which solve async event management are:
map
, filter
, concat
, flatMap
, etc.setTimeout
or requestAnimationFrame
or others.
var observable = Rx.Observable.create(function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
setTimeout(() => {
observer.next(4);
observer.complete();
}, 1000);
});
observable.subscribe(
(x) => console.log('got value ' + x),
(err) => console.error('something wrong occurred: ' + err),
() => console.log('done')
);
Source
Producer - source of values for observable.
const source = new Observable((observer) => {
const socket = new WebSocket('ws://someurl');
socket.addEventListener('message', (e) => observer.next(e));
return () => socket.close();
});
const socket = new WebSocket('ws://someurl');
const source = new Observable((observer) => {
socket.addEventListener('message', (e) => observer.next(e));
});
Like Array.prototype.map(), it passes each source value through a transformation function to get corresponding output values.
Like Array.prototype.filter(), it only emits a value from the source if it passes a criterion function.
It's like reduce, but emits the current accumulation whenever the source emits a value.
Intercepts each emission on the source and runs a function, but returns an output which is identical to the source.
SourceWhenever the source Observable emits a value, it computes a formula using that value plus the latest values from other input Observables, then emits the output of that formula.
Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula. Source
Maps each value to an Observable, then flattens all of these inner Observables using switch Source