-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Required input for
spawn
when defined inside referenced actor (#5139)
* Make `spawn` input required when defined (with tests) * changeset `spawn` input required
- Loading branch information
1 parent
f6f0a64
commit bf6119a
Showing
5 changed files
with
155 additions
and
48 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,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 } } | ||
) | ||
}) | ||
}); | ||
``` |
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
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(); | ||
}); | ||
}); |
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,49 @@ | ||
import { ActorRefFrom, assign, createMachine } from '../src'; | ||
|
||
describe('spawn inside machine', () => { | ||
it('input is required when defined in actor', () => { | ||
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) | ||
})) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); |