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

The "Actor Model Overview" 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 discusses and demonstrates what the actor model is, how to invoke promise, callback, and machine actors.

Preview
Close

Transcript from the "Actor Model Overview" Lesson

[00:00:00]
>> Now we're going to get into a big topic. And this is something that XD is really built around because it's just such a useful concept. And it's also something that is going to be simpler than you might think. And that's the actor model. So the actor model is a model of computation where basically everything is an actor.

[00:00:22]
So what do we mean by an actor? And again, I'm going to show you just by drawing. So let's pretend that this circle is an actor. And we'll even label it as Actor. So an actor can do a few things. First of all, the actor has a behavior.

[00:00:49]
And so, this behavior determines what it does, depending on which events it receives. So, we could have this behavior, or we could have a different behavior. So, if we're considering this to be a human actor, just like the example I gave earlier, we could have an Asleep behavior and an Awake behavior.

[00:01:11]
And we could transition between these two behaviors. So, event might come in, and I'm going to label this as just an arrow that comes into the actor. And this event can just be something like ALARM. And so, this ALARM can cause the actor to change its behavior from Asleep to Awake.

[00:01:38]
So actors can actually have this internal state machine. Or in other words, their behavior can be represented by many things. But one of the best things hopefully you learn from this workshop is that it's usually best to describe behaviors in terms of state machines. And so this actor can change its behavior depending on an event coming in.

[00:02:00]
And like we learned, this changing of behavior due to an event can include things like updating its context or performing some side effects. So what actors can also do in response to an event is the actors themselves can send events to other actors. So I'm just gonna label this as AN_EVENT.

[00:02:27]
And we are gonna create another actor over here with the right stroke, so Another Actor. Now, actors can only send events to actors that they have reference to. So this actor has a reference to another actor, so it could send it an event. However, let's say that we have a third actor right over here.

[00:02:55]
Since this actor has no way of knowing this, I'm just gonna call this Actor 3, has no way of knowing that this Actor 3 exists, it's unable to send it an event. Now what actors could also do is they can spawn other actors and basically create new actors.

[00:03:16]
So I'm gonna represent this by just having a line over here where this is an actor spawned by this other actor. And of course, since this actor spawned that actor, it can send events to it because it has a reference to it. Now depending on your implementation of actors, actors can also send events back to the parent's actor because it's like the they're born with that information.

[00:03:44]
They know who their parent is so they can send events back and forth. One of the easiest ways to conceptualize the actor model is pretending that each actor is a human just like you and me. I'm talking to you and you could talk to me, as long as you have contact with me.

[00:04:03]
For example, if you have my phone number or email address or Twitter username, you have a reference to me. So you can send messages to me. And you can also include, for example, your email, and I can reply to you via email because now I have reference to you as well.

[00:04:22]
Another concept of actors is actors each have their own local state. And this state cannot be directly read by other actors. So everything is done via sending events. And so with actors, if one actor wants to read the state of another actor, what typically happens, and this is best represented in HTTP requests.

[00:04:49]
Is that this actor would send a request like, I want to get this part of data from you. And after some async operation, this actor might respond and say, okay, here's the information. Now you could also model publishing and subscribing as implicit events. So you could say that this actor can subscribe to some part of this other actor.

[00:05:18]
Which in reality is this actor sending events to its subscribers on some sort of regular interval or whenever something changes. So, in short, the actor model is representing each entity in your application as something that you could send events to. Something that could send events to other actors it has reference to, and something that can also spawn its own actors.

[00:05:48]
And an actor also has its own internal state which can change depending on what events it was sent.

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