Skip to main content

core

Index

Type Aliases

Atom

Atom<T>: () => T & {}

A reactive value which notifies consumers of any changes.

Atoms are functions which returns their current value. To access the current value of an atom, call it.


Type parameters

  • T

AtomOptions

AtomOptions<T>: { equal?: ValueEqualityFn<T>; name?: string; onDestroy?: () => void }

Options passed to the atom creation function.


Type parameters

  • T

Type declaration

  • optionalequal?: ValueEqualityFn<T>

    A comparison function which defines equality for atom values.

  • optionalname?: string

    Atom's name

  • optionalonDestroy?: () => void

    Callback is called when the store is destroyed.

      • (): void
      • Callback is called when the store is destroyed.


        Returns void

AtomSubject

AtomSubject<T>: DestroyableAtom<T> & Readonly<{ error: (error: unknown) => void; next: (value: T) => void; asDestroyable: any }>

An atom that emits values and errors.


Type parameters

  • T

Computation

Computation<T>: () => T

A pure function that returns a value.


Type parameters

  • T

Type declaration

    • (): T
    • Returns T

ComputeOptions

ComputeOptions<T>: { equal?: ValueEqualityFn<T>; name?: string }

Options for compute


Type parameters

  • T

Type declaration

  • optionalequal?: ValueEqualityFn<T>

    A function to determine if two values are equal. Defaults to Object.is.

  • optionalname?: string

    Atom's name

DeclareStoreOptions

DeclareStoreOptions<State, Updates>: Readonly<{ initialState: State; options?: AtomOptions<State>; updates: Updates }>

Options for declaring a store


Type parameters

DestroyableAtom

DestroyableAtom<T>: Atom<T> & Readonly<{ onDestroyed: Signal<void>; asReadonly: any; destroy: any }>

An Atom that can be destroyed


Type parameters

  • T

EffectAction

EffectAction<T, R>: (value: T, context: EffectContext) => R | Promise<R>

Type parameters

  • T
  • R

Type declaration

EffectContext

EffectContext: { cleanup: any }

Type declaration

  • cleanup: function
    • cleanup(callback: () => void): void

    • Parameters

      • callback: () => void

      Returns void

EffectOptions

EffectOptions: { sync?: boolean }

Options for an effect


Type declaration

  • optionalsync?: boolean

EffectSubscription

EffectSubscription<R>: Readonly<{ onDestroy: Signal<void>; onError: Signal<unknown>; onResult: Signal<R>; destroy: any }>

A reactive effect, which can be manually destroyed.


Type parameters

  • R

ScopeTeardown

ScopeTeardown: Unsubscribable | Destroyable | () => unknown

A resource which can be unsubscribed from or destroyed

SharedScope

SharedScope: Omit<Scope, destroy>

SharedScope and Scope types allow to distinct which third-party code can invoke destroy() method.

Signal

Signal<Event>: { [SIGNAL_SYMBOL]: unknown } & ([Event] extends [undefined | void] ? (event?: Event) => void : (event: Event) => void)

Signal is an event emitter. It can be called to notify listeners of events.

@example
// Create the signal
const submitForm = signal<{login: string, password: string}>();

// Call the signal
submitForm({login: 'foo', password: 'bar'});

// Handle signal's events
effect(submitForm, (formData) => {
// Process the formData
});

Type parameters

  • Event

SignalOptions

SignalOptions<TEvent>: { name?: string; onDestroy?: () => void; onEvent?: (event: TEvent) => void; onSubscribe?: () => void; onUnsubscribe?: (isEmpty: boolean) => void; sync?: boolean }

Options passed to the signal creation function.


Type parameters

  • TEvent

Type declaration

  • optionalname?: string

    Signal's name

  • optionalonDestroy?: () => void

    Callback is called when the signal is destroyed.

      • (): void
      • Callback is called when the signal is destroyed.


        Returns void

  • optionalonEvent?: (event: TEvent) => void

    Callback is called at the same time when the signal is called

      • (event: TEvent): void
      • Callback is called at the same time when the signal is called


        Parameters

        • event: TEvent

        Returns void

  • optionalonSubscribe?: () => void

    Callback is called when an effect is subscribed.

      • (): void
      • Callback is called when an effect is subscribed.


        Returns void

  • optionalonUnsubscribe?: (isEmpty: boolean) => void

    Callback is called when an effect is unsubscribed.

      • (isEmpty: boolean): void
      • Callback is called when an effect is unsubscribed.


        Parameters

        • isEmpty: boolean

        Returns void

  • optionalsync?: boolean

    If true, the signal forces usage of "sync" scheduler.

StateMutation

StateMutation<State>: (state: State) => State

A function to update a state.

It is recommended to return a new state or the previous one.

Actually, the function can change the state in place, but it is responsible for a developer to provide comparator function to the store which handles the changes.

For making changes use a currying function to provide arguments:

const addPizzaToCart = (name: string): StateMutation<Array<string>> =>
(state) => ([...state, name]);
@returns

a next state


Type parameters

  • State

Type declaration

    • (state: State): State
    • Parameters

      • state: State

        a previous state

      Returns State

StateUpdates

StateUpdates<State>: Readonly<Record<string, (...args: any[]) => StateMutation<State>>>

A record of factories which create state mutations.


Type parameters

  • State

Store

Store<State, Updates>: WritableAtom<State> & { updates: StoreUpdates<State, Updates> }

Store and updating functions


Type parameters

StoreFactory

StoreFactory<State, Updates>: { initialState: State; updates: Updates }

Factory function for creating a store


Type parameters

Type declaration

    • (initialState?: FactoryStateArg<State>, options?: AtomOptions<State>): Store<State, Updates>
    • new (initialState?: FactoryStateArg<State>, options?: AtomOptions<State>): Store<State, Updates>
    • Parameters

      • optionalinitialState: FactoryStateArg<State>

        Initial state

      • optionaloptions: AtomOptions<State>

        Options for the store

      Returns Store<State, Updates>

    • Parameters

      • optionalinitialState: FactoryStateArg<State>

        Initial state

      • optionaloptions: AtomOptions<State>

        Options for the store

      Returns Store<State, Updates>

  • readonlyinitialState: State

    Initial state

  • readonlyupdates: Updates

    State mutators

StoreUpdate

StoreUpdate<Args>: (...args: Args) => void

Function which changes a state of the store


Type parameters

  • Args: unknown[]

Type declaration

    • (...args: Args): void
    • Parameters

      • rest...args: Args

      Returns void

StoreUpdates

StoreUpdates<State, Updates>: Readonly<{ [ K in keyof Updates ]: StoreUpdate<Parameters<Updates[K]>> }>

Record of store update functions


Type parameters

ValueEqualityFn

ValueEqualityFn<T>: (a: T, b: T) => boolean

A comparison function which can determine if two values are equal.


Type parameters

  • T

Type declaration

    • (a: T, b: T): boolean
    • Parameters

      • a: T
      • b: T

      Returns boolean