-
-
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.
Tie Svelte's
useMachine
's lifecycle to the lifecycle of the compone…
…nt (#3172) * Tie Svelte's `useMachine`'s lifecycle to the lifecycle of the component * Add test and changeset * Fix the lifetime issue of the machine in `@xstate/svelte/fsm` too
- Loading branch information
Showing
6 changed files
with
87 additions
and
12 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,5 @@ | ||
--- | ||
'@xstate/svelte': patch | ||
--- | ||
|
||
Fixed an issue with the internal interpreter created by `useMachine` being unsubscribed when its subscribers' count went to zero. The lifetime of this interpreter should be bound to the lifetime of the component that has created it. |
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
33 changes: 33 additions & 0 deletions
33
packages/xstate-svelte/test/UseMachineNonPersistentSubcription.svelte
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,33 @@ | ||
<script lang="ts"> | ||
export let persistedState: AnyState | undefined = undefined; | ||
import { useMachine } from '../src'; | ||
import UseMachineNonPersistentSubcriptionChild from './UseMachineNonPersistentSubcriptionChild.svelte'; | ||
import type { AnyState } from 'xstate'; | ||
import { assign, createMachine } from 'xstate'; | ||
let visible = true; | ||
const machine = createMachine({ | ||
context: { | ||
count: 0 | ||
}, | ||
on: { | ||
INC: { | ||
actions: assign({ | ||
count: (ctx: { count: number }) => ++ctx.count | ||
}) | ||
} | ||
} | ||
}); | ||
const { state, send } = useMachine(machine); | ||
</script> | ||
|
||
<div> | ||
<button type="button" on:click={() => (visible = !visible)}>Toggle</button> | ||
{#if visible} | ||
<!-- inlined version of this doesn't unsubscribe from the store when the content gets hidden, so we need to keep this in a separate component --> | ||
<UseMachineNonPersistentSubcriptionChild {send} {state} /> | ||
{/if} | ||
</div> |
14 changes: 14 additions & 0 deletions
14
packages/xstate-svelte/test/UseMachineNonPersistentSubcriptionChild.svelte
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,14 @@ | ||
<script> | ||
export let send; | ||
export let state; | ||
</script> | ||
|
||
<div> | ||
<div data-testid="count">{$state.context.count}</div> | ||
<button | ||
type="button" | ||
on:click={() => { | ||
send({ type: 'INC' }); | ||
}}>Increment</button | ||
> | ||
</div> |
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