-
Hi I have a machine A that is invoked by machine B, and I need to mark the final state in A to trigger the onDone in machine B. However, if I also want to use machine A standalone elsewhere, once it reaches the final state I can't re-run the machine (e.g. by calling a reset event) because it has reached the final state. I want to avoid duplicating the machine A for these two usage cases (because IRL machine A is large and would otherwise be identical), so is there a way to use a machine as both an invoked machine and standalone please? See example: https://codesandbox.io/s/suspicious-meadow-yb2du Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, I think there are a few solutions to this, depending on your precise use case. I can think of 2 right now:
|
Beta Was this translation helpful? Give feedback.
-
Hi @f-elix, thanks for your suggestions. Since I found no good solution I now only invoke machine A from another machine, and never use it standalone. This has only required a few 'wrapper' machines so not too bad a work around. Suggestion 1 is an interesting workaround, and I might try that instead of writing a wrapper machine next time. Suggestion 2 doesn't suit my situation but good to bear in mind for other situations. |
Beta Was this translation helpful? Give feedback.
Hi,
I think there are a few solutions to this, depending on your precise use case. I can think of 2 right now:
You don't use machine A as a standalone machine, instead you can invoke it on machine B and forward the events from B (parent) to A (child). This will allow you to communicate a 'DONE' event from machine A with the
sendParent()
action (https://xstate.js.org/docs/guides/communication.html#invoking-machines) when entering state2, instead of having the state be final. You can then send a 'Reset' event to machine B, which will forward it to machine A.You reverse the roles and instead invoke machine B from machine A, and communicate your result to machine B by using the
send()
ac…