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.
- 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.
- isListening() ⇒
boolean
- root(fn) ⇒
*
- sample(fn) ⇒
*
- transaction(fn) ⇒
*
- observable(value) ⇒
function
- computed(observer, value) ⇒
function
- cleanup(fn) ⇒
function
- subscribe(observer) ⇒
function
- on(obs, fn, [seed], [onchanges]) ⇒
function
- unsubscribe(observer)
Returns true if there is an active observer.
Kind: global function
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 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);
});
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 |
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. |
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. |
Run the given function just before the enclosing computation updates or is disposed.
Kind: global function
Param | Type |
---|---|
fn | function |
Subscribe to updates of an observable.
Kind: global function
Param | Type |
---|---|
observer | 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 from an observer.
Kind: global function
Param | Type |
---|---|
observer | function |
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