-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Required input for spawn
when defined inside referenced actor
#5139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
'xstate': patch | ||
--- | ||
|
||
Make `spawn` input required when defined inside referenced actor: | ||
|
||
```ts | ||
const childMachine = createMachine({ | ||
types: { input: {} as { value: number } } | ||
}); | ||
|
||
const machine = createMachine({ | ||
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } }, | ||
context: ({ spawn }) => ({ | ||
ref: spawn( | ||
childMachine, | ||
// Input is now required! | ||
{ input: { value: 42 } } | ||
) | ||
}) | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ActorRefFrom, createActor, createMachine } from '../src'; | ||
|
||
describe('spawn inside machine', () => { | ||
it('input is required when defined in actor', () => { | ||
const childMachine = createMachine({ | ||
types: { input: {} as { value: number } } | ||
}); | ||
const machine = createMachine({ | ||
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } }, | ||
context: ({ spawn }) => ({ | ||
ref: spawn(childMachine, { input: { value: 42 }, systemId: 'test' }) | ||
}) | ||
}); | ||
|
||
const actor = createActor(machine).start(); | ||
expect(actor.system.get('test')).toBeDefined(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ActorRefFrom, assign, createMachine } from '../src'; | ||
|
||
describe('spawn inside machine', () => { | ||
it('input is required when defined in actor', () => { | ||
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. we don't really check that it's required here, u just pass an input as an argument but the same test would pass without the patch introduced by this PR 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. Should this be inverted with a |
||
const childMachine = createMachine({ | ||
types: { input: {} as { value: number } } | ||
}); | ||
createMachine({ | ||
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } }, | ||
context: ({ spawn }) => ({ | ||
ref: spawn(childMachine, { input: { value: 42 } }) | ||
}), | ||
initial: 'idle', | ||
states: { | ||
Idle: { | ||
on: { | ||
event: { | ||
actions: assign(({ spawn }) => ({ | ||
ref: spawn(childMachine, { input: { value: 42 } }) | ||
})) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
it('input is not required when not defined in actor', () => { | ||
const childMachine = createMachine({}); | ||
createMachine({ | ||
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } }, | ||
context: ({ spawn }) => ({ | ||
ref: spawn(childMachine) | ||
}), | ||
initial: 'idle', | ||
states: { | ||
Idle: { | ||
on: { | ||
some: { | ||
actions: assign(({ spawn }) => ({ | ||
ref: spawn(childMachine) | ||
})) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
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 return type errors without this bypass. I think this is expected given that typescript has not enough information to correctly type-check all possible configurations.
All tests are passing, so I think it shouldn’t be an issue (the update is mostly on the types).