-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add changeset + clean up invoke.test.ts
- Loading branch information
1 parent
5569d75
commit 390eaaa
Showing
3 changed files
with
132 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
'xstate': major | ||
--- | ||
|
||
**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: | ||
|
||
```diff | ||
import { createMachine } from 'xstate'; | ||
+import { invokeActivity } from 'xstate/invoke'; | ||
|
||
const machine = createMachine( | ||
{ | ||
// ... | ||
- activities: 'someActivity', | ||
+ invoke: { | ||
+ src: 'someActivity' | ||
+ } | ||
}, | ||
{ | ||
- activities: { | ||
+ behaviors: { | ||
- someActivity: ((context, event) => { | ||
+ someActivity: invokeActivity((context, event) => { | ||
// ... some continuous activity | ||
|
||
return () => { | ||
// dispose activity | ||
} | ||
}) | ||
} | ||
} | ||
); | ||
``` | ||
|
||
**Breaking:** The `services` option passed as the second argument to `createMachine(config, options)` is renamed to `behaviors`. Each value in `behaviors`should be a function that takes in `context` and `event` and returns a [behavior](TODO: link). The provided invoke creators are: | ||
|
||
- `invokeActivity` | ||
- `invokePromise` | ||
- `invokeCallback` | ||
- `invokeObservable` | ||
- `invokeMachine` | ||
|
||
```diff | ||
import { createMachine } from 'xstate'; | ||
+import { invokePromise } from 'xstate/invoke'; | ||
|
||
const machine = createMachine( | ||
{ | ||
// ... | ||
invoke: { | ||
src: 'fetchFromAPI' | ||
} | ||
}, | ||
{ | ||
- services: { | ||
+ behaviors: { | ||
- fetchFromAPI: ((context, event) => { | ||
+ fetchFromAPI: invokePromise((context, event) => { | ||
// ... (return a promise) | ||
}) | ||
} | ||
} | ||
); | ||
``` | ||
|
||
**Breaking:** The `state.children` property is now a mapping of invoked actor IDs to their `ActorRef` instances. | ||
|
||
**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. | ||
|
||
**Breaking:** The `spawn` function is no longer imported globally. Spawning actors is now done inside of `assign(...)`, as seen below: | ||
|
||
```diff | ||
-import { createMachine, spawn } from 'xstate'; | ||
+import { createMachine } from 'xstate'; | ||
|
||
const machine = createMachine({ | ||
// ... | ||
entry: assign({ | ||
- someRef: (context, event) => { | ||
+ someRef: (context, event, { spawn }) => { | ||
- return spawn(somePromise); | ||
+ return spawn.from(somePromise); | ||
} | ||
}) | ||
}); | ||
|
||
``` | ||
|
||
**Breaking:** The `src` of an `invoke` config is now either a string that references the machine's `options.behaviors`, or a `BehaviorCreator`, which is a function that takes in `context` and `event` and returns a `Behavior`: | ||
|
||
```diff | ||
import { createMachine } from 'xstate'; | ||
+import { invokePromise } from 'xstate/invoke'; | ||
|
||
const machine = createMachine({ | ||
// ... | ||
invoke: { | ||
- src: (context, event) => somePromise | ||
+ src: invokePromise((context, event) => somePromise) | ||
} | ||
// ... | ||
}); | ||
``` | ||
|
||
**Breaking:** The `origin` of an `SCXML.Event` is no longer a string, but an `ActorRef` instance, which can be serialized into a string. | ||
This comment has been minimized.
Sorry, something went wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
how to serialize it into a string? can the serialized string be used anyhow?