Persistence
These are the v1 docs. They are not maintained.
This page is a verbatim snapshot of the commiq 1.x documentation, kept for people still running 1.x. It is not corrected and not tested against any release. Several pages contain claims that were wrong when written: examples that do not run as printed, sealStore described as read-only when it was not, handledEvent presented as a working subscription pattern when it never matched, and effects described as swallowing only AbortError when they swallowed everything.
Do not treat anything here as a statement about how commiq 2.x behaves.
What changed in 2.0 and how to upgrade · Current documentation
Site search only indexes the current docs. Use the sidebar to browse these pages.
Persist API
@naikidev/commiq-persist provides state persistence and rehydration for commiq stores. It subscribes to state changes, debounce-writes to storage, and hydrates on initialization via replaceState.
Installation
pnpm add @naikidev/commiq-persistnpm install @naikidev/commiq-persistyarn add @naikidev/commiq-persistbun add @naikidev/commiq-persistBasic Usage
import { createStore } from "@naikidev/commiq";
import { persistStore } from "@naikidev/commiq-persist";
const store = createStore({ count: 0 });
const { destroy, hydrated } = persistStore(store, {
key: "app-state",
});
await hydrated; // state is now loaded from localStorageAsync Storage
Use any storage backend by providing a custom StorageAdapter:
const { destroy, hydrated } = persistStore(store, {
key: "app-state",
storage: {
getItem: async (key) => {
const res = await fetch(`/api/state/${key}`);
return res.ok ? await res.text() : null;
},
setItem: async (key, value) => {
await fetch(`/api/state/${key}`, { method: "PUT", body: value });
},
},
});
await hydrated;persistStore(store, options)
Attaches persistence to a store. Returns a handle to destroy the subscription and a promise that resolves when hydration completes.
Options
| Option | Type | Default | Description |
|---|---|---|---|
key | string | required | Storage key |
storage | StorageAdapter | localStorage | Storage backend |
debounce | number | 300 | Debounce delay in ms |
serialize | (state: S) => string | JSON.stringify | Custom serializer |
deserialize | (raw: string) => S | JSON.parse | Custom deserializer |
Return Value
| Property | Type | Description |
|---|---|---|
destroy | () => void | Closes the stream listener and cancels pending writes |
hydrated | Promise<void> | Resolves when initial hydration is complete |
StorageAdapter
type StorageAdapter = {
getItem(key: string): string | null | Promise<string | null>;
setItem(key: string, value: string): void | Promise<void>;
};Both sync and async implementations are supported. localStorage and sessionStorage conform to this interface natively.
Rehydration Loop Prevention
When persistStore hydrates state, it internally skips writing back to storage. This prevents the hydration → stateChanged → write cycle. No configuration needed.