-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[v5] Actor types #4036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[v5] Actor types #4036
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
bf0f324
POC for actor types
davidkpiano a52fc3a
Merge branch 'next' into v5/machine-types-1
davidkpiano 2606909
Remove patterns.ts
davidkpiano c907217
Fix types sorta
davidkpiano cb88638
Remove ActorMap
davidkpiano e6f50ba
Add changeset
davidkpiano e423623
Merge branch 'next' into v5/machine-types-1
davidkpiano 1e4ba5d
Improve state.children
davidkpiano cbb0577
Add InputFrom and actor behavior input
davidkpiano 18aca45
Types WIP
davidkpiano 10e66cc
Merge branch 'next' into v5/machine-types-1
davidkpiano 3f3abe1
PromiseActorEvents
davidkpiano 6c14671
Undo
davidkpiano 2e9ec56
Merge branch 'next' into v5/machine-types-1
davidkpiano 491c659
Fix most type errors
davidkpiano 78929ec
Get types passing
davidkpiano 312d47d
Merge branch 'next' into v5/machine-types-1
davidkpiano eea6aae
Merge branch 'next' into v5/machine-types-1
davidkpiano e4f4c5e
Update packages/xstate-solid/test/useMachine.test.tsx
davidkpiano c3fd7eb
Merge branch 'next' into v5/machine-types-1
davidkpiano 704612c
Change changeset
davidkpiano 4f37bac
Merge branch 'v5/machine-types-1' of https://github.com/statelyai/xst…
davidkpiano 494f2aa
Fix types
davidkpiano c30336a
Fix test type
davidkpiano 6db1fbe
Clean up ActorImpl types
davidkpiano 328c57a
Fix import
davidkpiano 47aab9f
Merge branch 'next' into v5/machine-types-1
davidkpiano 53db8f8
Fix import
davidkpiano df6fb49
Merge branch 'next' into v5/machine-types-1
davidkpiano ba16573
Fix
davidkpiano 21eb12b
Merge branch 'next' into v5/machine-types-1
davidkpiano c86c7ef
Merge branch 'next' into v5/machine-types-1
davidkpiano 4d8ed11
Merge branch 'next' into v5/machine-types-1
davidkpiano 3a6eda6
Merge branch 'next' into v5/machine-types-1
davidkpiano 29655d7
Merge branch 'next' into v5/machine-types-1
davidkpiano 45d3ba0
Merge remote-tracking branch 'origin/next' into v5/machine-types-1
Andarist 6da96cf
Tweak how `TActors` are passed around
Andarist fa4c398
Stick to the `.logic` prop
Andarist 3fe8ec1
add some type tests for actor types
Andarist 1ffb8e6
tweak things
Andarist d702291
Removed TActions from state
davidkpiano 181c9df
Merge branch 'next' into v5/machine-types-1
davidkpiano 30e628e
Fix Svelte type issues
davidkpiano a017d25
Merge remote-tracking branch 'origin/next' into v5/machine-types-1
Andarist 08dc423
Add `TInput` to `TransitionActorLogic`
Andarist b946e4c
tweak small things
Andarist 6860904
require actor IDs when they are configured
Andarist d377232
small tweaks
Andarist 06927eb
Tweak changeset
Andarist 7a16521
Tweak how actor type is distributed in `InvokeConfig`
Andarist b37fe82
disallow inline actors when the actor type is provided
Andarist 2e2bdd6
Fix `GenerateActorEvents`
Andarist d5b63e3
Fixed `TInvokeSrcNameMap` usage in `GenerateActorEvents`
Andarist 9861bed
fixed tests
Andarist ca81cc6
Add comments explaining `GenerateActorEvents`
Andarist 819954e
fixed test
Andarist edbf505
remove old hack
Andarist 041b40b
Compute `State['children']` more accurately
Andarist 2e7a58b
fixed ComputeChildren type
Andarist 76b3fb7
Add test and fix the children being a union
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,29 @@ | ||
| --- | ||
| 'xstate': major | ||
| --- | ||
|
|
||
| Actor types can now be specified in the `.types` property of `createMachine`: | ||
|
|
||
| ```ts | ||
| const fetcher = fromPromise(() => fetchUser()); | ||
|
|
||
| const machine = createMachine({ | ||
| types: {} as { | ||
| actors: { | ||
| src: 'fetchData'; // src name (inline behaviors ideally inferred) | ||
| id: 'fetch1' | 'fetch2'; // possible ids (optional) | ||
| logic: typeof fetcher; | ||
| }; | ||
| }, | ||
| invoke: { | ||
| src: 'fetchData', // strongly typed | ||
| id: 'fetch2', // strongly typed | ||
| onDone: { | ||
| actions: ({ event }) => { | ||
| event.output; // strongly typed as { result: string } | ||
| } | ||
| }, | ||
| input: { foo: 'hello' } // strongly typed | ||
| } | ||
| }); | ||
| ``` | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,7 +9,8 @@ import { | |
| } from './stateUtils.ts'; | ||
| import { TypegenDisabled, TypegenEnabled } from './typegenTypes.ts'; | ||
| import type { | ||
| ActorRef, | ||
| ProvidedActor, | ||
| ActorRefFrom, | ||
| AnyState, | ||
| AnyStateMachine, | ||
| EventObject, | ||
|
|
@@ -18,10 +19,36 @@ import type { | |
| PersistedMachineState, | ||
| Prop, | ||
| StateConfig, | ||
| StateValue | ||
| StateValue, | ||
| TODO, | ||
| AnyActorRef, | ||
| Compute | ||
| } from './types.ts'; | ||
| import { flatten, matchesState } from './utils.ts'; | ||
|
|
||
| type ComputeConcreteChildren<TActor extends Required<ProvidedActor>> = { | ||
| [K in TActor['id']]?: ActorRefFrom<(TActor & { id: K })['logic']>; | ||
| }; | ||
|
|
||
| type ComputeChildren<TActor extends ProvidedActor> = | ||
| string extends TActor['src'] | ||
| ? // TODO: replace with UnknownActorRef~ | ||
| // TODO: consider adding `| undefined` here | ||
| Record<string, AnyActorRef> | ||
| : Compute< | ||
| // distribute over union | ||
| ComputeConcreteChildren<Extract<TActor, { id: string }>> & | ||
| // check if all actors have IDs | ||
| (undefined extends TActor['id'] | ||
| ? // if they don't we need to create an index signature containing all possible actor types | ||
| { | ||
| [id: string]: TActor extends any | ||
| ? ActorRefFrom<TActor['logic']> | undefined | ||
| : never; | ||
| } | ||
| : {}) | ||
| >; | ||
|
|
||
| export function isStateConfig< | ||
| TContext extends MachineContext, | ||
| TEvent extends EventObject | ||
|
|
@@ -39,7 +66,8 @@ export function isStateConfig< | |
| export const isState = isStateConfig; | ||
| export class State< | ||
| TContext extends MachineContext, | ||
| TEvent extends EventObject = EventObject, | ||
| TEvent extends EventObject, | ||
| TActor extends ProvidedActor, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this shouldn't receive |
||
| TResolvedTypesMeta = TypegenDisabled | ||
| > { | ||
| public tags: Set<string>; | ||
|
|
@@ -63,7 +91,8 @@ export class State< | |
| /** | ||
| * An object mapping actor names to spawned/invoked actors. | ||
| */ | ||
| public children: Record<string, ActorRef<any>>; | ||
| public children: ComputeChildren<TActor>; | ||
|
|
||
| /** | ||
| * Creates a new State instance for the given `stateValue` and `context`. | ||
| * @param stateValue | ||
|
|
@@ -73,13 +102,13 @@ export class State< | |
| TContext extends MachineContext, | ||
| TEvent extends EventObject = EventObject | ||
| >( | ||
| stateValue: State<TContext, TEvent, any> | StateValue, | ||
| stateValue: State<TContext, TEvent, TODO, any> | StateValue, | ||
| context: TContext = {} as TContext, | ||
| machine: AnyStateMachine | ||
| ): State<TContext, TEvent, any> { | ||
| ): State<TContext, TEvent, TODO, any> { | ||
| if (stateValue instanceof State) { | ||
| if (stateValue.context !== context) { | ||
| return new State<TContext, TEvent>( | ||
| return new State<TContext, TEvent, TODO, any>( | ||
| { | ||
| value: stateValue.value, | ||
| context, | ||
|
|
@@ -98,7 +127,7 @@ export class State< | |
| getStateNodes(machine.root, stateValue) | ||
| ); | ||
|
|
||
| return new State<TContext, TEvent>( | ||
| return new State<TContext, TEvent, TODO, any>( | ||
| { | ||
| value: stateValue, | ||
| context, | ||
|
|
@@ -127,7 +156,7 @@ export class State< | |
| this.configuration = | ||
| config.configuration ?? | ||
| Array.from(getConfiguration(getStateNodes(machine.root, config.value))); | ||
| this.children = config.children; | ||
| this.children = config.children as any; | ||
|
|
||
| this.value = getStateValue(machine.root, this.configuration); | ||
| this.tags = new Set(flatten(this.configuration.map((sn) => sn.tags))); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.