nusm axolotlnusm
API Reference

createNusmStore

The complete store factory signature, options, persistence configuration, and return type.

function createNusmStore<TState>(
  initialState: TState,
  options?: CreateNusmStoreOptions<TState>,
): NusmStore<TState>;

Creates a TanStack Store extended with nusm hydration and Devtools identity.

All options are optional. With no adapter, the store remains in memory, ready resolves immediately, and hydration.overall is not_configured. Applications can use this form alongside persisted nusm stores.

Options

Prop

Type

adapter

Enables persistence for this store through a built-in or custom NusmAdapter. A stable storeId is required when an adapter is present. Omit the adapter for in-memory state.

sync

Configures authenticated synchronization for this store. Set false to opt out of a global source, or provide { source, validator, optimistic } to use a store-specific source. Every source requires a validator; store validators override the global validator. See Authenticated synchronization.

persist

Chooses entire or slices. If omitted while an adapter is present, runtime behavior defaults to entire-store persistence; specify the strategy explicitly so intent is visible.

devtools

Enables the framework-neutral event bridge. Use an object to provide a display name and bounded eventLogCap.

onError

Receives adapter, hydration, persistence, external-event, and snapshot failures. Fatal hydration may also reject ready.

storeId

The stable logical identifier used to derive persistence keys, synchronization capabilities, and label Devtools state. Persistence and synchronization require an explicit storeId; Devtools names never provide identity.

Hydration configuration

Prop

Type

merge applies to entire-store hydration. Slice values go through their individual apply functions.

Slice definition

Prop

Type

A slice must own only the selected value represented by its key.

Return value

Prop

Type

Use the inherited state, setState, and subscribe members for ordinary store work. Call await store.dispose() to flush queued persistence and release internal subscriptions. Disposal is asynchronous, idempotent, and returns one shared promise. It marks the store disposed synchronously, so late hydration cannot mutate memory, persistence, or queued events. Every cleanup and flush is attempted. If any fail, the promise rejects once with an AggregateError and reports each failure through onError. A pending ready rejects with AbortError, and hydration states become cancelled.

Global configuration

configureNusm({
  sync: {
    source,
    validator: validateCandidate,
  },
});

This captures the source only for subsequently created stores. A global source auto-enables stores with an explicit storeId; sync: false opts out one store. Unnamed stores remain in memory and are not synchronized.

Lifecycle and Devtools types

type HydrationState =
  | "not_configured"
  | "pending"
  | "hydrated"
  | "discarded"
  | "error"
  | "cancelled";

Prop

Type

Prop

Type

Prop

Type

Example

const store = createNusmStore(
  { locale: "en", sidebarOpen: true },
  {
    adapter: createLocalStorageAdapter(),
    devtools: { name: "UI preferences" },
    onError: console.error,
    persist: {
      strategy: "entire",
      hydrate: {
        validate: (value) => isPreferences(value),
      },
    },
    storeId: "ui-preferences",
    sync: {
      validator: (candidate) =>
        typeof candidate === "object" &&
        candidate !== null &&
        typeof (candidate as { locale?: unknown }).locale === "string" &&
        typeof (candidate as { sidebarOpen?: unknown }).sidebarOpen ===
          "boolean",
    },
  },
);

await store.ready;

On this page