I am trying to understand how to manage spawned actors from a parent actor. Let's say I have a state machine for a todo item, and I have a parent machine that responds to an "updateTodo" event. I want to use an API like parentActor.send({ type: 'updateTodo', todoId: 'abcde' }). I want this to throw if a Todo with this ID does not exist as a spawned child. How could I achieve this?
#Spawned actors & parent events
1 messages · Page 1 of 1 (latest)
Hi. Couple initial questions. What would be the use case where a spawned actor exists but the id isn't found; where are the ids being stored, in the parent machine? Often, the reference to the spawned actor is stored in the parent machine's context and since it needs to be cleaned up its existence can stay "in sync" in that sense. Also, in terms of throwing an error, which entity would receive or make use of that error? Is it to be emitted from the parent machine? Should it cause an internal transition in the parent machine?
Thanks for the good questions! I'm trying to use xstate in the backend, rather than a front-end app, powering an API. Using the contrived todo example, let's say I'm wanting an API like await todoApi.updateTodo(todoId, todoDetails), which then then wants to go through various steps in the todo state machine and return success or failure. I've been thinking that xstate core could be really useful for managing complex state in non-frontend apps, but maybe that's why I'm struggling a bit with the docs.