sample
This method can be used for linking two nodes, resulting the third one, which will fire only upon clock
node trigger.
Quite a common case, when you need to handle some event with some store's state. Instead of using store.getState()
, which may cause race conditions and inconsistency of state, it is more suitable to use sample
method.
#
FormulaeWhen clock
is triggered, read the value from source
and trigger target
with it.
- If the
clock
is not passed, sample will be trigged on everysource
update. - If the
fn
is passed, pass value fromsource
through before passing totarget
- If the
target
is not passed, create it and return fromsample()
#
Schematarget
#
Type of the created If target
is not passed to sample()
call, it will be created internally. The type of the unit is described in the table below:
clock\source | Store | Event | Effect |
---|---|---|---|
Store | Store | Event | Event |
Event | Event | Event | Event |
Effect | Event | Event | Event |
How to read it:
- You need to know type of the
source
, it is a column - Type of the
clock
in the rows - Match the column and the row
For example:
sample({clock?, source, fn?, target?, greedy?})
#
Arguments
params
(Object): Configuration object
clock?
: Unit or array of units- If event or effect: trigger
target
upon event or effect is called - If store: trigger
target
upon store is updated - If array of units: trigger
target
upon any given unit is called or updated. Shorthand for inline merge call - If not passed:
source
is used asclock
- If event or effect: trigger
source?
: Unit or object/array with stores- If event or effect: take last invocation argument value. That event or effect must be invoked at least once
- If store: take current state of given store
- If array or object with stores: take values from given stores combined to object or array. Shorthand for inline combine call
- If not passed:
clock
is used assource
target?
: Unit or array of units- If event or effect: call given event or effect upon
clock
is triggered - If store: update given store upon
clock
is triggered - If array of units: trigger every given unit upon
clock
is triggered - If not passed: new unit will be created under the hood and will be returned as result of the
sample()
call. Type of created target is described in table beyond
- If event or effect: call given event or effect upon
fn?
((sourceData, clockData) => result): Combinator function, which will transform data fromsource
andclock
before passing it totarget
, should be pure. If not passed, data fromsource
will be passed totarget
as it isgreedy?
(boolean) Modifier defines whether sampler will wait for resolving calculation result, and will batch all updates, resulting only one trigger, or will be triggered upon every linked node invocation, e.g. ifgreedy
istrue
,sampler
will fire on trigger of every node, linked to clock, whereasnon-greedy sampler(greedy: false)
will fire only upon the last linked node trigger
note
Array of units in target are supported since effector 21.8.0
Returns
(Event | Store) - Unit, which fires/updates upon clock
is trigged, if source
is not passed.
The type of returned unit depends on the types of clock and source..
#
Examplesample(sourceUnit, clockUnit, fn?)
#
It is just another form of the sample
invocation, with the same sense.
Arguments
sourceUnit
: Source unit- If event or effect. Take last invocation argument value. That event or effect must be invoked at least once
- If store. Take current store`s state
clockUnit
: Clock unit. If not passed,source
is used as clock- If event or effect. Trigger sampled unit, upon event or effect is called
- If store. Trigger sampled unit, upon store is updated
fn?
((sourceData, clockData) => result): Optional combinator function, should be pure. Since, this handler is supposed to organize data flow, you should avoid declaring side-effects here. It's more appropriate to place it inwatch
method for sampled node.
Returns
(Event | Store) - Unit, which fires/updates upon clock
is trigged, if source
is not passed.
The type of returned unit depends on the types of clock and source..
#
Examplesample({name?})
#
since
effector 20.4.0
Every unit in effector may have a name.
You now can name sampled entities in the same manner as basic ones.
sample({ source })
#
Objects and arrays of Store in #
Object of storessince
effector 20.8.0
sample
can be called with object of Store as source
:
#
Array of storessince
effector 20.8.0
sample
can be called with array of Store as source
:
#
Support array in clocksince
effector 21.2.0
Support for sample clock field which acts like a merge call