Skip to content

Commit e9f65e0

Browse files
authored
Merge pull request #178 from statelyai/davidkpiano/createactor
interpret -> createActor
2 parents d7ce39c + 9e193ce commit e9f65e0

20 files changed

+274
-220
lines changed

versioned_docs/version-5/actions.mdx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Examples of actions:
5353

5454
In our video player machine, we have entry and exit actions on the Playing state. We use the entry action of playVideo to fire an effect playing the video on entry to the Playing state. We use the exit action of pauseVideo to fire an effect pausing the video when the Playing state is exited.
5555

56-
<EmbedMachine
56+
<EmbedMachine
5757
name="video player machine"
5858
embedURL="https://stately.ai/registry/editor/embed/e13bef2b-bb13-4465-96ac-0bc25340688e?machineId=222e2d7a-0ed6-4f2c-843a-e6646d717000"
5959
/>
@@ -142,9 +142,9 @@ To add a built-in action, first add a [transition action](#add-a-transition-acti
142142

143143
1. Open the state, or event <Info size={18} /> **Details** panel from the right tool menu.
144144
1. Hover, focus, or select the action block to reveal the <Edit size={18} /> edit icon button.
145-
2. Use the <Edit size={18} /> edit icon button to open the **Action** panel.
146-
3. Choose your desired action from the dropdown menu at the top of the **Action** panel.
147-
4. Fill out the required corresponding action input fields.
145+
1. Use the <Edit size={18} /> edit icon button to open the **Action** panel.
146+
1. Choose your desired action from the dropdown menu at the top of the **Action** panel.
147+
1. Fill out the required corresponding action input fields.
148148

149149
## Add a custom action
150150

@@ -165,9 +165,9 @@ First, add a [transition action](#add-a-transition-action), [entry action](#add-
165165

166166
1. Open the state, or event <Info size={18} /> **Details** panel from the right tool menu.
167167
1. Hover, focus, or select the action block to reveal the <Edit size={18} /> edit icon button.
168-
2. Use the <Edit size={18} /> edit icon button to open the **Action** panel.
169-
3. Custom action is selected by default. Add your custom action type in the **Action type** text input.
170-
4. Add your custom **Action parameters** using the **Parameter key** and **Parameter value** text input pairs.
168+
1. Use the <Edit size={18} /> edit icon button to open the **Action** panel.
169+
1. Custom action is selected by default. Add your custom action type in the **Action type** text input.
170+
1. Add your custom **Action parameters** using the **Parameter key** and **Parameter value** text input pairs.
171171

172172
## Entry and exit actions
173173

@@ -183,7 +183,6 @@ Entry and exit actions are defined using the `entry: ...` and `exit: ...` attrib
183183

184184
Coming soon… example
185185

186-
187186
## Inline actions
188187

189188
You can declare actions as inline functions:
@@ -263,7 +262,7 @@ const feedbackMachine = createMachine(
263262
These implementations can later be overridden by providing implementations in the `machine.provide(implementations)` method, which creates a new machine with the same config but with the provided implementations:
264263

265264
```ts
266-
const feedbackActor = interpret(
265+
const feedbackActor = createActor(
267266
// highlight-start
268267
feedbackMachine.provide({
269268
actions: {
@@ -297,7 +296,7 @@ const countMachine = createMachine({
297296
},
298297
});
299298

300-
const countActor = interpret(countMachine);
299+
const countActor = createActor(countMachine);
301300
countActor.subscribe((state) => {
302301
console.log(state.context.count);
303302
});
@@ -340,7 +339,7 @@ The raise action is a special action that _raises_ an event that is received by
340339
actions: raise({ type: 'someEvent', data: 'someData' });
341340
```
342341

343-
Internally, when an event is raised, it is placed into an “internal event queue”. After the current transition is finished, these events are processed in insertion order ([first-in first-out, or FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))). External events are only processed once all events in the internal event queue are processed.
342+
Internally, when an event is raised, it is placed into an “internal event queue”. After the current transition is finished, these events are processed in insertion order ([first-in first-out, or FIFO](<https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)>)). External events are only processed once all events in the internal event queue are processed.
344343

345344
Raised events can be dynamic:
346345

versioned_docs/version-5/actors.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ In XState, actor logic is defined by an object containing methods like `.transit
4848

4949
## Creating actors
5050

51-
You can create an actor, which is a “live” instance of some actor logic, via `interpret(actorLogic, options?)`. The `interpret(...)` function takes the following arguments:
51+
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:
5252

53-
- `actorLogic`: the [actor logic](actors.mdx#creating-actor-logic) to interpret
54-
- `options` (optional): interpreter options
53+
- `actorLogic`: the [actor logic](actors.mdx#creating-actor-logic) to create an actor from
54+
- `options` (optional): actor options
5555

56-
When you `interpret(...)` actor logic, 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:
56+
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:
5757

5858
```ts
59-
import { interpret } from 'xstate';
59+
import { createActor } from 'xstate';
6060
import { someActorLogic } from './someActorLogic.ts';
6161

62-
const actor = interpret(someActorLogic);
62+
const actor = createActor(someActorLogic);
6363
actor.start();
6464

6565
// Now the actor can receive events
@@ -91,15 +91,15 @@ When an actor receives an event, its internal state may change. An actor may emi
9191
An actor’s snapshot is not necessarily the same as its internal state; instead, it is a value the actor wants to _share_ with subscribers. For example, a promise actor has an internal state consisting of `data`, `status`, and other internal properties, but only the `data` is emitted as its snapshot.
9292

9393
```ts
94-
import { fromPromise, interpret } from 'xstate';
94+
import { fromPromise, createActor } from 'xstate';
9595

9696
const countLogic = fromPromise(async () => {
9797
const count = await fetchCount();
9898

9999
return count;
100100
});
101101

102-
const countActor = interpret(countLogic);
102+
const countActor = createActor(countLogic);
103103
countActor.start();
104104

105105
countActor.getSnapshot(); // logs undefined
@@ -148,14 +148,14 @@ subscription.unsubscribe();
148148

149149
When the actor is stopped, all observers will automatically be unsubscribed.
150150

151-
You can initialize actor logic at a specific persisted internal state by passing the state in the second `options` argument of `interpret(logic, options)`. If the state is compatible with the actor logic, this will create an actor that will be started at that persisted state:
151+
You can initialize actor logic at a specific persisted internal state by passing the state in the second `options` argument of `createActor(logic, options)`. If the state is compatible with the actor logic, this will create an actor that will be started at that persisted state:
152152

153-
You can initialize actor logic at a specific persisted internal state by passing the state in the second `options` argument of `interpret(logic, options)`. If the state is compatible with the actor logic, this will create an actor that will be started at that persisted state:
153+
You can initialize actor logic at a specific persisted internal state by passing the state in the second `options` argument of `createActor(logic, options)`. If the state is compatible with the actor logic, this will create an actor that will be started at that persisted state:
154154

155155
```ts
156156
const persistedState = JSON.parse(localStorage.get('some-persisted-state'));
157157

158-
const actor = interpret(someLogic, {
158+
const actor = createActor(someLogic, {
159159
// highlight-start
160160
state: persistedState,
161161
// highlight-end
@@ -206,7 +206,7 @@ const toggleMachine = createMachine({
206206
},
207207
});
208208

209-
const toggleActor = interpret(toggleMachine);
209+
const toggleActor = createActor(toggleMachine);
210210
toggleActor.subscribe((snapshot) => {
211211
// snapshot is the machine's state
212212
console.log('state', snapshot.value);
@@ -232,7 +232,7 @@ const promiseLogic = fromPromise(() => {
232232
return fetch('...').then((data) => data.json());
233233
});
234234

235-
const promiseActor = interpret(promiseLogic);
235+
const promiseActor = createActor(promiseLogic);
236236
promiseActor.subscribe((snapshot) => {
237237
console.log(snapshot);
238238
});
@@ -264,7 +264,7 @@ const transitionLogic = fromTransition(
264264
{ count: 0 },
265265
);
266266

267-
const transitionActor = interpret(transitionLogic);
267+
const transitionActor = createActor(transitionLogic);
268268
transitionActor.subscribe((snapshot) => {
269269
console.log(snapshot);
270270
});
@@ -286,7 +286,7 @@ Sending events to observable actors will have no effect.
286286
```ts
287287
const secondLogic = fromObservable(() => interval(1000));
288288

289-
const secondActor = interpret(secondLogic);
289+
const secondActor = createActor(secondLogic);
290290
secondActor.start();
291291
// At every second:
292292
// Logs 0
@@ -316,7 +316,7 @@ const canvasMachine = createMachine({
316316
},
317317
});
318318

319-
const canvasActor = interpret(canvasMachine);
319+
const canvasActor = createActor(canvasMachine);
320320
canvasActor.start();
321321
```
322322

versioned_docs/version-5/context.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const feedbackMachine = createMachine({
2020
},
2121
});
2222

23-
const feedbackActor = interpret(feedbackMachine);
23+
const feedbackActor = createActor(feedbackMachine);
2424

2525
feedbackActor.subscribe((state) => {
2626
console.log(state.context.feedback);
@@ -79,7 +79,7 @@ const feedbackMachine = createMachine({
7979
}),
8080
});
8181

82-
const feedbackActor = interpret(feedbackMachine).start();
82+
const feedbackActor = createActor(feedbackMachine).start();
8383

8484
console.log(feedbackActor.getSnapshot().context.createdAt);
8585
// logs the current timestamp
@@ -89,7 +89,7 @@ Lazy initial context is evaluated per actor, so each actor will have its own `co
8989

9090
### Input
9191

92-
You can provide input data to a machine’s initial `context` by passing an `input` property to the `interpret(machine, { input })` function and using the `input` property from the first argument in the `context` function:
92+
You can provide input data to a machine’s initial `context` by passing an `input` property to the `createActor(machine, { input })` function and using the `input` property from the first argument in the `context` function:
9393

9494
```ts
9595
const feedbackMachine = createMachine({
@@ -99,7 +99,7 @@ const feedbackMachine = createMachine({
9999
}),
100100
});
101101

102-
const feedbackActor = interpret(feedbackMachine, {
102+
const feedbackActor = createActor(feedbackMachine, {
103103
input: {
104104
defaultRating: 5,
105105
},
@@ -131,7 +131,7 @@ const feedbackMachine = createMachine({
131131
},
132132
});
133133

134-
const feedbackActor = interpret(feedbackMachine);
134+
const feedbackActor = createActor(feedbackMachine);
135135

136136
feedbackActor.subscribe((state) => {
137137
console.log(state.context.feedback);
@@ -218,7 +218,7 @@ const machine = createMachine({
218218
}),
219219
});
220220

221-
const feedbackActor = interpret(machine, {
221+
const feedbackActor = createActor(machine, {
222222
input: {
223223
defaultRating: 5,
224224
},

versioned_docs/version-5/final-states.mdx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import EmbedMachine from '@site/src/components/EmbedMachine';
77
A final state is a state that represents the completion or successful termination of a machine. It is defined by the `type: 'final'` property on a state node:
88

99
```ts
10-
import { createMachine, interpret } from 'xstate';
10+
import { createMachine, createActor } from 'xstate';
1111

1212
const feedbackMachine = createMachine({
1313
initial: 'prompt',
@@ -42,7 +42,10 @@ A machine can have multiple final states or no final states.
4242
- When a machine reaches a top-level final state, it terminates.
4343
- Final states cannot have transitions
4444

45-
<EmbedMachine name="Video player" embedURL="https://stately.ai/registry/editor/embed/e13bef2b-bb13-4465-96ac-0bc25340688e?machineId=c6f8ca35-25e3-4fc6-b4fe-c9994715852e" />
45+
<EmbedMachine
46+
name="Video player"
47+
embedURL="https://stately.ai/registry/editor/embed/e13bef2b-bb13-4465-96ac-0bc25340688e?machineId=c6f8ca35-25e3-4fc6-b4fe-c9994715852e"
48+
/>
4649

4750
## Using final states in Stately Studio
4851

@@ -86,7 +89,7 @@ When all regions of a parallel state are "done", the parallel state is considere
8689
In this example, the `preparation` state is a parallel state with two regions: `beans` and `water`. When both regions are done, the `preparation` state is done, and the `brewing` state is entered.
8790

8891
```ts
89-
import { createMachine, interpret } from 'xstate';
92+
import { createMachine, createActor } from 'xstate';
9093

9194
const coffeeMachine = createMachine({
9295
initial: 'preparation',
@@ -139,7 +142,7 @@ const coffeeMachine = createMachine({
139142
When a machine reaches its top-level final state, it can produce output data. You can specify this output data in the `.output` property of the top-level final state:
140143

141144
```ts
142-
import { createMachine, interpret } from 'xstate';
145+
import { createMachine, createActor } from 'xstate';
143146

144147
const currencyMachine = createMachine({
145148
// ...
@@ -159,7 +162,7 @@ const currencyMachine = createMachine({
159162
},
160163
});
161164

162-
const currencyActor = interpret(currencyMachine, {
165+
const currencyActor = createActor(currencyMachine, {
163166
input: {
164167
amount: 10,
165168
fromCurrency: 'USD',

versioned_docs/version-5/finite-states.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const feedbackMachine = createMachine({
5252
},
5353
});
5454

55-
const feedbackActor = interpret(feedbackMachine).start();
55+
const feedbackActor = createActor(feedbackMachine).start();
5656

5757
// Finite state
5858
console.log(feedbackActor.getSnapshot().value);
@@ -161,7 +161,7 @@ const feedbackMachine = createMachine({
161161
},
162162
});
163163

164-
const feedbackActor = interpret(feedbackMachine).start();
164+
const feedbackActor = createActor(feedbackMachine).start();
165165

166166
console.log(feedbackActor..getSnapshot().hasTag('visible'));
167167
// logs true
@@ -220,7 +220,7 @@ const feedbackMachine = createMachine({
220220
},
221221
});
222222

223-
const feedbackActor = interpret(feedbackMachine).start();
223+
const feedbackActor = createActor(feedbackMachine).start();
224224

225225
console.log(feedbackActor.getSnapshot().meta);
226226
// logs the object:
@@ -239,7 +239,7 @@ console.log(feedbackActor.getSnapshot().meta);
239239
Transitions are how you move from one finite state to another. They are defined by the `on` property on a state node:
240240

241241
```ts
242-
import { createMachine, interpret } from 'xstate';
242+
import { createMachine, createActor } from 'xstate';
243243

244244
const feedbackMachine = createMachine({
245245
initial: 'prompt',
@@ -258,7 +258,7 @@ const feedbackMachine = createMachine({
258258
},
259259
});
260260

261-
const feedbackActor = interpret(feedbackMachine).start();
261+
const feedbackActor = createActor(feedbackMachine).start();
262262

263263
console.log(feedbackActor.getSnapshot().value);
264264
// logs 'prompt'
@@ -276,7 +276,7 @@ Read more about [events and transitions](transitions.mdx).
276276
A transition's `target` property defines where the machine should go when the transition is taken. Normally, it targets a sibling state node:
277277

278278
```ts
279-
import { createMachine, interpret } from 'xstate';
279+
import { createMachine, createActor } from 'xstate';
280280

281281
const feedbackMachine = createMachine({
282282
initial: 'prompt',
@@ -302,7 +302,7 @@ const feedbackMachine = createMachine({
302302
The `target` can also target a descendant of a sibling state node:
303303

304304
```ts
305-
import { createMachine, interpret } from 'xstate';
305+
import { createMachine, createActor } from 'xstate';
306306

307307
const feedbackMachine = createMachine({
308308
initial: 'prompt',
@@ -333,7 +333,7 @@ const feedbackMachine = createMachine({
333333
When the target state node is a descendant of the source state node, the source state node key can be omitted:
334334

335335
```ts
336-
import { createMachine, interpret } from 'xstate';
336+
import { createMachine, createActor } from 'xstate';
337337

338338
const feedbackMachine = createMachine({
339339
// ...
@@ -366,7 +366,7 @@ const feedbackMachine = createMachine({
366366
When the state node doesn't change; i.e., the source and target state nodes are the same, the `target` property can be omitted:
367367

368368
```ts
369-
import { createMachine, interpret } from 'xstate';
369+
import { createMachine, createActor } from 'xstate';
370370

371371
const feedbackMachine = createMachine({
372372
// ...
@@ -389,7 +389,7 @@ const feedbackMachine = createMachine({
389389
State nodes can also be targeted by their `id` by prefixing the `target` with a `#` followed by the state node's `id`:
390390

391391
```ts
392-
import { createMachine, interpret } from 'xstate';
392+
import { createMachine, createActor } from 'xstate';
393393

394394
const feedbackMachine = createMachine({
395395
initial: 'prompt',
@@ -415,7 +415,7 @@ const feedbackMachine = createMachine({
415415
States can be identified with a unique ID: `id: 'myState'`. This is useful for targeting a state from any other state, even if they have different parent states:
416416

417417
```ts
418-
import { createMachine, interpret } from 'xstate';
418+
import { createMachine, createActor } from 'xstate';
419419

420420
const feedbackMachine = createMachine({
421421
initial: 'prompt',

versioned_docs/version-5/guards.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const feedbackMachine = createMachine(
4141

4242
## Using guards in Stately Studio
4343

44-
<EmbedMachine name="Video player" embedURL="https://stately.ai/registry/editor/embed/e13bef2b-bb13-4465-96ac-0bc25340688e?machineId=96f3bfce-147a-4aa0-ae2d-674cdfcb14ec"/>
44+
<EmbedMachine
45+
name="Video player"
46+
embedURL="https://stately.ai/registry/editor/embed/e13bef2b-bb13-4465-96ac-0bc25340688e?machineId=96f3bfce-147a-4aa0-ae2d-674cdfcb14ec"
47+
/>
4548

4649
:::studio
4750

@@ -155,7 +158,7 @@ const feedbackMachine = createMachine(
155158
Guards can later be provided or overridden by providing custom guard implementations in the `.provide()` method:
156159

157160
```ts
158-
const feedbackActor = interpret(
161+
const feedbackActor = createActor(
159162
// highlight-start
160163
feedbackMachine.provide({
161164
guards: {

0 commit comments

Comments
 (0)