Effector

Effector

  • Docs
  • Try
  • API
  • Blog
  • Twitter
  • GitHub

›Other

Introduction

  • Installation
  • Core Concepts
  • Prior Art

Recipes

  • Recipes: Index
  • Usage with TypeScript
  • Migrating to Effector

    • From Redux

    React

    • Example
    • Example: Effects
    • Example: Forms
    • Gate: a bridge between props and store
    • Example: TODO creator
    • Example: Dynamic form schema

    React Native

    • React Native Example

Other

  • Troubleshooting
  • Glossary

FAQ

  • FAQ
Edit

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 an intention to change state.

function createEvent<E>(eventName?: string): Event<E>;
  • createEvent(eventName) creates event
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>;
  • createEffect(effectName) creates effect
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;
}
  • (payload) calls Effect with payload
  • use(asyncFunction) injects async function into effect (can be called multiple times)
  • watch(watcher) listens to this effect and calls given watcher
  • prepend(fn) creates new effect that preprocesses payload before calling original event
  • shortName is used for debug

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>>
  • createStore(defaultState) creates new store
  • createStoreObject(obj) combines multiple stores into one
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>;
}
  • reset(...triggers) resets state to default when event occurs
  • getState() returns current state
  • map(fn) creates computed store from previous state
  • on(event, handler) calls reducer on store when event occurs
  • off(event) disables reducer
  • watch(watcher) calls given watcher when event occurs
  • thru(fn) calls function with this store and return its result
  • shortName is used for debug
  • defaultState is createStore first argument
  • updates is event for watch store changes only

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;
  • createDomain(domainName) creates new 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;
}
  • 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
  • event(name) is the function that creates Event described above.
  • effect(name) is the function that creates Effect described above.
  • store(defaultState) is the function that creates Store described above.
  • domain(name) creates nested 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,
}
← TroubleshootingFAQ →
  • Event
  • Effect
  • Store
  • Domain
  • Reducer
  • Watcher
  • Subscription
Effector
Docs
Getting StartedAPI Reference
Community
User ShowcaseStack OverflowGitterTwitter
More
GitHubStar
Copyright © 2019 zerobias