Lifecycle
How persistSource hydrates, writes, tears down, and surfaces errors.
persistSource is a lifecycle, not a setItem call. It hydrates (or waits for you to), writes on subscribe, and tears down on destroy().
Phases
- Hydrate — on create, unless
skipHydration. Thenawait persist.rehydrate()when you are ready (route guard, feature flag, after auth). - Write — every eligible
setStateafter hydrate settles; optional trailingthrottleMs. Writes are gated until hydrated. - Destroy —
destroy()flushes a pending throttled write, removes cross-tab listeners, and unregisters from aregistry. Required for non-singleton stores.
const persist = persistStore(store, {
name: "app:prefs:v1",
storage,
skipHydration: true,
});
// later — after the user is known
await persist.rehydrate();
Errors
Failures route to onError with a phase (write / hydrate / migrate / crossTab). Without a sink, the console fallback is dev-only — production is silent by design. Errors never throw into the caller’s setState.
persistStore(store, {
name: "app:prefs:v1",
storage,
onError: (error, { name, phase }) => {
report({ error, name, phase });
},
});
Payload stamps
Stored envelopes carry version (→ migrate), timestamp (→ maxAge), and optional buster. retryWrite shrinks-or-gives-up on quota errors with a write-generation guard so a stale retry never clobbers a newer write.
setOptions limits
setOptions can swap mutable callbacks and knobs (partialize, merge, onError, throttleMs, …). Structural wiring — registry, crossTab, crossTabEventTarget — is fixed at create time. Passing { merge: undefined } does not clear an existing merge.
No hidden magic
- Codecs are sync — async work belongs in the backend.
HydrationSignaldoes not fire an initial notification; pullisHydrated()after subscribe.- Default
mergeis shallow spread (persisted over current) — customize explicitly. skipPersistremovals are immediate (not throttled) so reset-to-default drops the key now.
For the UI gate on async backends, see Hydration. For option recipes, see Recipes → Options.