Store
Store is an object that holds the state value. There can be multiple stores.
Store Methods
map(fn: (state: State, lastState?: T) => T)
Creates a derived store. It will call a provided function with the state, when the original store updates, and will use the result to update the derived store
Formulae
- When
$firststore is updated, callfnwith new state and previous state - Next update
$secondstore with result offn()call and trigger all subscribers
Arguments
fn(Function): Function that receivesstateandlastState?and returns a new state for the derived store
If the function returns an old state or if it returns undefined, the new store will not be updated.
Returns
Store: New store
Example
on(trigger, handler)
Updates state when trigger is triggered by using handler. For each trigger, last installed handler will override previous handlers (useful for dynamic behavior).
Formulae
- When
triggeris triggered, callhandlerwith payload of thetriggerand data of$store - Next update
$storewith result ofhandler()call and trigger all subscribers
Arguments
triggerEvent, Effect or Storehandler(Function): Reducer function that receivesstateandparamsand returns a new state, should be pure. A store cannot hold anundefinedvalue. If a reducer function returnsundefined, the store will not be updated.state: Current state of storeparams: Parameters passed to event call
Returns
Store: Current store
Example
Unsubscribe example
on(triggers[], handler)
Updates state when any from triggers is triggered by using handler.
Formulae
- When
triggerAortriggerBis triggered, callhandlerwith payload of thetriggerAortriggerBand data of$store - Next update
$storewith result ofhandler()call and trigger all subscribers - Any count of triggers can be passed to
triggers
Arguments
triggersarray of Event, Effect or Storehandler(Function): Reducer function that receivesstateandparamsand returns a new state, should be pure. A store cannot hold anundefinedvalue. If a reducer function returnsundefined, the store will not be updated.state: Current state of storepayload: Value passed to event/effect call, or source if it passed as trigger
Returns
Store: Current store
Example
watch(watcher)
Call watcher function each time when store is updated.
If trigger not passed, run watcher on each event that linked to the store.
Formulae
- On initialize and each
$storeupdate, callwatcherwith the new state of$store - When
unwatchis called, stop callingwatcher
Arguments
watcher(Watcher): Watcher function that receives current store state as the first argument
Returns
Subscription: Unsubscribe function
Example
watch(trigger, watcher)
Run watcher only when trigger event triggered.
Formulae
- On each
$storeupdate with passedtrigger, callwatcherwith the new state of$storeand payload fromtrigger - When
unwatchis called, stop callingwatcher
Arguments
triggerEvent, Effect or Store: Trigger, which leads to call ofwatcherwatcher(Function): Function that receives current store state as the first argument and payload of trigger as the second argument.
Returns
Subscription: Unsubscribe function
Example 1
.watch trigger watcher when foo is executed, because foo is explicitly passed to watch.
First argument of watcher is a state value, second is an event value.
Example 2
Here .on(bar, ...) changes the state between foo executions.
But .watch reacts only to foo event
Example 3
Here watch reacts only to incr and decr events, because it is explicitly used in .on calls. But doesn't react to any other event.
Example with Effect
Effect is an Event with 2 additional events: fail and done.
You can subscribe to fail and done events of the effect.
Example with another Store
One store can subscribe to updates of another store.
Output
Example with watcher
Output
reset(...triggers)
Resets store state to the default value.
A state is reset when Event or Effect is called or another Store is changed.
Formulae
- When any unit from
triggerslist is triggered, update$storewith its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
Example
reset(triggersArray)
Resets store state to the default value. An overload for arrays of units, which make reset consistent with merge and store.on(triggers[], fn)
A state is reset when Event or Effect is called or another Store is changed.
Formulae
- When any unit from
triggersArraylist is triggered, update$storewith its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
Example
off(trigger)
- Removes handler for given
trigger, which was installed via \$store.on or \$store.reset - If there was no handler for that
trigger, this method will do nothing
Arguments
Returns
Store: Current store
getState()
Returns current state of store
Returns
(State): Current state of the store
Example
thru(fn)
Call function with the given store and return result as it is.
Formulae
- Call
fnwith$storeas argument - Return result of the
fn()call
Arguments
fn(Function): Function that receivesStoreand returns some value
Returns
(any): Value, returned by fn
Example
Store Properties
updates
Formulae
- When
$storeis changed triggerupdatesevent with the new state
Returns
Event: Event that represents updates of the given store.
Use case: watchers, which will not trigger immediately after creation (unlike store.watch)
shortName
Returns
(string): ID or short name of the store
defaultState
Returns
(State): Default state of the store