-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[v5] Input #3815
[v5] Input #3815
Conversation
🦋 Changeset detectedLatest commit: 718da10 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 718da10:
|
|
||
const Test = () => { | ||
const [state] = useMachine(testMachine, { | ||
input: { test: true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add the same test to all other integration packages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you thinking of adding a way to get/set the type of |
As in with something like |
More in the sense of making it part of a machine's public api so you can expect a certain type of input data that's consumed |
Ah, ye - the types for this will come in a followup PR |
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
packages/core/src/StateMachine.ts
Outdated
// TODO: this getter should be removed | ||
public get context(): TContext { | ||
return this.getContextAndActions()[0]; | ||
public getContext(input?: any): TContext { | ||
return this.getContextAndActions(input)[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts about removing this getter altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with it as long as we can make all the tests pass still 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's do this in a follow up work as this is just a symptom of a bigger story
packages/core/src/types.ts
Outdated
string, | ||
ActorBehaviorCreator<TContext, TEvent> | AnyActorBehavior | ||
>; | ||
actors: Record<string, AnyActorBehavior>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we might see in the diff here - now actors won't have access to TContext
and TEvent
. So it's not possible to "configure" their dynamic part (input) in the implementations object. That's only possible within the config itself, through the invoke.input
property. However, at the moment, we are not able to provide typegen-based information there so this might get annoying to people.
WDYT about allowing this to be:
// it's not a complete final type, just an illustration for this accepting `{ src, input }`
actors: Record<string, AnyActorBehavior | { src: AnyActorBehavior, input: InputCreator<TContext, TEvent> }>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that input
might be different depending on where you come from:
state1: {
invoke: {
src: 'something',
input: (ctx) => /* get from context */
}
}
state2: {
invoke: {
src: 'something',
input: (_ctx, ev) => /* get from event */
}
}
I don't understand the typegen information; invoke.input
is only relevant to the invoked actor, not to the machine (which is the scope of typegen); can you clarify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conceptual TInput
always comes from a given actor type and is the same regardless of where and how it is instantiated. What might be different between instantiation sites, are the "arguments" used to resolve that same type. But that's exactly the same as with any other implementation type:
state1: {
entry: 'something'
}
state2: {
entry: 'something'
}
The final implementation of something
just has to deal with all possible argument types. It's free to ignore context
completely, and it's free to ignore event
- but when it starts using the event
, it just has to assume that it's a union of all possible event types that can lead to triggering this specific action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should be the resolution for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose this: #3897
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
* Remove `withContext` and `.provide({ context })` * Remove input property from the StateMachine * remove unused things * remove another unused import
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
* Allow input to be referenced in the implementations * Allow dynamic parametrized inputs to be used together with spawn * make tests less dirty Co-authored-by: David Khourshid <davidkpiano@gmail.com> * Update packages/core/src/StateMachine.ts --------- Co-authored-by: David Khourshid <davidkpiano@gmail.com>
This PR adds
input
to the second argument of theinterpret(...)
function. This passes input data in the"xstate.init"
event:Addresses STA-3438