Browse documentation

FluxStore

Provides named mutations, asynchronous actions, computed getters, modules and plugins over Flux state.

Table of contents

Category

Value

Import

js
import { FluxStore } from "valyrian.js/flux-store";

Public signature

ts
new FluxStore(options?: StoreOptions): FluxStore

API form

FluxStore is a class. Construct it with state and the named mutations, actions, getters or modules used by the application.

Options and defaults

ts
type StoreOptions = {
  state?: Record<string, unknown> | (() => Record<string, unknown>); // {}
  mutations?: Record<string, (state, ...args) => void>; // {}
  actions?: Record<string, (store, ...args) => unknown>; // {}
  getters?: Record<string, (state, getters, rootState?, rootGetters?) => unknown>; // {}
  modules?: Record<string, StoreOptions>; // {}
  shouldFreeze?: boolean; // true
  namespace?: string;
  rootStore?: FluxStore;
};

Properties and methods

ts
state: Record<string, unknown>
getters: Record<string, unknown>
rootStore: FluxStore | null
namespace: string | null
commit(mutation: string, ...args): void
dispatch(action: string, ...args): Promise<unknown>
on(event: string, listener, namespace?: string): () => void
off(event: string, listener): void
use(plugin, ...options): void
registerModule(namespace: string, module: StoreOptions): void
unregisterModule(namespace: string): void

Events and restrictions

Events include set, delete, beforecommit, commit, beforedispatch, dispatch, getter, plugin, registerModule and unregisterModule. Modules use dot-separated namespaces. With shouldFreeze enabled, writes outside mutations throw. A plugin is applied once per store instance.

Errors

Throws for invalid mutation paths, duplicate namespaces and invalid store functions.