You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -88,24 +93,32 @@ An example of the difference between invoking and spawning actors could occur in
88
93
89
94
When an actor receives an event, its internal state may change. An actor may emit a **snapshot** when a state transition occurs. You can read an actor’s snapshot synchronously via `actor.getSnapshot()`.
90
95
91
-
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.
92
-
93
96
```ts
94
97
import { fromPromise, createActor } from'xstate';
95
98
99
+
asyncfunction fetchCount() {
100
+
returnPromise.resolve(42);
101
+
}
102
+
96
103
const countLogic =fromPromise(async () => {
97
104
const count =awaitfetchCount();
98
105
99
106
returncount;
100
107
});
101
108
102
109
const countActor =createActor(countLogic);
110
+
103
111
countActor.start();
104
112
105
113
countActor.getSnapshot(); // logs undefined
106
114
107
115
// After the promise resolves...
108
-
countActor.getSnapshot(); // logs 42
116
+
countActor.getSnapshot();
117
+
// => {
118
+
// output: 42,
119
+
// status: 'done',
120
+
// ...
121
+
// }
109
122
```
110
123
111
124
You can subscribe to an actor’s snapshot values via `actor.subscribe(observer)`. The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
When the actor is stopped, all observers will automatically be unsubscribed.
150
-
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:
162
+
When the actor is stopped, all of its observers will automatically be unsubscribed.
152
163
153
164
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:
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:
0 commit comments