Check out a free preview of the full State Machines in JavaScript with XState, v2 course

The "Guarded Transitions Solution" Lesson is part of the full, State Machines in JavaScript with XState, v2 course featured in this preview video. Here's what you'd learn in this lesson:

David walks through the solution to the guarded transitions exercise. A student's question regarding how guards are visualized in the state chart is also covered in this segment.


Transcript from the "Guarded Transitions Solution" Lesson

>> In this current exercise, we had to do a few things. And I'm gonna start with the simplest thing first over here, which is making sure the volume is within range. Of course, you could add a condition over here and apply this in line. So, the condition function takes two arguments, the context and the events.

But we want to make sure that this event level, and we're only focused on the event level but we wanna make sure that that's between 0 and 10. So you can do something like return event.level is greater than 0 and event.level is less than or equal to 10.

And so now, when we run this, Guards, we see over here that the volume is currently 5. So if we send a volume level of, let's say 6, and they have it conveniently over there ready, then we see that the volume changed to 6, which is great. So now if we send something like 600, That is going to keep it at 6.

Now one question might be, okay, why do we have to have a guard here where we could just constrain the volume in assignVolume? And you definitely can do that. Here's the difference, though. If you allow this transition to be taken, it's going to basically tell the state machine, this state is changing.

But if the volume isn't changing at all, then nothing really is changing, so you're sort of lying about the state changing. So, it's just a more concrete way of us describing that, no, nothing is changing. We're not changing the state, we're not running any actions because the volume is not within range.

And with things like states.can, which is that useful method that tells you if a state is able to accept an event, that is going to be false if the volume is out of range. Because it is going to check this condition and tell you nothing's gonna happen if you send that event.

Whereas if we had this in assignVolume, then it's gonna say yes, we do accept that event. But nothing will happen, but there's no way of you knowing that nothing will happen. So the second part that I want to go over is using an always or an eventless transition to automatically go somewhere when some condition is true.

And so we specify that here using always. And one trick to prevent infinite loops, because always functions, or sorry, always transitions are always gonna be taken, is that you want to get in the habit of adding a condition first. So, add the condition that the context.elapsed is greater than or equal to context.duration.

And so, when that happens, we want to go to the paused state. So now when we run this, and let's say that right now it is currently playing and we have a minute 40. And so, let's say that we have something that changes the elapsed time. And that thing is gonna be audio.time, which is this assignTime here.

So, I'm going to send type AUDIO.TIME and I'm gonna give it a current time, which is what we have over here. That current time is gonna be something really, really high, like 1000, just trigger this. And so, yeah, you have to add service.send. And so now you see that it immediately went to the paused state because it elapsed or the elapsed time was greater than the duration.

And so, yeah, that's just a useful way of combining both transitions and guards. And using that within always is a really useful pattern because this, again, doesn't have to be dependent on any specific events. So now if we go to, you could take a look at all of these implemented.

We have this which is always going to the paused state when the elapsed value is greater than the duration value. And over here we also have the guards for LIKE.TOGGLE. So it's going to check this one first and say, if the like status is liked, and we're using the raise action here again, we raised the unlike event which is going to cause us to unlike the song.

And if the like status is unliked, then we raise the like event, so we're just going back and forth. Now, some of you observant ones might look at this and be like, isn't this sort of a state machine on its own? And the answer is yes, and so this is also an exercise to the reader.

You could also implement this as a separate parallel state once we get to that in a couple of lessons. So, the final behavior over here should be, I'll just show it to you right here. So yeah, this is the desired behavior, if you're able to implement it, where it goes between liked and unliked.

And so also we have the guards preventing the volume from getting too loud and also skipping ahead, or yeah, going to the paused state when the elapsed time is greater than the current duration. One of the questions asked was, when we apply the guards, so cond, how does that look in the actual state chart?

So we could visualize that here or here. So let me just make an example over here, so const machine = createMachine. All right, so let's say that we have a loading state. And on some event, we only want to do something if the condition is true, so loaded.

So we could say cond: 'dataReceived', sorry, 'dataValid', and target: 'loaded'. So this is how it's going to look and this is how it's represented in a state chart. We have the event here, some event, and we have the guard here that's represented using these brackets. And so this is typical for state chart notation.

So whenever you see those brackets, just realize that this event is a guarded transition or is part of a guarded transition.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now