Skip to content

Commit 2fdb8d9

Browse files
authored
Merge pull request #250 from audionerd/docs-actor-logic-creators
actors.mdx: Add an "Actor logic creators" heading and group creator docs underneath
2 parents b3e99f8 + 8532404 commit 2fdb8d9

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

docs/actors.mdx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,17 @@ In the actor model, actors are objects that can communicate with each other. The
3333
- Or emitting snapshots, which can be considered implicit events sent to subscribers.
3434
- Actors can create (spawn/invoke) new actors.
3535

36-
## Creating actor logic
36+
## Actor logic
3737

38-
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**. The types of actor logic you can create from XState are:
39-
40-
- [State machine logic (`createMachine(...)`)](cheatsheet.mdx#creating-a-state-machine)
41-
- [Promise logic (`fromPromise(...)`)](cheatsheet.mdx#creating-promise-logic)
42-
- [Transition logic (`fromTransition(...)`)](cheatsheet.mdx#creating-promise-logic)
43-
- [Observable logic (`fromObservable(...)`)](cheatsheet.mdx#creating-observable-logic)
44-
- [Event observable logic (`fromEventObservable(...)`)](migration.mdx#use-actor-logic-creators-for-invokesrc-instead-of-functions)
45-
- [Callback logic (`fromCallback(...)`)](cheatsheet.mdx#creating-callback-logic)
38+
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)**.
4639

4740
In XState, actor logic is defined by an object containing methods like `.transition(...)`, `.getInitialState()`, `.getSnapshot()`, and more. This object tells an interpreter how to update an actor’s internal state when it receives an event and which effects to execute (if any).
4841

4942
## Creating actors
5043

5144
You can create an actor, which is a “live” instance of some actor logic, via `createActor(actorLogic, options?)`. The `createActor(...)` function takes the following arguments:
5245

53-
- `actorLogic`: the [actor logic](actors.mdx#creating-actor-logic) to create an actor from
46+
- `actorLogic`: the [actor logic](actors.mdx#actor-logic) to create an actor from
5447
- `options` (optional): actor options
5548

5649
When you create an actor from actor logic via `createActor(actorLogic)`, you implicitly create an [actor system](system.mdx) where the created actor is the root actor. Any actors spawned from this root actor and its descendants are part of that actor system. The actor must be started by calling `actor.start()`, which will also start the actor system:
@@ -209,7 +202,18 @@ console.log(snapshot.output);
209202
// => 100
210203
```
211204

212-
## State machine actor logic
205+
## Actor logic creators
206+
207+
The types of actor logic you can create from XState are:
208+
209+
- [State machine logic (`createMachine(...)`)](#createmachine)
210+
- [Promise logic (`fromPromise(...)`)](#frompromise)
211+
- [Transition function logic (`fromTransition(...)`)](#fromtransition)
212+
- [Observable logic (`fromObservable(...)`)](#fromobservable)
213+
- [Event observable logic (`fromEventObservable(...)`)](#fromeventobservable)
214+
- [Callback logic (`fromCallback(...)`)](#fromcallback)
215+
216+
### State machine logic (`createMachine(...)`) {#createmachine}
213217

214218
You can describe actor logic as a [state machine](machines.mdx). Actors created from state machine actor logic can:
215219

@@ -242,7 +246,7 @@ toggleActor.send({ type: 'toggle' });
242246
// Logs 'active'
243247
```
244248

245-
## Promise actor logic
249+
### Promise logic (`fromPromise(...)`) {#frompromise}
246250

247251
Promise actor logic is described by an async process that resolves or rejects after some time. Actors created from promise logic (“promise actors”) can:
248252

@@ -275,9 +279,9 @@ promiseActor.start();
275279
// }
276280
```
277281

278-
## Transition function actors
282+
### Transition function logic (`fromTransition(...)`) {#fromtransition}
279283

280-
Transition actor logic is described by a [transition function](migration.mdx#use-actor-logic-creators-for-invokesrc-instead-of-functions), similar to a [reducer](cheatsheet.mdx#creating-transition-logic). Transition functions take the current `state` and received `event` object as arguments, and return the next state. Actors created from transition logic (“transition actors”) can:
284+
Transition actor logic is described by a [transition function](migration.mdx#use-actor-logic-creators-for-invokesrc-instead-of-functions), similar to a [reducer](fromTransition). Transition functions take the current `state` and received `event` object as arguments, and return the next state. Actors created from transition logic (“transition actors”) can:
281285

282286
- Receive events
283287
- Emit snapshots of its state
@@ -315,9 +319,9 @@ transitionActor.send({ type: 'increment' });
315319
// }
316320
```
317321

318-
## Observable actors
322+
### Observable logic (`fromObservable(...)`) {#fromobservable}
319323

320-
Observable actor logic is described by an [observable stream of values](cheatsheet.mdx#creating-observable-logic). Actors created from observable logic (“observable actors”) can:
324+
Observable actor logic is described by an [observable stream of values](#fromObservable). Actors created from observable logic (“observable actors”) can:
321325

322326
- Emit snapshots of its emitted value
323327

@@ -340,7 +344,7 @@ secondActor.start();
340344
// ...
341345
```
342346

343-
## Event observable actors
347+
### Event observable logic (`fromEventObservable(...)` {#fromeventobservable}
344348

345349
Event observable actor logic is described by an observable stream of [event objects](transitions.mdx#event-objects). Actors created from event observable logic (“event observable actors”) can:
346350

@@ -365,7 +369,7 @@ const canvasActor = createActor(canvasMachine);
365369
canvasActor.start();
366370
```
367371

368-
## Callback actors
372+
### Callback logic (`fromCallback(...)`) {#fromcallback}
369373

370374
Callback actor logic is described by a callback function that receives a single object argument that includes a `sendBack(event)` function and a `receive(event => ...)` function. Actors created from callback logic (“callback actors”) can:
371375

0 commit comments

Comments
 (0)