Skip to content

Commit a4a3a79

Browse files
authored
Merge pull request #273 from audionerd/docs-actor-model
actor-model.mdx: re-written and expanded
2 parents 9ba5a99 + 827fa2b commit a4a3a79

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

docs/actor-model.mdx

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,80 @@
11
---
2-
title: The actor model
2+
title: The Actor model
33
---
44

5-
When you run a state machine, it becomes an actor. In the actor model, actors are “live” objects that can communicate with each other via asynchronous messages. In XState, we refer to these messages as [**events**](transitions.mdx).
5+
The [Actor model](https://en.wikipedia.org/wiki/Actor_model) in computer science is a mathematical model of concurrent computation in which an "actor" is the basic building block.
66

7-
Coming soon… what are some good examples of actors in software development?
7+
The actor model allows developers to build reliable message-based systems by using actors to communicate. State machines and statecharts can model the logic of actors. These actors can communicate with each other, and with other actors, in the same way.
8+
9+
:::tip
10+
When you run a state machine in XState, it becomes an actor.
11+
:::
12+
13+
### What Defines An "Actor"?
14+
15+
Actors are independent "live" objects that can communicate with each other via asynchronous message passing. In XState, we refer to these messages as [_events_](transitions.mdx).
16+
17+
- An actor has its own internal, encapsulated state that can only be updated by the actor itself. An actor may choose to update its internal state in response to a message it receives, but it cannot be updated by any other entity.
18+
- Actors communicate with other actors by sending and receiving events asynchronously.
19+
- Actors process one message at a time. They have an internal “mailbox” that acts like an event queue, processing events sequentially.
20+
- Internal actor state is not shared between actors. The only way for an actor to share any part of its internal state is by:
21+
- Sending events to other actors
22+
- Or emitting snapshots, which can be considered implicit events sent to subscribers.
23+
- Actors can create (spawn/invoke) new actors.
24+
25+
You'll find strong similarities to the Actor model in software you may already be familiar with. The concept of objects encapsulating state and passing messages to each other may be familiar from Object-Oriented Programming. And actors are analagous to real-world physical concepts like cell biology, and communication in human relationships.
826

927
## State
1028

11-
An actor has its own internal, encapsulated state that the actor itself can only update. An actor may update its internal state in response to a message it receives, but it cannot be updated by any other entity. Actors do not share state. The only way for an actor to share data is by sending events.
29+
An actor has its own internal, encapsulated state that only the actor itself can update. An actor may update its internal state in response to a message it receives, but it cannot be updated by any other entity. Actors do not share state. The only way for an actor to share data is by sending events.
1230

13-
[Read more about actors and state](actors.mdx).
31+
[Read more about XState actors and state](actors.mdx).
1432

15-
## Communication with events
33+
## Communication With Events
1634

1735
Actors communicate with other actors by sending and receiving events asynchronously. Actors use an internal “mailbox” that acts like an event queue, processing events one at a time.
1836

19-
[Read more about actors and events](actors.mdx).
37+
[Read more about XState events and transitions](transitions.mdx).
2038

2139
## Spawning
2240

23-
Actors can spawn new actors. Actors will spawn new actors in situations where they need to delegate work to another actor or when they need to create a new actor to handle a new task. Spawning allows for a flexible and dynamic system where actors can be created and destroyed as needed to handle the workload efficiently.
41+
Actors can spawn new actors, which is useful in situations where an actor needs to delegate work to another actor. Spawning allows for a flexible and dynamic system where actors can be created and destroyed as needed to handle the workload efficiently.
42+
43+
- [Read more about spawning actors in XState](spawn.mdx).
44+
- [Read about the difference between invoking and spawning actors in XState](actors.mdx#invoking-and-spawning-actors).
45+
46+
## The Actor Model in Backend Development
2447

25-
- [Read more about spawning actors](spawn.mdx).
26-
- [Read about the difference between invoking and spawning actors](actors.mdx#invoking-and-spawning-actors).
48+
The actor model is often used to coordinate backend systems. There are direct implementations of the Actor model, like [Akka](https://doc.akka.io/docs/akka/current/typed/guide/introduction.html) for the JVM. In [Erlang](https://www.erlang.org/docs), processes can be seen as actors, which can send and receive messages and spawn new processes. Erlang is used by massive distributed systems, like Discord and WhatsApp.
2749

28-
## Backend development
50+
In [Stately Sky](https://stately.ai/docs/stately-sky-getting-started), a state machine actor be used to manage long-running backend processes like medical patient onboarding flows, inventory management, or multi-player collaborative experiences like whiteboard canvases or games.
2951

30-
Coming soon… how do you use the actor model in backend dev?
52+
## The Actor Model in Frontend Development
3153

32-
## Frontend development
54+
The Actor model is especially useful for coordinating the many moving parts of a front-end web application.
3355

34-
Coming soon… how do you use the actor model in frontend dev?
56+
**Your front-end app is always a distributed system**, and managing distributed systems is where the actor model shines. This is because in a browser environment **you never really have a "global source of truth"**, you instead have **many independent sources of state and events**: 3rd-party components, local component state, local storage, query parameters, routers, network I/O, DOM events and their listeners, etc.
57+
58+
> [] there is no such thing as a single source of truth in any non-trivial application. All applications, even front-end apps, are distributed at some level. – via: [Redux is Half of a Pattern (2/2)](https://dev.to/davidkpiano/redux-is-half-of-a-pattern-2-2-4jo3)
59+
60+
So even for simple web apps, with small app-specific state and a few known app-specific events, the actor model can be helpful.
3561

3662
## XState
3763

38-
Coming soon… how does the actor model work in XState?
64+
Actors in XState can:
65+
- **Accept messages** as [events](transitions#event-objects) passed to their own internal logic, or for state machines as received by [transitions](transitions.mdx).
66+
- **Create more actors** within a state machine using `spawn` in an [`assign`](actions.mdx#assign-action), or using the `spawnChild` action creator. For details, see [Spawn](spawn.mdx).
67+
- **Send more messages** as events using `self.send` in their own logic, or [action creators](actions.mdx) like [`sendTo`](actions#send-to-action) or [`raise`](actions#raise-action) in a state machine.
68+
69+
Actors in XState have their own [actor logic](actors.mdx#actor-logic) which they use to:
70+
- **Make local decisions**
71+
- **Determine how to repond to the next message received**
72+
- **Modify their own private state** (but only affect each other via messaging)
73+
74+
Actors in XState exist in [systems](system.mdx) and can communicate with each other within and across those systems.
75+
76+
## Reference
77+
78+
- [What is the actor model and when should I use it?](https://stately.ai/blog/what-is-the-actor-model)
79+
- [The Actor Model Explained in 5 Minutes](https://www.youtube.com/watch?v=ELwEdb_pD0k)
80+
- [Wikipedia: Actor model](https://en.wikipedia.org/wiki/Actor_model)

docs/actors.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ In the actor model, actors are objects that can communicate with each other. The
2727

2828
- An actor has its own internal, encapsulated state that can only be updated by the actor itself. An actor may choose to update its internal state in response to a message it receives, but it cannot be updated by any other entity.
2929
- Actors communicate with other actors by sending and receiving events asynchronously.
30-
- Actors process one message at a time. They have an internal “mailbox” that acts like an event queue, processing events one at a time.
30+
- Actors process one message at a time. They have an internal “mailbox” that acts like an event queue, processing events sequentially.
3131
- Internal actor state is not shared between actors. The only way for an actor to share any part of its internal state is by:
3232
- Sending events to other actors
3333
- Or emitting snapshots, which can be considered implicit events sent to subscribers.
3434
- Actors can create (spawn/invoke) new actors.
3535

36+
[Read more about the Actor model](actor-model.mdx)
37+
3638
## Actor logic
3739

3840
Actor logic is the actor’s logical “model” (brain, blueprint, DNA, etc.) It describes how the actor should change behavior when receiving an event. You can create actor logic using **[actor logic creators](#actor-logic-creators)**.

0 commit comments

Comments
 (0)