Reactive

ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming

Iterator pattern

Iterator: Sequentially access the elements of a collection without knowing the inner workings of the collection.

//ES 6

var iterator = ["apple", "oranges", "banana"];        
<-:undefined   

var fruits = iterator[Symbol.iterator]();
<:-undefined

> fruits
Array Iterator {}
ƒ next()
Symbol(Symbol.toStringTag)
"Array Iterator"

> fruits.next()
<:-{value: "apple", done: false}

> fruits.next()
<:-{value: "oranges", done: false}

>fruits.next()
<:-{value: "banana", done: false}

>fruits.next()
<:-{value: undefined, done: true}

Observer pattern

Observer: A way of notifying change to a number of classes to ensure consistency between the classes.

Let's try to combine both patterns

Try out: jsbin

Observables:

Observables helps in modeling

  • Events

  • Async Requests

  • Animations

We can use Observables as replacement for

  • Dom Events

  • setIntervals

  • XMLHttpRequests

  • WebSockets

  • Node Streams

  • Service Workers

Last updated