#Parallel states questions

1 messages · Page 1 of 1 (latest)

lusty river
#

Hello, I'm building a state machine to explain our CI. It's a good opportunity to introduce xstate to the team. I've started to model the state machine, but I haven't worked much with parallel states.

My questions:

  1. There is an initial state on the parallel state, but both children states start, does this initial state have some meaning?
  2. I'm connecting the nested states directly to other external states and it causes a lot of "wiring" and makes it a bit noisy. Is this a normal thing to do or are there more readable alternatives?
  3. I was starting to implement a guard, but I can't find the place to edit it. Did it change? I could swear I did it on the editor before.

This is the ongoing state machine: https://stately.ai/registry/editor/ae678c2f-a611-44ae-8a89-1b32cb1c92d7?mode=Design&machineId=7e6eb546-da01-42b1-84d6-ea086c4305b3

Thank you,
Artur

rich urchin
#

Hi Artur!

  1. The initial state tells your machine which child state to enter first inside your parallel state. This is clearer if you have multiple child states inside a parallel parent.

  2. You might have more nested states than you need. Often if there’s a single state nested in another single state, that’s a sign that you can merge those into one state. If you want to add more information to those states, I’d recommend using descriptions or notes. Here’s a version of your machine I remixed: https://stately.ai/registry/editor/417297d9-448a-4878-9bc6-ed2833dd8c90?machineId=e41578ae-dbcf-4fb0-84fb-ab2695d5143a

  3. Select an event (one of your success or fail events) and then choose Guard from the + menu… this will add a guard on that transition.

lusty river
#

Hi Laura, thank you so much!

  1. Yeah, I think I need to test a bit more to get it.
  2. Indeed I can simplify if it has a single state, good tip! But let's say I have 10 child states, the only way to implement this is by creating 10 transitions with guards to the next step?
  3. I know we can add the guard name, but I wanted to add the guard code. I found the state machine definition on the left, but it's read-only. If I want to add logic (like add +1 in a context field), I can only do this in a running project?

Again, thanks for the great reply!

rich urchin
lusty river
#
  1. Oh I see, I'll try it that way too.
  2. Ah ok, great!

Thank you very much and have a nice day!