-
-
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.
* Update `@xstate/svelte` * prefer useActor * tweak things * fixed type errors * yet another type error * changesets
- Loading branch information
Showing
19 changed files
with
137 additions
and
119 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,15 @@ | ||
--- | ||
'@xstate/svelte': major | ||
--- | ||
|
||
The `useMachine(machine)` hook now returns `{ snapshot, send, actorRef }` instead of `{ state, send, service }`: | ||
|
||
```diff | ||
const { | ||
- state, | ||
+ snapshot, | ||
send, | ||
- service | ||
+ actorRef | ||
} = useMachine(machine); | ||
``` |
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,5 @@ | ||
--- | ||
'@xstate/svelte': minor | ||
--- | ||
|
||
The `useActorRef(logic)` and `useActor(logic)` hooks have been added. |
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { useActor } from './useActor.ts'; | ||
export { useActorRef } from './useActorRef.ts'; | ||
export { useMachine } from './useMachine.ts'; | ||
export { useSelector } from './useSelector.ts'; |
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,34 @@ | ||
import { Readable, readable } from 'svelte/store'; | ||
import { | ||
ActorOptions, | ||
AnyActorLogic, | ||
ActorRefFrom, | ||
EventFrom, | ||
SnapshotFrom, | ||
AnyActorRef | ||
} from 'xstate'; | ||
import { useActorRef } from './useActorRef'; | ||
|
||
export function useActor<TLogic extends AnyActorLogic>( | ||
logic: TLogic, | ||
options?: ActorOptions<TLogic> | ||
): { | ||
snapshot: Readable<SnapshotFrom<TLogic>>; | ||
send: (event: EventFrom<TLogic>) => void; | ||
actorRef: ActorRefFrom<TLogic>; | ||
} { | ||
const actorRef = useActorRef(logic, options) as AnyActorRef; | ||
|
||
let currentSnapshot = actorRef.getSnapshot(); | ||
|
||
const snapshot = readable(currentSnapshot, (set) => { | ||
return actorRef.subscribe((nextSnapshot) => { | ||
if (currentSnapshot !== nextSnapshot) { | ||
currentSnapshot = nextSnapshot; | ||
set(currentSnapshot); | ||
} | ||
}).unsubscribe; | ||
}); | ||
|
||
return { snapshot, send: actorRef.send, actorRef } as any; | ||
} |
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,11 @@ | ||
import { onDestroy } from 'svelte'; | ||
import { ActorOptions, ActorRefFrom, AnyActorLogic, createActor } from 'xstate'; | ||
|
||
export function useActorRef<TLogic extends AnyActorLogic>( | ||
logic: TLogic, | ||
options?: ActorOptions<TLogic> | ||
): ActorRefFrom<TLogic> { | ||
const actorRef = createActor(logic as any, options).start(); | ||
onDestroy(() => actorRef.stop()); | ||
return actorRef as any; | ||
} |
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 |
---|---|---|
@@ -1,76 +1,21 @@ | ||
import { onDestroy } from 'svelte'; | ||
import { Readable, readable } from 'svelte/store'; | ||
import { | ||
AnyStateMachine, | ||
AreAllImplementationsAssumedToBeProvided, | ||
InternalMachineImplementations, | ||
createActor, | ||
ActorOptions, | ||
StateFrom, | ||
Actor, | ||
ContextFrom | ||
ActorOptions | ||
} from 'xstate'; | ||
|
||
type Prop<T, K> = K extends keyof T ? T[K] : never; | ||
import { useActor } from './useActor'; | ||
|
||
type RestParams<TMachine extends AnyStateMachine> = | ||
AreAllImplementationsAssumedToBeProvided< | ||
TMachine['__TResolvedTypesMeta'] | ||
> extends false | ||
? [ | ||
options: ActorOptions<TMachine> & | ||
InternalMachineImplementations< | ||
ContextFrom<TMachine>, | ||
TMachine['__TResolvedTypesMeta'], | ||
true | ||
> | ||
] | ||
: [ | ||
options?: ActorOptions<TMachine> & | ||
InternalMachineImplementations< | ||
ContextFrom<TMachine>, | ||
TMachine['__TResolvedTypesMeta'] | ||
> | ||
]; | ||
|
||
type UseMachineReturn< | ||
TMachine extends AnyStateMachine, | ||
TInterpreter = Actor<TMachine> | ||
> = { | ||
state: Readable<StateFrom<TMachine>>; | ||
send: Prop<TInterpreter, 'send'>; | ||
service: TInterpreter; | ||
}; | ||
? [options: ActorOptions<TMachine>] | ||
: [options?: ActorOptions<TMachine>]; | ||
|
||
/** @deprecated */ | ||
export function useMachine<TMachine extends AnyStateMachine>( | ||
machine: TMachine, | ||
...[options = {}]: RestParams<TMachine> | ||
): UseMachineReturn<TMachine> { | ||
const { guards, actions, actors, delays, ...interpreterOptions } = options; | ||
|
||
const machineConfig = { | ||
guards, | ||
actions, | ||
actors, | ||
delays | ||
}; | ||
|
||
const resolvedMachine = machine.provide(machineConfig as any); | ||
|
||
const service = createActor(resolvedMachine, interpreterOptions).start(); | ||
|
||
onDestroy(() => service.stop()); | ||
|
||
let snapshot = service.getSnapshot(); | ||
|
||
const state = readable(snapshot, (set) => { | ||
return service.subscribe((nextSnapshot) => { | ||
if (snapshot !== nextSnapshot) { | ||
snapshot = nextSnapshot; | ||
set(snapshot); | ||
} | ||
}).unsubscribe; | ||
}); | ||
|
||
return { state, send: service.send, service } as any; | ||
) { | ||
return useActor(machine, options); | ||
} |
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
24 changes: 14 additions & 10 deletions
24
...ages/xstate-svelte/test/UseMachine.svelte → packages/xstate-svelte/test/UseActor.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 |
---|---|---|
@@ -1,31 +1,35 @@ | ||
<script lang="ts"> | ||
export let persistedState: AnyMachineSnapshot | undefined = undefined; | ||
import { useMachine } from '@xstate/svelte'; | ||
import { useActor } from '@xstate/svelte'; | ||
import { fetchMachine } from './fetchMachine.ts'; | ||
import type { AnyMachineSnapshot } from 'xstate'; | ||
import { fromPromise } from 'xstate/actors'; | ||
const onFetch = () => | ||
new Promise<string>((res) => setTimeout(() => res('some data'), 50)); | ||
const { state, send } = useMachine(fetchMachine, { | ||
state: persistedState, | ||
actors: { | ||
fetchData: fromPromise(onFetch) | ||
const { snapshot, send } = useActor( | ||
fetchMachine.provide({ | ||
actors: { | ||
fetchData: fromPromise(onFetch) | ||
} | ||
}), | ||
{ | ||
state: persistedState | ||
} | ||
}); | ||
); | ||
</script> | ||
|
||
<div> | ||
{#if $state.matches('idle')} | ||
{#if $snapshot.matches('idle')} | ||
<button on:click={() => send({ type: 'FETCH' })}>Fetch</button> | ||
{:else if $state.matches('loading')} | ||
{:else if $snapshot.matches('loading')} | ||
<div>Loading...</div> | ||
{:else if $state.matches('success')} | ||
{:else if $snapshot.matches('success')} | ||
<div> | ||
Success! Data: | ||
<div data-testid="data">{$state.context.data}</div> | ||
<div data-testid="data">{$snapshot.context.data}</div> | ||
</div> | ||
{/if} | ||
</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
4 changes: 2 additions & 2 deletions
4
...hineNonPersistentSubscriptionChild.svelte → ...ctorNonPersistentSubscriptionChild.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
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
Oops, something went wrong.