Skip to content

Commit

Permalink
Add/tweak changesets and apply the same fix for Vue and Svelte libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Aug 27, 2021
1 parent 49abc07 commit fe3e859
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
13 changes: 12 additions & 1 deletion .changeset/little-teachers-sing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@
'xstate': patch
---

Allow for lazy context in machine.withConfig
author: @farskid
author: @Andarist

Adjusted TS type definitions of the `withContext` and `withConfig` methods so that they accept "lazy context" now.

Example:

```js
const copy = machine.withContext(() => ({
ref: spawn(() => {})
}));
```
19 changes: 19 additions & 0 deletions .changeset/sixty-shoes-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@xstate/react': patch
'@xstate/vue': patch
'@xstate/svelte': patch
---

author: @farskid
author: @Andarist

Fixed an issue with actors not being spawned correctly by `useMachine` and `useInterpret` when they were defined a lazily evaluated context, like for example here:

```js
createMachine({
// lazy context
context: () => ({
ref: spawn(() => {})
})
});
```
2 changes: 1 addition & 1 deletion packages/xstate-react/test/useMachine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ describe('useMachine hook', () => {
it('should successfully spawn actors from the lazily declared context', () => {
let childSpawned = false;

const machine = createMachine({
createMachine({
context: () => ({
ref: spawn(() => {
childSpawned = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/xstate-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export function useMachine<
delays
};

const resolvedMachine = machine.withConfig(machineConfig, {
const resolvedMachine = machine.withConfig(machineConfig, () => ({
...machine.context,
...context
} as TContext);
}));

const service = interpret(resolvedMachine, interpreterOptions).start(
rehydratedState ? new State(rehydratedState) : undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/xstate-vue/src/useInterpret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export function useInterpret<
delays
};

const machineWithConfig = machine.withConfig(machineConfig, {
const machineWithConfig = machine.withConfig(machineConfig, () => ({
...machine.context,
...context
} as TContext);
}));

const service = interpret(machineWithConfig, interpreterOptions).start(
rehydratedState ? (State.create(rehydratedState) as any) : undefined
Expand Down

0 comments on commit fe3e859

Please sign in to comment.