Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.3k
Unified way to "dump" snapshot on every event / entry to state?#5388
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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 ever 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 not |
BetaWas this translation helpful?Give feedback.
All reactions
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
-
Hey thanks for the kind words! You can do 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 that |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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>);}; |
BetaWas this translation helpful?Give feedback.
All reactions
-
This pertains tohttps://stately.ai/docs/xstate-react#createactorcontextlogic It seems like I should actually invoke |
BetaWas this translation helpful?Give feedback.