Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Store<State>

A Store keeps an Immutable RootState - any array, tuple or object - which can be changed and monitored for changes to drive an app. Make a new Store by calling createStore with an initialState.

Watching State

Assigning a new Immutable RootState using Store.write notifies Watchers previously subscribed using Store.watch. This mechanism ensures that app logic and renderers can track the latest state.

Editing State

Changes to state are normally 'drafted' by calling Store.edit and passing an editor callback function. In this function you can make changes to state as if it wasn't Immutable, then write will be called on your behalf with the equivalent Immutable result. See Editor for more about drafting.

Alternatively you can construct a new Immutable value yourself, then explicitly call Store.write to update the state.

Immutable State: Motivation

Never modifying the state tree means when the state or a selected branch of the state is the same item as before, it is guaranteed to contain all the same values as before. This guarantee is crucial.

Immutability allows Watchers you write, renderers like React and memoizers like Reselect or React's useMemo() to use 'shallow equality checking'. They can efficiently check when changes to an item should trigger a re-render or recompute - simply whenObject.is(prevItem,nextItem)===false.

Immutability eliminates bugs and race conditions in state-change event handlers. Handlers notified of a change effectively have a snapshot of state. You don't have to handle cases where other code changed the state again before your handler read the data.

Finally, Immutability establishes a basis for advanced debugging techniques such as time-travel debugging since every state change notification includes a momentary snapshot of the app state which can be stored indefinitely.

Note whenever you edit data in the Store, this necessarily operates over the current, not historical state, but this reconciliation is unavoidable and manageable.

Type parameters

Hierarchy

Index

Properties

Properties

edit

edit: (editor: Editor<State>) => Immutable<State>

Type declaration

    • Accepts an Editor function which will be passed a draft of the current state. The function can manipulate the draft state using normal javascript assignments and operations. When it returns, a new Immutable state is passed to write which matches those changes.

      Parameters

      • editor: Editor<State>

        A function to draft the next state

      Returns Immutable<State>

      The resulting new Immutable state.

read

read: () => Immutable<State>

Retrieve the current state.

Type declaration

select

select: <Selected>(selector: Selector<State, Selected>) => Immutable<Selected>

Type declaration

    • Derive some sub-part or computed value from the current state using a Selector. A bound method for convenience that is equivalent to selector(store.read()).

      Type parameters

      • Selected

      Parameters

      • selector: Selector<State, Selected>

        A Selector which will be passed the current state.

      Returns Immutable<Selected>

      The value extracted or computed by the selector.

watch

watch: (watcher: Watcher<Immutable<State>>) => Unwatch

Subscribes watcher to receive notifications.

Type declaration

write

write: (state: Immutable<State>) => Immutable<State>

Store a new state.

Type declaration

Generated using TypeDoc