Replies: 1 comment 1 reply
-
Perhaps you can achieve this using a factory method for the machine. Something like... const makeActor = (input) => {
return setup({
initial: determineInitialState(input),
// ...
}).createMachine({
states: {
// ...
}
})
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Right now the initial starting point of a machine can only be setup statically. I propose allowing setting up the initial state dynamically via a callback that takes the machine's
input
as parameter, just as you can do withContext
:I understand this might go against some principles of FSMs but the use cases are multiple and nothing super edge case
Example use case: A multi-step wizard that can be resumed
Imagine you are building a multi-step wizard with many steps and branching paths, xstate is a great option since you can model each step as a discrete state, save entered data into context and use guards and transitions to move between the steps
Now lets say you want to periodically save data to a database so that if the wizard is quit in the middle, the user can then resume where they left off. With a dynamic initial state this becomes incredibly easy, its just a matter of gathering the persisted data, entering it into the machine via
input
, then letting the machine decide where to resume based on saidinput
Currently (correct me if im mistaken) the only way of achieving this is by creating a dummy routing state at the start of the machine, then typing out every possible transition to every state you might want to resume at then add a guard for each transition. This adds a ton of boilerplate, is tedious to type out and maintain, and destroys the visibility of your machine's graph
Persisting the machine's state using the persistence feature is also an option but very limiting (saved state becomes incompatible after changes, can't be easily used on invoked machines, etc.)
Beta Was this translation helpful? Give feedback.
All reactions