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 intention to change store, fact about what happening in application, command to be executed, aggregated analytics trigger and so on.

function createEvent<E>(eventName?: string): Event<E>;
type Event<Payload> = {
(payload: Payload): Payload;
watch(watcher: (payload: Payload) => any): Subscription;
map<T>(fn: (payload: Payload) => T): Event<T>;
filter(options: {fn(payload: Payload): boolean}): Event<Payload>;
filterMap<T>(fn: (payload: Payload) => T | void): Event<T>;
prepend<Before>(fn: (params: Before) => Payload): Event<Before>;
shortName: string;
}
  • (payload) calls Event with payload
  • watch(watcher) listens to this event and calls given watcher
  • map(fn)
  • 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 original event
  • shortName is used for debug

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 function call

The only requirement for function:

  • Should have zero or one argument
function createEffect<Params, Done, Fail>(
effectName?: string,
): Effect<Params, Done, Fail>;
type Effect<Params, Done, Fail = Error> = {
(payload: Params): Promise<Done>;
done: Event<{params: Params, result: Done}>;
fail: Event<{params: Params, error: Fail}>;
use: {
(asyncFunction: (params: Params) => Promise<Done>): this,
getCurrent(): (params: Params) => Promise<Done>,
};
watch(watcher: (payload: Params) => any): Subscription;
prepend<Before>(fn: (_: Before) => Params): Event<Before>;
shortName: string;
}

Store

Store is an object that holds the state tree. There can be multiple stores.

function createStore<State>(defaultState: State): Store<State>
function createStoreObject<State: {[key: string]: Store<any> | any}>(
obj: State
): Store<$ObjMap<State, <S>(field: Store<S> | S) => S>>
type Store<State> = {
reset(...triggers: Array<Event<any> | Effect<any, any, any> | Store<any>>): this;
getState(): State;
map<T>(fn: (_: State) => T): Store<T>;
on<E>(
trigger: Event<E> | Effect<E, any, any> | Store<E>,
handler: (state: State, payload: E) => State | void,
): this;
off(trigger: Event<any> | Effect<any, any, any> | Store<any>): void;
watch<E>(
watcher: (state: State, payload: E, type: string) => any,
): Subscription;
watch<E>(
trigger: Event<E> | Effect<E, any, any> | Store<E>,
watcher: (state: State, payload: E) => any,
): Subscription;
thru<U>(fn: (store: Store<State>) => U): U;
shortName: string;
defaultState: State;
updates: Event<State>;
}

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.

function createDomain(domainName?: string): Domain;
type Domain = {
onCreateEvent(hook: (newEvent: Event<unknown>) => any): Subscription;
onCreateEffect(hook: (newEffect: Effect<unknown, unknown, unknown>) => any): Subscription;
onCreateStore(hook: (newStore: Store<unknown>) => any): Subscription;
onCreateDomain(hook: (newDomain: Domain) => any): Subscription;
event<Payload>(name?: string): Event<Payload>;
effect<Params, Done, Fail>(name?: string): Effect<Params, Done, Fail>;
store<State>(defaultState: State): Store<State>;
domain(name?: string): Domain;
}

Reducer

type StoreReducer<State, E> = (state: S, payload: E) => State | void
type EventOrEffectReducer<T, E> = (state: T, payload: E) => T

Reducer calculates a new state given the previous state and an event.

Watcher

type Watcher<T> = (update: T) => any

Watcher is used for side effects

Subscription

type Subscription = {
(): void,
unsubscribe(): void,
}