Core API
The core module provides the foundation for all StateMesh features. It includes state management, actions, selectors, computed values, transactions, undo/redo, time travel, and middleware pipelines.
Import
ts
import { createMesh } from "statemesh-core";All core APIs are available from the main statemesh-core package.
What's Included
| Feature | Description | Page |
|---|---|---|
| State | Read, write, subscribe to state | State |
| Actions | Named synchronous mutations | Actions |
| Selectors & Computed | Derived state with caching | Selectors & Computed |
| Batch | Group multiple updates into one notification | Batch |
| Transactions | Async operations with optimistic UI and rollback | Transactions |
| Undo / Redo | State history navigation | Undo / Redo |
| Time Travel | Record and replay state changes | Time Travel |
| Middleware Pipelines | Composable middleware with next() pattern | Middleware Pipelines |
Mesh Instance
The mesh instance returned by createMesh is the central API. It provides methods for all operations:
ts
const mesh = createMesh({
name: "my-app", // Optional name (used in DevTools)
state: { /* ... */ }, // Initial state
undo: { maxHistory: 50 }, // Optional: enable undo/redo
timeTravel: { maxEntries: 1000 } // Optional: enable time travel
});Lifecycle
ts
// Create
const mesh = createMesh({ state: { /* ... */ } });
// Use
mesh.getState();
mesh.setState(newState);
mesh.setPath("key", value);
// Destroy (cleanup all subscriptions, plugins, timers)
mesh.destroy();WARNING
Always call mesh.destroy() when the mesh is no longer needed (e.g., in SSR, tests, or when unmounting a feature). This prevents memory leaks from subscriptions, timers, and plugin resources.
