Instructor: It's approaching winter here in Portland while I'm recording this. I'm inspired by my space heater, so I made a space heater machine.
It has two top-level states -- powered off and powered on -- that are transitioned to with the toggle power event. Inside of powered on, we have a hierarchical state where we also have low heat and high heat that is toggled with the toggle heat.
Now, my space heater has another useful feature that we should add and that's oscillation, the ability for it to go back and forth. Oscillation definitely falls under powered on, but it's not affected by low heat and high heat, so adding it here doesn't seem to make sense.
No, oscillating is really the state of the space heater that happens in parallel to the heating of the space heater. In XState, we can create parallel states. I want to comment this out for right now, so that we're not distracted by it.
To create parallel states, we first do not provide an initial, since it's in each state all at the same time. Instead, we define a type of parallel.
From here, we then enumerate the states that we're in all at the same time. In this case, we'll be in a heated state and we'll be in an oscillation state.
Let's update the machine as is. We can now see that when we're powered on, we have two top-level states. If we toggle in, we're inside of heated and oscillation both at the same time.
We can now take the values we had here for our heated. We can copy them and move them inside of the heated state. We'll update the machine. We'll see that heated now has its own state that we can toggle between, and it doesn't change anything about oscillation, partly because we haven't written it yet, so let's add that.
I'm going to fix my indentation really quick and give myself some room here to see my editor. Inside of oscillation, we'll have an initial of disabled and states of enabled and disabled.
We'll update our machine. We can now see that when we're powered on, we're both in the initial state of low heat for heated and disabled for oscillation.
I can toggle these and they have no effect on the other state. When I click toggled power, I leave all those states and go back to powered off.