createStoreMachine
createStoreMachine<
T
,TR
>(initialValue
:T
,transitions
:TR
):StoreMachine
<T
,TR
>
Defined in: store-machine.ts:105
Creates a minimal, event-driven store machine for a single value.
Usage:
// Create a storeconst store = createStoreMachine<number>({ [type]: (...params) => value => replacementValue}, initalValue);// Use the storestore.dispatch(actionType, ...params);store.getState() // Current valuestore.getChange() // Last change event
Type Parameters
Section titled “Type Parameters”Type Parameter | Default type |
---|---|
T | - |
TR extends StoreTransitionRecord <T > | StoreTransitionRecord <T > |
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
initialValue | T | Initial value for the store |
transitions | TR | StoreTransitionRecord mapping event types to updater functions or values |
Returns
Section titled “Returns”StoreMachine
<T
, TR
>
A StoreMachine instance with event-driven update logic and lifecycle hooks
Example
Section titled “Example”import { createStoreMachine } from "./store-machine";
const counter = createStoreMachine<number>({ increment: (value, amount = 1) => value + amount, decrement: (value, amount = 1) => value - amount, set: (value, next) => next, reset: 0,}, 0);
counter.send("increment");counter.send("increment", 5);counter.send("decrement", 2);counter.send("set", 42);counter.send("reset");
console.log(counter.getState()); // 0console.log(counter.getChange()); // { type: "reset", params: [], from: 42, to: 0 }
- StoreMachine
- FactoryMachine