Skip to content

Commit 7a68cbb

Browse files
[v5] Explicit spawn (#3148)
* All tests pass! * Fix all tests * Some types cleanup * Fix types WIP * Add TODOs for types * Skip questionable test * Remove stray console logs * Add `spawn.machine` * Add `spawn.promise` * Add `spawn.observable` * Add tests for referenced behaviors * Revert "Add `spawn.observable`" This reverts commit 6df48b5. * Revert "Add `spawn.promise`" This reverts commit 9fce5a9. * Revert "Add `spawn.machine`" This reverts commit 0a3ac9f. * Cleanup * Temp fix for string references * Ignore excessive stack depth warnings * This actually passes * Remove invoke* * Rename createDeferredBehavior -> createCallbackBehavior * Make getContextAndActions private * Fix & clarify unskipped test * Add test from todo * Clean up test * Make parent "private" * Make sure children is "immutable" (+ todos) * Revert initialState change * Add comment * Oops * Remove invoke* functions (except for invokeMachine) * Quell 1 type error * Fixed the conditional type in the `interpret` * Bring back removed `spawn` type test * Tweak one `assign` test * Remove lazy argument from `createMachineBehavior` and remove `invokeMachine` entirely * Add a TODO comment about hiding `ActorRef["_parent"]` * Require a machine with all implementations provided as an argument for `createMachineBehavior` * Cache the `initialState` getter in `createMachineBehavior` * Unwrap `createCallbackBehavior` argument from a redundant lazy layer * Renaming WIP * Renaming WIP * Add back spawnBehavior but call it createActorRef * behaviors -> actors * Tweak tests * Remove Actor class * Remove actor.ts * Fix missing function * Remove createBehaviorFrom * Make behavior private * Add onEmit and remove complete * Tighten up types (a little) * Fix types * Remove createBehaviorFrom * No more magic events * Update packages/core/src/actors.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/core/src/types.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Fix types * Fix sendError (send raw error data, not events) * Attempt to fix type * Fix type * Rename onEmit -> onSnapshot * Rename types: TEmitted -> TSnapshot * Change src to allow behaviors directly (WIP) * Fix type errors * Add back InvokeSrcDefinition for now * Add fromEventObservable * Remove outdated `xstate/behaviors` references (#3280) * Avoid wrapping `behaviorImpl` in a throwaway function (#3281) * Update packages/core/test/actions.test.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/xstate-svelte/src/useSelector.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/xstate-svelte/src/useSelector.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/xstate-vue/src/useSelector.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/xstate-vue/src/useSelector.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Remove unused type * Remove subscriptions from behaviors (they belong on ObservableActorRef only) * Add TODO * Remove unused type * Refactor: do not use observers for invoked promises * Remove latestData logic in interpreter * Remove observer logic from fromObservable/fromObservableEvent * Remove observers * Remove subscription logic from interpreter * Remove unused type * Fixed `ObservableActorRef` subscriptions and remove some outdated/redundant code (#3319) * Fixed `ObservableActorRef` subscriptions and remove some outdated/redundant code * Add changesets * Remove `createActorRef` * Make `Interpreter#nextState` pure again (#3340) * Rename `EmittedFrom` to `SnapshotFrom` (#3341) * Add changesets Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent dc3eba3 commit 7a68cbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2425
-2408
lines changed

.changeset/curvy-feet-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'xstate': minor
3+
---
4+
5+
`onSnapshot` is now available for invoke configs. You can specify a transition there to be taken when a snapshot of an invoked actor gets updated. It works similarly to `onDone`/`onError`.

.changeset/green-apes-heal.md

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@xstate/vue': major
3+
---
4+
5+
Removed `getSnapshot` parameter from composables. It is expected that the received `actorRef` has to have a `getSnapshot` method on it that can be used internally.

.changeset/plenty-dragons-lie.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'xstate': major
3+
---
4+
5+
`spawn` is no longer importable from `xstate`. Instead you get it in `assign` like this:
6+
7+
```js
8+
assign((ctx, ev, { spawn }) => {
9+
return {
10+
...ctx,
11+
actorRef: spawn(promiseActor)
12+
};
13+
});
14+
```
15+
16+
In addition to that, you can now `spawn` actors defined in your implementations object, in the same way that you were already able to do that with `invoke`. To do that just reference the defined actor like this:
17+
18+
```js
19+
spawn('promiseActor');
20+
```

.changeset/quiet-peaches-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'xstate': major
3+
---
4+
5+
`EmittedFrom` type helper has been renamed to `SnapshotFrom`.

.changeset/rotten-rivers-wonder.md

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,8 @@
22
'xstate': major
33
---
44

5-
**Breaking:** Activities are no longer a separate concept. Instead, activities are invoked. Internally, this is how activities worked. The API is consolidated so that `activities` are no longer a property of the state node or machine options:
6-
7-
```diff
8-
import { createMachine } from 'xstate';
9-
+import { invokeActivity } from 'xstate/invoke';
10-
11-
const machine = createMachine(
12-
{
13-
// ...
14-
- activities: 'someActivity',
15-
+ invoke: {
16-
+ src: 'someActivity'
17-
+ }
18-
},
19-
{
20-
- activities: {
21-
+ actors: {
22-
- someActivity: ((context, event) => {
23-
+ someActivity: invokeActivity((context, event) => {
24-
// ... some continuous activity
25-
26-
return () => {
27-
// dispose activity
28-
}
29-
})
30-
}
31-
}
32-
);
33-
```
34-
35-
**Breaking:** The `services` option passed as the second argument to `createMachine(config, options)` is renamed to `actors`. Each value in `actors`should be a function that takes in `context` and `event` and returns a [behavior](TODO: link) for an actor. The provided invoke creators are:
36-
37-
- `invokeActivity`
38-
- `invokePromise`
39-
- `invokeCallback`
40-
- `invokeObservable`
41-
- `invokeMachine`
42-
43-
```diff
44-
import { createMachine } from 'xstate';
45-
+import { invokePromise } from 'xstate/invoke';
46-
47-
const machine = createMachine(
48-
{
49-
// ...
50-
invoke: {
51-
src: 'fetchFromAPI'
52-
}
53-
},
54-
{
55-
- services: {
56-
+ actors: {
57-
- fetchFromAPI: ((context, event) => {
58-
+ fetchFromAPI: invokePromise((context, event) => {
59-
// ... (return a promise)
60-
})
61-
}
62-
}
63-
);
64-
```
65-
665
**Breaking:** The `state.children` property is now a mapping of invoked actor IDs to their `ActorRef` instances.
676

687
**Breaking:** The way that you interface with invoked/spawned actors is now through `ActorRef` instances. An `ActorRef` is an opaque reference to an `Actor`, which should be never referenced directly.
698

70-
**Breaking:** The `src` of an `invoke` config is now either a string that references the machine's `options.actors`, or a `BehaviorCreator`, which is a function that takes in `context` and `event` and returns a `Behavior`:
71-
72-
```diff
73-
import { createMachine } from 'xstate';
74-
+import { invokePromise } from 'xstate/invoke';
75-
76-
const machine = createMachine({
77-
// ...
78-
invoke: {
79-
- src: (context, event) => somePromise
80-
+ src: invokePromise((context, event) => somePromise)
81-
}
82-
// ...
83-
});
84-
```
85-
869
**Breaking:** The `origin` of an `SCXML.Event` is no longer a string, but an `ActorRef` instance.

.changeset/rotten-schools-march.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
'xstate': major
3+
---
4+
5+
The `services` option passed as the second argument to `createMachine(config, options)` is renamed to `actors`. Each value in `actors` should be a function that takes in `context` and `event` and returns a [behavior](TODO: link) for an actor. The provided behavior creators are:
6+
7+
- `fromMachine`
8+
- `fromPromise`
9+
- `fromCallback`
10+
- `fromObservable`
11+
- `fromEventObservable`
12+
13+
```diff
14+
import { createMachine } from 'xstate';
15+
+import { fromPromise } from 'xstate/actors';
16+
17+
const machine = createMachine(
18+
{
19+
// ...
20+
invoke: {
21+
src: 'fetchFromAPI'
22+
}
23+
},
24+
{
25+
- services: {
26+
+ actors: {
27+
- fetchFromAPI: (context, event) => {
28+
+ fetchFromAPI: (context, event) => fromPromise(() => {
29+
// ... (return a promise)
30+
})
31+
}
32+
}
33+
);
34+
```

.changeset/tricky-spoons-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@xstate/react': major
3+
---
4+
5+
Removed `getSnapshot` parameter from hooks. It is expected that the received `actorRef` has to have a `getSnapshot` method on it that can be used internally.

docs/packages/xstate-react/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Example: the `'fetchData'` service and `'notifySuccess'` action are both configu
358358

359359
```js
360360
import { createMachine } from 'xstate';
361-
import { invokePromise } from 'xstate/invoke';
361+
import { fromPromise } from 'xstate/actors';
362362

363363
const fetchMachine = createMachine({
364364
id: 'fetch',
@@ -406,9 +406,10 @@ const Fetcher = ({ onResolve }) => {
406406
notifySuccess: (ctx) => onResolve(ctx.data)
407407
},
408408
actors: {
409-
fetchData: invokePromise((_, event) =>
410-
fetch(`some/api/${event.query}`).then((res) => res.json())
411-
)
409+
fetchData: (_, event) =>
410+
fromPromise(() =>
411+
fetch(`some/api/${event.query}`).then((res) => res.json())
412+
)
412413
}
413414
});
414415

packages/core/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// ...
1515
const loginService = interpret(loginMachine).start();
1616

17-
const loggedInState = await waitFor(loginService, state =>
17+
const loggedInState = await waitFor(loginService, (state) =>
1818
state.hasTag('loggedIn')
1919
);
2020

0 commit comments

Comments
 (0)