Store
Extends:
Container for application state and logic.
This class is abstract and cannot be instantiated.
Implementing
A store:
Must have instance method
getInitialState
with signature:function(): Any<S>
Must have either one of:
static method
reducer
with signature:function(state: Any<S>, message: Message): Any<S>
static property
reducers
with signature:{[string]: function(state: Any<S>, message: Message): Any<S>}
Single reducer variant:
let logger = new class extends Store {
getInitialState() { return [] }
static reducer(state, {type, value}) {
if (type === "add") {}
return state.concat([value]);
}
}
}
Object mapping Message#type to reducers:
let logger = new class extends Store {
getInitialState() { return [] }
static reducers = {
add(state, {value}) {
return state.concat([value])
}
}
}
Note: you can also create a store with createStore.
Example:
let counter = new class extends Store {
getInitialState() { return 0 }
static reducers = {
inc(state, {amount}) { return state + amount; },
dec(state, {amount}) { return state - amount; }
}
}
function increment(amount=1) {
counter.send({type: "inc", amount})
}
function decrement(amount=1) {
counter.send({type: "dec", amount})
}
counter.subscribe(n => console.log("Counter value:", n));
increment(); //> Counter value: 1
increment(); //> Counter value: 2
increment(2); //> Counter value: 4
decrement(12); //> Counter value: -8
Constructor Summary
Public Constructor | ||
public |
|
Method Summary
Public Methods | ||
public abstract |
|
|
public |
Send a message. |
Inherited Summary
From class Observable | ||
public |
Invoke each registered listener with |
|
public |
filter(predicate: function(value: Any): boolean): Observable |
|
public |
hasSubscribed(listener: function): boolean True if |
|
public |
map(mapper: function(value: Any): Any): Observable |
|
public |
|
|
public |
subscribe(listener: function): Subscription Register listener |
|
public |
unsubscribe(listener: function) Unregister listener |
From class ObservableState | ||
public |
Same as Observable#emit, but sets
current state to |
|
public |
filter(predicate: function(value: Any): boolean): ObservableState Same as Observable#filter, but returns ObservableState |
|
public |
Return current state or asynchronously invoke callback |
|
public |
map(mapper: function(value: Any): Any): ObservableState Same as Observable#map, but returns ObservableState |
|
public |
Same as Observable#reduce, but starts with current state reduced |