-
-
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.
* Tests * Rename * Convert some types * Add setup function to exports * Rework setup inference and specifically guard inferences to aid higher order guards (#4474) * Rework setup inference and specifically guard inferences to aid higher order guards * move tests * add extra runtime tests for guards execution * fixed `not` types * shift one extra type-level test * Remove `TParams` from `choose` and `pure` * Remove generics provided by `setup` from `setup().createMachine` * Resolve `TypegenDisabled` type in `setup` * Removed unused type parameters from `InternalMachineImplementations` * Add changeset --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
- Loading branch information
1 parent
6777c32
commit a3a11c8
Showing
19 changed files
with
1,302 additions
and
185 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,46 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
You can now use the `setup({ ... }).createMachine({ ... })` function to setup implementations for `actors`, `actions`, `guards`, and `delays` that will be used in the created machine: | ||
|
||
```ts | ||
import { setup, createMachine } from 'xstate'; | ||
|
||
const fetchUser = fromPromise(async ({ input }) => { | ||
const response = await fetch(`/user/${input.id}`); | ||
const user = await response.json(); | ||
return user; | ||
}); | ||
|
||
const machine = setup({ | ||
actors: { | ||
fetchUser | ||
}, | ||
actions: { | ||
clearUser: assign({ user: undefined }) | ||
}, | ||
guards: { | ||
isUserAdmin: (_, params) => params.user.role === 'admin' | ||
} | ||
}).createMachine({ | ||
// ... | ||
invoke: { | ||
// Strongly typed! | ||
src: 'fetchUser', | ||
input: ({ context }) => ({ id: context.userId }), | ||
onDone: { | ||
guard: { | ||
type: 'isUserAdmin', | ||
params: ({ context }) => ({ user: context.user }) | ||
}, | ||
target: 'success', | ||
actions: assign({ user: ({ event }) => event.output }) | ||
}, | ||
onError: { | ||
target: 'failure', | ||
actions: 'clearUser' | ||
} | ||
} | ||
}); | ||
``` |
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
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
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
Oops, something went wrong.