Reactive Extensions for JavaScript

What is Reactive Programming?

Reactive Programming is programming with (asynchronous) data streams

Anything can be a stream:

  • variables
     
  • events
     
  • data structures
     
  • etc

Anything can be a stream:

  • variables
     
  • events
     
  • data structures
     
  • etc

Pure Functions

Pure Functions

  • The function always evaluates the same result value given the same argument value(s)
     
  • Evaluation of the result does not cause any side effects, such as mutation of mutable objects or output to I/O devices.

Monads

Monads

  • come from Category Theory
     
  • encapsulate values of particular data type
     
  • join simple components in robust ways
     
  • are used to build pipelines that process data in a series of steps 
     
  • contain additional processing rules for each step

Monads in JS (1)

function unit(value) {}
//returns a monad that contains the value

function bind(monad, function (value)) {}
//returns a monad containing the result of the function 
//applied to the value of the monad

Monad Laws

  • Left identity: Creating a monad with 'a' and binding it with 'f' is the same as calling 'f(a)'
                            bind(unit(x), f) ==== f(x)
     
  • Right identity: Binding the unit function on a monad returns the same monad
                    bind(monad, unit) ==== monad
     
  • Associativity
          bind(bind(m, f), g) ==== bind(m, x ⇒ bind(f(x), g))

Container

function _Container(val) {
    this._val = val;
}

_Container.prototype.map = function(f) {
    return Container(f(this._val));
};

var Container = function(val) {
    return new _Container(val);
};

Creating Observables

Observable Operators

Subjects

Hot & Cold Observables

Reactive Extensions for JS

By idakiev

Reactive Extensions for JS

  • 405