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
$first
store is updated, callfn
with new state and previous state - Next update
$second
store with result offn()
call and trigger all subscribers
Arguments
fn
(Function): Function that receivesstate
andlastState?
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
trigger
is triggered, callhandler
with payload of thetrigger
and data of$store
- Next update
$store
with result ofhandler()
call and trigger all subscribers
Arguments
trigger
Event, Effect or Storehandler
(Function): Reducer function that receivesstate
andparams
and returns a new state, should be pure. A store cannot hold anundefined
value. 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
triggerA
ortriggerB
is triggered, callhandler
with payload of thetriggerA
ortriggerB
and data of$store
- Next update
$store
with result ofhandler()
call and trigger all subscribers - Any count of triggers can be passed to
triggers
Arguments
triggers
array of Event, Effect or Storehandler
(Function): Reducer function that receivesstate
andparams
and returns a new state, should be pure. A store cannot hold anundefined
value. 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
$store
update, callwatcher
with the new state of$store
- When
unwatch
is 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
$store
update with passedtrigger
, callwatcher
with the new state of$store
and payload fromtrigger
- When
unwatch
is called, stop callingwatcher
Arguments
trigger
Event, Effect or Store: Trigger, which leads to call ofwatcher
watcher
(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
triggers
list is triggered, update$store
with 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
triggersArray
list is triggered, update$store
with 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
fn
with$store
as argument - Return result of the
fn()
call
Arguments
fn
(Function): Function that receivesStore
and returns some value
Returns
(any): Value, returned by fn
Example
Store Properties
updates
Formulae
- When
$store
is changed triggerupdates
event 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