[@xstate/store] Version 4#5512
Open
davidkpiano wants to merge 27 commits intomainfrom
Open
Conversation
…ate related types and tests
🦋 Changeset detectedLatest commit: e7acd0c The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added
createAsyncStorecreateAsyncStore(...)creates stores with async transition handlers. Awaitedtransition work must go through
await enq.step(stepId, exec)so the store cansuspend and resume through
snapshot.async.While async work is in progress,
store.getSnapshot().asynccontains executionstate keyed by an internal execution id:
Resolved steps are replayed by step id. When the async transition completes,
the execution entry is removed from
snapshot.async.Async handlers are not supported by
createStore(...)orcreateStoreTransition(...). Move async transitions tocreateAsyncStore(...)and make each awaited operation an
enq.step(...).Standard Schema typing
Store configs now accept
schemasfor type inference from anyStandard Schema compatible library. This can type
context, accepted events, and emitted events.
fromStore(...)also acceptsschemasand can infer context, event, andemitted-event types from schema definitions.
The root package now exports
StandardSchemaV1, async store types, and helpertypes such as
AsyncStoreConfig,AsyncStoreAssigner,AsyncEnqueueObject,StoreSchemas,InferSchemaOutput,InferSchemaPayloadMap,ResolveStoreContext,ResolveStoreEventPayloadMap, andResolveStoreEmittedPayloadMap.Optional schema-declared handlers
When events are declared through
schemas.events, matching handlers inonareoptional. Missing handlers are no-ops, but the event still exists for typing and
store.trigger.Async persistence support
The
@xstate/store/persistextension now persists activesnapshot.asyncstate. A
createAsyncStore(...)created from a persisted snapshot resumesin-progress async work automatically.
Changed
Emitted event declarations moved to
schemas.emittedThe
emitsconfig property has been replaced byschemas.emitted. Emittingevents with
enq.emitis unchanged; only the type declaration moved.const store = createStore({ context: { count: 0 }, - emits: { - increased: (_payload: { by: number }) => {} - }, + schemas: { + emitted: { + increased: z.object({ + by: z.number() + }) + } + }, on: { inc: (context, event: { by: number }, enq) => { enq.emit.increased({ by: event.by }); return { count: context.count + event.by }; } } });Store inspection emits one transition event
store.inspect(...)now emits a single@xstate.transitionevent shape insteadof separate
@xstate.actor,@xstate.event, and@xstate.snapshotevents.store.inspect((inspectionEvent) => { - // '@xstate.actor' | '@xstate.event' | '@xstate.snapshot' + // '@xstate.transition' + inspectionEvent.event; + inspectionEvent.snapshot; });Subscribing to inspection immediately emits the current snapshot as an
@xstate.transitionevent with{ type: '@xstate.init' }. Previously,inspection emitted actor/snapshot events and used the initial snapshot on
subscribe.
store.get()is the readable readStores are now
Readable<StoreSnapshot<TContext>>values. Use:store.getSnapshot()for explicit snapshot access.store.get()when reading the store as a reactive/tracked readable value.The two methods currently return the same snapshot, but they communicate
different intent.
Computed atom getters no longer receive
readComputed atoms now read other atoms directly through
.get(). The previousvalue remains available as the first argument.
store.triggeris concrete when event types are knownFor config-created stores,
store.triggeris now built from known event types.Those event types come from
schemas.eventswhen present, otherwise fromObject.keys(on). Extension events such asreset,undo, andredoare alsoadded when those extensions are applied.
Unknown trigger names no longer exist on those concrete trigger objects. If a
store is created from custom logic without event type metadata,
triggerkeepsthe previous proxy behavior.
Store extensions reserve their internal event names
In development, applying
reset()to a store that already declares aresetevent now throws. Applying
undoRedo()to a store that already declaresundoor
redonow throws.The
persist(...)extension also reserves its internal rehydration event(
__persist.rehydrate).Rename the conflicting application event before applying the extension.
Persistence writes snapshots, not just context
persist(...)still stores context by default, but it now derives the persistedvalue from the full store snapshot so active async executions can be included
when present. The stored shape is still context-based, with an additional
top-level
asyncproperty only while async executions are active. If you usepick, it still receives the context value and controls the persisted contextportion.
clearStorage(store)andflushStorage(store)may now return aPromisewhenthe configured storage adapter is async.
Deleted
@xstate/store/reactand@xstate/store/solidThe framework-specific subpath exports were removed from
@xstate/store. Usethe dedicated framework packages instead.
The
reactandsolidpackage folders are no longer published by@xstate/store, andreact/solid-jsare no longer peer dependencies of@xstate/store.createStoreWithProducercreateStoreWithProducer(...)was removed. UsecreateStore(...)and call yourproducer inside transition handlers.
createStore(context, transitions)The deprecated two-argument
createStore(context, transitions)API was removed.Use the config object form.
undoRedo(config, options)The deprecated config-wrapping form of
undoRedo(...)was removed. Use theextension form with
.with(...).Public
_snapshotaccessStores no longer expose
_snapshot. Usestore.getSnapshot()for explicitsnapshot reads, or
store.get()when reading the store as aReadable.