Skip to content

@sqlrooms/room-store / CreateRoomStorePersistenceOptions

Type Alias: CreateRoomStorePersistenceOptions<TState, TPersisted, TSnapshot>

CreateRoomStorePersistenceOptions<TState, TPersisted, TSnapshot> = RoomStorePersistenceSnapshotCodec<TPersisted, TSnapshot> & RoomStorePersistenceSnapshotEquivalence<TSnapshot> & object

Options for creating controller-backed persistence for a room store.

This helper composes Zustand persist storage, room-store partialization, and createPersistenceController() so apps can share dirty tracking, autosave, final flush, and hydration saved-snapshot behavior without repeating glue.

Type Declaration

NameTypeDescription
partialize()(state) => TPersistedSelects the portion of full room-store state that should be persisted. When using Zustand's persist middleware, pass the same function to its partialize option so the storage receives the same TPersisted shape.
load()() => Promise<TSnapshot | null>Loads the latest durable snapshot, or null when none exists.
save()(snapshot, metadata?) => Promise<void>Writes a durable snapshot. Metadata carries the save reason, such as setItem, store-change, autosave, or final-flush, for adapters that want observability.
remove()?(name) => Promise<void>Removes the durable snapshot when Zustand clears persistence.
store?StoreApi<TState>Optional Zustand store to observe directly. Provide this when persistence should track store changes independently of Zustand persist's setItem calls.
shouldPersistChange?RoomStorePersistenceChangePredicate<TState>Returns true when a changed partialized snapshot should mark persistence dirty. Use this for host lifecycle guards, such as skipping restore-time updates or failed initialization states. Skipped snapshots are still recorded as observed so they are not saved later by an unrelated state change.
applySnapshot()?(persisted, context) => void | Promise<void>Applies a deserialized snapshot to runtime state during hydrate(). The helper runs this callback while persistence is paused, so restoring state does not mark the controller dirty.
autosaveDelayMs?number | nullDebounce delay for autosaves, or null to disable autosave.
version?numberVersion returned from storage.getItem() for Zustand persist migrations.
now()?() => numberClock used by the underlying controller, mostly useful for tests.
initialState?TStateInitial state to snapshot when binding store immediately. Useful when the host creates persistence inside a Zustand state creator, before store.getState() returns the completed initial state.
markInitialSnapshotSaved?booleanWhether an initially-bound store snapshot should be treated as already saved. Defaults to true, which avoids treating the current runtime state as a user edit immediately after binding.
subscribeReason?PersistenceSaveReasonSave reason used when bindStore() detects a changed partialized snapshot.

Type Parameters

Type ParameterDefault typeDescription
TState-The full room-store state.
TPersistedPartial<TState>The partial state shape written by Zustand persist.
TSnapshotstringThe durable snapshot shape handled by the adapter.