Glossary
This is a glossary of the core terms in Effector, along with their type signatures. The types are documented using TypeScript notation.
Event
Event is a function you can subscribe to. It can be an intention to change the store, indication of something happening in the application, a command to be executed, aggregated analytics trigger and so on.
Event in api documentation
- createEvent(eventName) creates event
(payload)
callsEvent
with payload- watch(watcher) listens to the event and calls provided
watcher
- map(fn) creates a new event, which will be trigger after the original event was triggered, transforming the payload with provided
fn
- filter({fn}) creates a new event that will tigger only if provided
fn
function returnstrue
- filterMap(fn) creates a new event that will be triggered with the result of
fn
applied to the payload, but only if the result is notundefined
. Use cases: extract value from react's refs; statically typed filters; - prepend(fn) creates a new event that will trigger the original event with a payload transformed by
fn
Store
Store is an object that holds state. There can be multiple stores.
Store in api documentation
- createStore(defaultState) creates a new store
- combine(stores) combines multiple stores into one
- on(event, reducer) calls
reducer
on store whenever an event occurs - map(fn) creates computed store from given one
- reset(...triggers) resets state to default whenever any of the triggers occur
- watch(watcher) registers a
watcher
to be called with the new state when the store is updated - updates is an
event
that triggers when thestore
is updated - defaultState initial state of the given store
Effect
Effect is a container for (possibly async) side effects.
It exposes special events and stores, such as pending
, done
, fail
, finally
, etc...
It can be safely used in place of the original async function.
It returns promise with result of a function call
The only requirement for the function:
- Must have zero or one argument
Effect in api documentation
- createEffect(config) creates an effect
(payload)
starts theEffect
with payload and returns a Promise- use(function) replaces the handler in the effect (can be called multiple times)
- watch(watcher) listens to the effect and calls provided
watcher
when effect starts
Domain
Domain is a namespace for your events, stores and effects.
Domains are notified when events, stores, effects, or nested domains are created via onCreateEvent
, onCreateStore
, onCreateEffect
, onCreateDomain
methods.
It is useful for logging or other side effects.
Domain in api documentation
- createDomain(domainName) creates a new domain
- onCreateEvent(hook) calls the hook when nested
Event
is created - onCreateEffect(hook) calls the hook when nested
Effect
is created - onCreateStore(hook) calls the hook when nested
Store
is created - onCreateDomain(hook) calls the hook when nested
Domain
is created - createEvent(name) creates a domain-bound
Event
- createEffect(name) creates a domain-bound
Effect
- createStore(defaultState) creates a domain-bound
Store
- createDomain(name) creates a nested, domain-bound
Domain
Reducer
Reducer calculates a new state given the previous state and an event's payload. For stores, if reducer returns undefined or the same state (===), then there will be no update for a given store.
Watcher
Watcher is used for side effects. Accepted by event.watch, store.watch and domain.onCreate* hooks. Return value of a watcher is ignored.
Subscription
Function, returned by forward, event.watch, store.watch and some others methods. Used for cancelling a subscription. After first call, subscription will do nothing
Purity
Most of functions in api must not call other events or effects: it's easier to reason about application's data flow when imperative triggers are grouped inside watchers and effect handlers rather than spread across entire business logic.
Correct, imperative:
Correct, declarative:
Incorrect: