Reactive Programming

Junghoo Cho

cho@cs.ucla.edu

Summing an Array

let sum = 0;
for (let i = 0; i < a.length; i++) {
    sum += a[i];
}
console.log(sum);

Summing an Array

let sum = 0;
for (let i = 0; i < a.length; i++) {
    sum += a[i];
}
console.log(sum);

Iterable: Example

let sum = 0;
while (!a.end()) {
    sum += a.next();
}
console.log(sum);

Iterable: Example

let sum = 0;
while (!a.end()) {
    sum += a.next();
}
console.log(sum);

Observable: Example

let sum = 0;
a.onNext = (x => { sum += x; });
a.onCompleted = (() => { console.log(sum)); });

Iterable vs Observable

TaskIterableObservable
Consume ItemT next()onNext(T)
Encounter Errorthrow Error(e)onError(e)
Finishend()onCompleted()

Key Terminology

Everything is Observable!

Reactive Programming

Operator

Operator: Transform

Operator: Aggregate

Operator: Buffering

Operator: Throttling

Multi-Way Operators

Operator: Example

Operator: Example

Operator: Example

Operator: Example

Operator: Example

Simple to Complex: Example

Consuming Observable: subscribe

When is Observable Useful?

Declarative Programming

Pure Function

Pure Function

Reactive Program: Properties

→ leads to easy-to-understand, easy-to-optimize, easy-to-maintain program

RxJS

What We Learned