API

A Store instance exposes the following methods.

register

Registers a new module into the store registry. Returns module's id. Throws an error a module with the same id already exists in registry.

public register<T>(id: string, module: Module<T>): string;
  • id Module's unique identifier in registry. Can be any string, although it is recommended to follow a tree-structure pattern, like /my_app/module_a/module_b.

  • module Module to register.

unregister

Unregisters module with id id from the global modules registry. Throws an error module still has related user-defined combined modules, or if module with id id does not exist.

public unregister(id: string): void;
  • id Id of the module to unregister.

combine

Combines one or several modules to allow subscriptions on that combination. Returns combined module's id. Throws an error if a module with the same id already exists in registry, or if one of the modules' ids does not exist.

public combine<T>(id: string, moduleIds: string[], reducer: Reducer<T>): string;
  • id Combined module's unique identifier in registry. Can be any string, although it is recommended to follow a tree-structure pattern, e.g. /my_app/module_a/module_b.

  • moduleIds Ids of the modules to combine.

  • reducer Transformation function. This function is called with every combined module's state as arguments. For instance: (stateA, stateB, stateC) => ({ a: stateA.prop, b: stateB, c: stateC.propC })

uncombine

Uncombines user-defined combined module with id id. Throws an error if combined module with id id does not exist, if the given id corresponds to a default combined module, or if combined module still has subscriptions.

public uncombine(id: string): void;
  • id Id of the combined module to uncombine.

subscribe

Subscribes to changes on module with id id. Returns the subscription's id, used to unsubscribe handler. Throws an error if module with id id does not exist.

public subscribe<T>(id: string, handler: Subscription<T>): string;
  • id Id of the module to subscribe to.

  • handler Callback to execute each time module notifies changes.

unsubscribe

Unsubscribes from changes on module with id id. Throws an error if module with id id does not exist, or if subscription with id id does not exist on that module.

public unsubscribe(id: string, subscriptionId: string): void;
  • id Id of the module to unsubscribe from.

  • subscriptionId Id of the subscription.

mutate

Performs a state mutation on module with id id. Throws an error if module with id id does not exist or is a combined module, or if mutation's name does not exist on that module.

public mutate<T>(id: string, name: string, data?: T): void;
  • id Id of the module on which to perform mutation.

  • name Name of the mutation to perform.

  • data Additional data to pass to the mutation.

dispatch

Dispatches an asynchronous action to module with id id. Throws an error if module with id id does not exist or is a combined module, or if action's name does not exist on that module.

public async dispatch<T>(id: string, name: string, data?: T): Promise<void>;
  • id Id of the module to dispatch action on.

  • name Name of the action to perform.

  • data Additional data to pass to the action.

use

Applies the given middleware to the store.

public use(middleware: Subscription): void;
  • middleware Middleware to apply to store.

Last updated