Effect
Effect is a container for async function.
It can be safely used in place of the original async function.
Arguments
params
(Params): parameters passed to effect
Returns
(Promise
)
#
Example#
Effect Methodsuse(handler)
#
Provides a function, which will be called when effect is triggered.
#
Formulae- Set handler
fn
foreffect
- If effect already had an implementation at the time of the call, it will be replaced by a new one
- Hint: current handler can be extracted with
effect.use.getCurrent()
note
You must provide a handler either through .use
method or handler
property in createEffect, otherwise effect will throw with "no handler used in %effect name%" error when effect will be called
Arguments
handler
(Function): Function, that receives the first argument passed to an effect call.
Returns
(Effect): The same effect
#
Examplewatch(watcher)
#
Subscribe to effect calls.
#
Formulae- Call
fn
on eacheffect
call, pass payload ofeffect
as argument tofn
- When
unwatch
is called, stop callingfn
Arguments
watcher
(Watcher): A function that receivespayload
.
Returns
Subscription: Unsubscribe function.
#
Exampleprepend(fn)
#
Creates an event, upon trigger it sends transformed data into the source event. Works kind of like reverse .map
. In case of .prepend
data transforms before the original event occurs and in the case of .map
, data transforms after original event occurred.
#
Formulae- When
event
is triggered, callfn
with payload fromevent
, then triggereffect
with result offn()
Arguments
fn
(Function): A function that receivespayload
, should be pure.
Returns
Event: New event.
map(fn)
#
Creates a new event, which will be called after the original effect is called, applying the result of a fn
as a payload. It is special function which allows you to decompose dataflow, extract or transform data.
#
Formulae- When
first
is triggered, pass payload fromfirst
tofn
- Trigger
second
with the result of thefn()
call as payload
Arguments
fn
(Function): A function that receivespayload
, should be pure.
Returns
Event: New event.
#
Exampleuse.getCurrent()
#
Returns current handler of effect. Useful for testing.
#
Formulae- Returns current handler for
effect
- If no handler was assigned to
effect
, default handler will be returned (that throws an error) - Hint: to set a new handler use
effect.use(handler)
Returns
(Function): Current handler, defined by handler
property or via use
call.
#
Example#
Effect Propertiesnote
You are not supposed to forward to parts of effect (even though it consists of events and stores), since it's a complete entity on its own. This behavior will not be supported
doneData
#
Event, which is triggered with result of the effect execution:
#
FormulaedoneData
is an event, that triggered wheneffect
is successfully resolved withresult
from.done
Important
Do not manually call this event. It is event that depends on effect.
since
effector 20.12.0
Event triggered when handler is resolved.
#
ExamplefailData
#
Event, which is triggered with error thrown by the effect
#
FormulaefailData
is an event, that triggered wheneffect
is rejected witherror
from.fail
Important
Do not manually call this event. It is event that depends on effect.
since
effector 20.12.0
Event triggered when handler is rejected or throws error.
#
Exampledone
#
Event, which is triggered when handler is resolved.
Important
Do not manually call this event. It is event that depends on effect.
#
PropertiesEvent triggered with object of params
and result
:
params
(Params): An argument passed to the effect callresult
(Done): A result of the resolved handler
#
Examplefail
#
Event, which is triggered when handler is rejected or throws error.
Important
Do not manually call this event. It is event that depends on effect.
#
PropertiesEvent triggered with object of params
and error
:
params
(Params): An argument passed to effect callerror
(Fail): An error catched from the handler
#
Examplefinally
#
since
effector 20.0.0
Event, which is triggered when handler is resolved, rejected or throws error.
Important
Do not manually call this event. It is event that depends on effect.
#
PropertiesEvent, which is triggered with object of status
, params
and error
or result
:
status
(string): A status of effect (done
orfail
)params
(Params): An argument passed to effect callerror
(Fail): An error catched from the handlerresult
(Done): A result of the resolved handler
#
Examplepending
#
#
Formulae$store
will update whendone
orfail
are triggered$store
containstrue
value until the effect is resolved or rejected
Important
Do not modify $store
value! It is derived store and should be in predictable state.
#
ExampleIt's a shorthand for common use case
inFlight
#
#
Formulae- Store
$count
will be0
if no calls ofeffect
in pending state, its default state - On each call of
effect
state in$count
store will be increased - When effect resolves to any state(done or fail) state in
$count
store will be decreased
Important
Do not modify $store
value! It is derived store and should be in predictable state.
since
effector 20.11.0
Store which show how many effect calls aren't settled yet. Useful for rate limiting.