persist
Core @stainless-code/persist concepts — persistSource, PersistOptions, PersistApi, hydration gate, throttle, cross-tab, migrate. Load before framework hydration skills or when wiring a custom PersistableSource.
- License 许可
- MIT
- 最近更新
- 2026-11-01
- Stars
- ★ 5
- Forks
- 0
01 前言 Front Matter
- name
- persist
- license
- MIT
- sources
- [ "stainless-code/persist:src/core/persist-core.ts", "stainless-code/persist:docs/architecture.md", "stainless-code/persist:README.md" ]
- metadata
- { "type": "core", "library": "@stainless-code/persist", "library_version": "0.4.1" }
- description
- Core @stainless-code/persist concepts — persistSource, PersistOptions, PersistApi, hydration gate, throttle, cross-tab, migrate. Load before framework hydration skills or when wiring a custom PersistableSource.
03 SKILL.md 正文
Persist
Zero-dep persistSource(source, options) owns hydrate → subscribe → write. First-party ./sources/* adapters map library APIs onto PersistableSource (getState / setState / subscribe) and may add opinions (default merge, reducer wraps, assign vs $patch).
When to use this skill
- Custom store shape (no
persist-*adapter). - Shared Options /
PersistApisemantics across adapters. - Before
react-persist(and other framework hydration skills).
Library-specific wiring → the matching persist-* composition skill.
Minimal custom source
import { createJSONStorage, persistSource } from "@stainless-code/persist";
const persist = persistSource(
{
getState: () => state,
setState: (updater) => {
state = updater(state);
},
subscribe: (listener) => {
listeners.add(listener);
return { unsubscribe: () => listeners.delete(listener) };
},
},
{
name: "app:custom:v1",
storage: createJSONStorage(() => localStorage),
},
);
Contracts agents must not invent
- Hydration write gate —
setStatebefore hydrate settles does not clobber storage. Don't double-gate manually. throttleMs— trailing-only; first write waits out the window.destroy()flushes pending writes.maxAge— opt-in; prefs should not silently expire.instanceof Promiseon reads — not thenable duck-typing.PersistDecodeRethrowError— decode errors that must rethrow / skipclearCorrupt(e.g. wrong sync/async schema lane).migrate— called once with(state, fromVersion); multi-step chains →createMigrationChain.- Teardown —
persist.destroy()for non-singleton lifetimes.
PersistApi (keep the reference)
rehydrate(), hasHydrated(), onHydrate, onFinishHydration, setOptions, clearStorage, getOptions, destroy.
Hydration signal for UI
import { toHydrationSignal } from "@stainless-code/persist";
export const hydration = toHydrationSignal(persist);
// frameworks/react → useHydrated(hydration) — see react-persist
Backend × codec (short)
| State | Backend | Helper / codec |
|---|---|---|
| JSON-able | localStorage |
createJSONStorage |
Set/Map/Date |
localStorage |
seroval codec |
| Structured clone | IndexedDB | identityCodec |
See also: persist-* for sources / codecs / backends / transport; *-persist for UI frameworks.
04 同仓库更多 Skills
vue-persist
Gate Vue 3 UI on Persist hydration with useHydrated (shallowRef). Use in setup/effectScope when avoiding flash of default state on async backends.
svelte-store-persist
Gate Svelte store-based UI on Persist hydration with hydratedStore (Readable). Use for Svelte 3–4 or Svelte 5 apps that still use stores; runes UI → svelte-persist.
svelte-persist
Gate Svelte 5 runes UI on Persist hydration with hydratedRune (.current). Use for Svelte 5; for stores / Svelte 3–4 use svelte-store-persist.
solid-persist
Gate Solid UI on Persist hydration with useHydrated (Accessor). Use when avoiding flash of default state on async backends; read hydrated() in reactive scopes.
react-persist
Gate React UI on Persist hydration with useHydrated + toHydrationSignal. Use when avoiding flash of default state on async backends (IndexedDB), SSR snapshot true, or wiring HydrationSignal into React 18/19.
preact-persist
Gate Preact UI on Persist hydration with useHydrated (preact/compat useSyncExternalStore). Use when avoiding flash of default state on async backends or wiring HydrationSignal into Preact.