Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unified way to "dump" snapshot on every event / entry to state?#5388

Discussion options

Thanks for all the amazing work on this project! I am using a machine to model backend lifecycle flows that are also exposed client side and I need to store my snapshot updates after every state/event change so the machine can be restored when the user wants to interact with it next.

Currently I can do this by defining an action that fires the save request but its tedious and error prone to add it to everon event object in the machine. I have searched the docs and discussions so apologies if I missed it but wondering if there is a better way to define a single "action" that fires for every snapshot.

This seems akin to an observable actor but I need to do this for any actor.

Bonus question: Its not entirely clear to me how to infer when a machine is in 'idle' as in there are no promise actors or invocations running that might produce a new event to auto transition a machine. In my use case, a user might submit an event to a machine that could move the machine 1 to n states forward depending on the logic for each state and I am not sure how to know when the machine is "done" automatically moving through states as the states requiring new user input are notcomplete states but rather the machine is waiting for the next user input event. Relevant to my backend specifically as I do not want to respond to the client till all possible auto transitions are made before showing the client their action is done and the new "resting" state of the machine

You must be logged in to vote

Hey thanks for the kind words!

You can doactor.subscribe(snapshot => {/* … */}).

You can also use the inspect API for more fine-grained inspection:

// either on creationconstactor=createActor(machine,{inspect:(inspEv)=>{ … }});
// or after the factactor.system.inspect(inspEv=>{})

See the inspection docs here:https://stately.ai/docs/inspection.


Re: the bonus question, you can likely check thatObject.keys(snapshot.children).length === 0, which indicates that no child actors are present/active.

Replies: 2 comments 1 reply

Comment options

Hey thanks for the kind words!

You can doactor.subscribe(snapshot => {/* … */}).

You can also use the inspect API for more fine-grained inspection:

// either on creationconstactor=createActor(machine,{inspect:(inspEv)=>{ … }});
// or after the factactor.system.inspect(inspEv=>{})

See the inspection docs here:https://stately.ai/docs/inspection.


Re: the bonus question, you can likely check thatObject.keys(snapshot.children).length === 0, which indicates that no child actors are present/active.

You must be logged in to vote
0 replies
Answer selected byzacharyhansen
Comment options

You're a legend for the quick response this works great. Had one more question for you on the frontend if you have the time - I see how I can provide a machine to my components and override actions and such but I can't quite figure out how to provide that machine with the restored snapshot actor I need it to work with.

constExisitingConnectFlow=({portfolioConnectItem,}:{portfolioConnectItem:PortfolioConnectItemFragment;})=>{const[state,send,actorRef]=useMachine(machine,{snapshot:portfolioConnectItem.persistedSnapshot,// provide persisted state config object here});const[saveSnapshot]=useUpdatePortfolioConnectSnapshotMutation();useEffect(()=>{constsubscription=actorRef.subscribe((snapshot)=>{saveSnapshot({variables:{id:portfolioConnectItem.id,persistedSnapshot:snapshot,},});});returnsubscription.unsubscribe;},[actorRef,portfolioConnectItem.id,saveSnapshot]);return(<ConnectMachineContext.Providerlogic={machine.provide({// <--- not sure how to inject the restored actor from above so all child components referencing this also get the restored actoractions:{saveSnapshot:({ context, self})=>{console.log('hit saveSnapshot',self.getPersistedSnapshot());saveSnapshot({variables:{id:portfolioConnectItem.id,persistedSnapshot:self.getPersistedSnapshot(),},});},},})}><ConnectFlowonPortfolioConnectCreated={()=>{// Already have a portfolioConnectId, no-op}}/></ConnectMachineContext.Provider>);};
You must be logged in to vote
1 reply
@zacharyhansen
Comment options

This pertains tohttps://stately.ai/docs/xstate-react#createactorcontextlogic

It seems like I should actually invokecreateActorContext(machine, { snaphsot }) in a parent component than provide this with my own provider?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@zacharyhansen@davidkpiano

[8]ページ先頭

©2009-2025 Movatter.jp