Minor Changes
-
#5479
3b8c68eThanks @davidkpiano! - Addstrategy: 'event'option to thepersistextension. Instead of persisting context snapshots, this persists the event log and replays events on rehydration to reconstruct state. WhenmaxEventsis set, a snapshot checkpoint is automatically saved so that replay starts from the checkpoint rather than initial context, preserving correctness.Also adds
isHydrated(store)helper to check hydration status.const store = createStore({ context: { count: 0 }, on: { inc: (ctx) => ({ count: ctx.count + 1 }) } }).with( persist({ name: 'my-store', strategy: 'event', maxEvents: 100 }) );
-
#5474
e299d40Thanks @davidkpiano! - Addresetstore extension for resetting store context to its initial state via.with(reset()).import { createStore } from '@xstate/store'; import { reset } from '@xstate/store/reset'; const store = createStore({ context: { count: 0, user: null }, on: { inc: (ctx) => ({ ...ctx, count: ctx.count + 1 }), login: (ctx, e: { user: string }) => ({ ...ctx, user: e.user }) } }).with(reset()); store.trigger.inc(); store.trigger.reset(); // resets to { count: 0, user: null }
Supports custom reset logic via
tofor partial resets:.with(reset({ to: (initial, current) => ({ ...initial, user: current.user }) }))
-
#5472
f7c2bebThanks @davidkpiano! - Addpersiststore extension for persisting store context to storage (localStorage, sessionStorage, async adapters, etc.) via.with(persist({ name: 'my-store' })).import { createStore } from '@xstate/store'; import { persist, rehydrateStore, clearStorage, flushStorage, createJSONStorage } from '@xstate/store/persist'; const store = createStore({ context: { count: 0 }, on: { inc: (ctx) => ({ count: ctx.count + 1 }) } }).with(persist({ name: 'my-store' })); // Default storage is localStorage
Features:
- Sync and async storage adapters (localStorage, AsyncStorage, etc.)
pick— persist only selected fieldsversion+migrate— schema versioning and migrationmerge— custom merge strategy on rehydrationthrottle— batched/throttled writesserialize/deserialize— custom serializationfilter— skip persisting for specific eventsskipHydration+rehydrateStore()— manual/async rehydrationclearStorage()— remove persisted dataflushStorage()— force immediate write of pending throttled datacreateJSONStorage()— SSR-safe storage adapter factoryonDone/onErrorcallbacks