Skip to content

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 store
const store = createStoreMachine<number>({
[type]: (...params) => value => replacementValue
}, initalValue);
// Use the store
store.dispatch(actionType, ...params);
store.getState() // Current value
store.getChange() // Last change event
Type ParameterDefault type
T-
TR extends StoreTransitionRecord<T>StoreTransitionRecord<T>
ParameterTypeDescription
initialValueTInitial value for the store
transitionsTRStoreTransitionRecord mapping event types to updater functions or values

StoreMachine<T, TR>

A StoreMachine instance with event-driven update logic and lifecycle hooks

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()); // 0
console.log(counter.getChange()); // { type: "reset", params: [], from: 42, to: 0 }
  • StoreMachine
  • FactoryMachine