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, some occurence in application, 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 new event, which will be called after the original event is called, applying the result of a fn as a payload
- filter({fn}) creates new event that will receive update only when given
fn
returns true - filterMap(fn) creates new event that will receive value, returned by given
fn
, but only when it returns anything but undefined. Use cases: extract value from react's refs; statically typed filters; - prepend(fn) creates new event that preprocesses payload before calling the original event
Effect
Effect is a container for async function.
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 effect
(payload)
callsEffect
with payload and returns a Promise- use(asyncFunction) injects async function into the effect (can be called multiple times)
- watch(watcher) listens to the effect and calls provided
watcher
when effect starts
Store
Store is an object that holds the state tree. There can be multiple stores.
Store in api documentation
- createStore(defaultState) creates new store
- combine(stores) combines multiple stores into one
- on(event, reducer) calls
reducer
on store when event occurs - map(fn) creates computed store from given one
- reset(...triggers) resets state to default when event occurs
- watch(watcher) calls given
watcher
with current state - updates is
event
for watchstore
changes only - defaultState initial state of given store
Domain
Domain is a namespace for your events, stores and effects.
Domain can subscribe to event, effect, store or nested domain creation with onCreateEvent
, onCreateStore
, onCreateEffect
, onCreateDomain
methods.
It is useful for logging or other side effects.
Domain in api documentation
- createDomain(domainName) creates new domain
- onCreateEvent(hook) calls hook when nested
Event
created - onCreateEffect(hook) calls hook when nested
Effect
created - onCreateStore(hook) calls hook when nested
Store
created - onCreateDomain(hook) calls hook when nested
Domain
created - createEvent(name) is the function that creates
Event
- createEffect(name) is the function that creates
Effect
- createStore(defaultState) is the function that creates
Store
- createDomain(name) creates nested
Domain
Reducer
Reducer calculates a new state given the previous state and an event. For stores, if reducer returns undefined or the same state (===), then there will be no update for 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
Pureness
Most of functions in api mustn't call other events or effects: it's easier to reason about application dataflow when imperative triggers are grouped inside watchers and effect handlers rather than spread across entire business logic.
Correct, imperative:
Correct, declarative:
Incorrect: