Browse documentation

createPulseStore

Returns proxied state and named pulse methods that publish committed changes.

Table of contents

Category

Value

Import

js
import { createPulseStore } from "valyrian.js/pulses";

Public signature

ts
createPulseStore<StateType extends Record<string, any>, PulsesType extends Record<string, Pulse<StateType, any>>>(initialState: StateType, pulses: PulsesType & ThisType<PulsesType & PulseContext>): StorePulses<PulsesType> & { state: ProxyState<StateType>; on: (event: string, callback: Function) => void; off: (event: string, callback: Function) => void; }

API form

createPulseStore is a factory. Its declaration returns state as ProxyState<StateType>, the named pulse methods supplied in pulses and pulse event subscriptions.

Returned surface

ts
type Pulse<StateType, Result> = (
  state: StateType,
  ...args: any[]
) => Result | Promise<Result>;
type ProxyState<StateType> = StateType & { [key: string]: any };
type StorePulses<PulsesType> = {
  [Name in keyof PulsesType]: PulsesType[Name] extends (
    state: any,
    ...args: infer Args
  ) => infer Result ? (...args: Args) => Result : never;
};

state: ProxyState<StateType>
...namedPulseMethods
on("pulse", callback: (pulseName: string, args: unknown[]) => void): void
off("pulse", callback): void

// Available as this inside a pulse method
$flush(): void

Runtime write restriction

The TypeScript declaration does not mark state as readonly. At runtime, createPulseStore rejects direct assignment and deletion with an error that requires a pulse. Write through named pulse methods so the store can publish the change.