Skip to content

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 ​

FeatureDescriptionPage
StateRead, write, subscribe to stateState
ActionsNamed synchronous mutationsActions
Selectors & ComputedDerived state with cachingSelectors & Computed
BatchGroup multiple updates into one notificationBatch
TransactionsAsync operations with optimistic UI and rollbackTransactions
Undo / RedoState history navigationUndo / Redo
Time TravelRecord and replay state changesTime Travel
Middleware PipelinesComposable middleware with next() patternMiddleware 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.

Released under the MIT License.