Skip to content

Latest commit

 

History

History
215 lines (140 loc) · 5.2 KB

observable.md

File metadata and controls

215 lines (140 loc) · 5.2 KB

Sinuous Observable

Sinuous Observable is a tiny reactive library. It shares the core features of S.js to be the engine driving the reactive dom operations in Sinuous.

Features

  • Automatic updates: when an observable changes, any computation that read the old value will re-run.
  • Automatic disposals: any child computations are automatically disposed when the parent is re-run.

API

Functions

isListening() ⇒ boolean

Returns true if there is an active observer.

Kind: global function


root(fn) ⇒ *

Creates a root and executes the passed function that can contain computations. The executed function receives an unsubscribe argument which can be called to unsubscribe all inner computations.

Kind: global function

Param Type
fn function

sample(fn) ⇒ *

Sample the current value of an observable but don't create a dependency on it.

Kind: global function

Param Type
fn function

Example

computed(() => {
  if (foo()) bar(sample(bar) + 1);
});

transaction(fn) ⇒ *

Creates a transaction in which an observable can be set multiple times but only trigger a computation once.

Kind: global function

Param Type
fn function

observable(value) ⇒ function

Creates a new observable, returns a function which can be used to get the observable's value by calling the function without any arguments and set the value by passing one argument of any type.

Kind: global function

Param Type Description
value * Initial value.

computed(observer, value) ⇒ function

Creates a new computation which runs when defined and automatically re-runs when any of the used observable's values are set.

Kind: global function
Returns: function - Computation which can be used in other computations.

Param Type Description
observer function
value * Seed value.

cleanup(fn) ⇒ function

Run the given function just before the enclosing computation updates or is disposed.

Kind: global function

Param Type
fn function

subscribe(observer) ⇒ function

Subscribe to updates of an observable.

Kind: global function

Param Type
observer function

on(obs, fn, [seed], [onchanges]) ⇒ function

Statically declare a computation's dependencies.

Kind: global function
Returns: function - Computation which can be used in other computations.

Param Type Description
obs function | Array
fn function Callback function.
[seed] * Seed value.
[onchanges] boolean If true the initial run is skipped.

unsubscribe(observer)

Unsubscribe from an observer.

Kind: global function

Param Type
observer function

Example

import observable, { S } from 'sinuous/observable';

var order = '',
  a = observable(0),
  b = S(function () {
    order += 'b';
    return a() + 1;
  }),
  c = S(function () {
    order += 'c';
    return b() || d();
  }),
  d = S(function () {
    order += 'd';
    return a() + 10;
  });

console.log(order); // bcd

order = '';
a(-1);

console.log(order); // bcd
console.log(c()); // 9

order = '';
a(0);

console.log(order); // bcd
console.log(c()); // 1