Skip to content
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

Throw errors from actor snapshots to trigger error boundaries in React #4663

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ Read [📽 the slides](http://slides.com/davidkhourshid/finite-state-machines) (

## Packages

| Package | Description |
| --------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| 🤖 `xstate` | Core finite state machine and statecharts library + interpreter |
| [📉 `@xstate/graph`](https://github.com/statelyai/xstate/tree/main/packages/xstate-graph) | Graph traversal and model-based testing utilities using XState |
| [⚛️ `@xstate/react`](https://github.com/statelyai/xstate/tree/main/packages/xstate-react) | React hooks and utilities for using XState in React applications |
| [💚 `@xstate/vue`](https://github.com/statelyai/xstate/tree/main/packages/xstate-vue) | Vue composition functions and utilities for using XState in Vue applications |
| [🎷 `@xstate/svelte`](https://github.com/statelyai/xstate/tree/main/packages/xstate-svelte) | Svelte utilities for using XState in Svelte applications |
| [🥏 `@xstate/solid`](https://github.com/statelyai/xstate/tree/main/packages/xstate-solid) | Solid hooks and utilities for using XState in Solid applications |
| [🔍 `@statelyai/inspect`](https://github.com/statelyai/inspect) | Inspection utilities for XState |
| Package | Description |
| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| 🤖 `xstate` | Core finite state machine and statecharts library + interpreter |
| [📉 `@xstate/graph`](https://github.com/statelyai/xstate/tree/main/packages/xstate-graph) | Graph traversal and model-based testing utilities using XState |
| [⚛️ `@xstate/react`](https://github.com/statelyai/xstate/tree/main/packages/xstate-react) | React hooks and utilities for using XState in React applications |
| [💚 `@xstate/vue`](https://github.com/statelyai/xstate/tree/main/packages/xstate-vue) | Vue composition functions and utilities for using XState in Vue applications |
| [🎷 `@xstate/svelte`](https://github.com/statelyai/xstate/tree/main/packages/xstate-svelte) | Svelte utilities for using XState in Svelte applications |
| [🥏 `@xstate/solid`](https://github.com/statelyai/xstate/tree/main/packages/xstate-solid) | Solid hooks and utilities for using XState in Solid applications |
| [🔍 `@statelyai/inspect`](https://github.com/statelyai/inspect) | Inspection utilities for XState |

## Finite State Machines

Expand Down
10 changes: 9 additions & 1 deletion packages/xstate-react/src/useActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Actor,
ActorOptions,
AnyActorLogic,
Snapshot,
SnapshotFrom,
type ConditionalRequired,
type IsNotNever,
Expand Down Expand Up @@ -43,7 +44,10 @@ export function useActor<TLogic extends AnyActorLogic>(

const subscribe = useCallback(
(handleStoreChange: () => void) => {
const { unsubscribe } = actorRef.subscribe(handleStoreChange);
const { unsubscribe } = actorRef.subscribe(
handleStoreChange,
handleStoreChange
);
return unsubscribe;
},
[actorRef]
Expand All @@ -55,6 +59,10 @@ export function useActor<TLogic extends AnyActorLogic>(
getSnapshot
);

if ((actorSnapshot as Snapshot<any>).status === 'error') {
throw (actorSnapshot as Snapshot<any>).error;
}

useEffect(() => {
actorRef.start();

Expand Down
22 changes: 18 additions & 4 deletions packages/xstate-react/src/useActorRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AnyActorLogic,
AnyStateMachine,
Observer,
Snapshot,
SnapshotFrom,
createActor,
toObserver,
Expand Down Expand Up @@ -52,6 +53,8 @@ export function useIdleActorRef<TLogic extends AnyActorLogic>(
return actorRef;
}

const UNIQUE = {};

export function useActorRef<TLogic extends AnyActorLogic>(
machine: TLogic,
...[options, observerOrListener]: IsNotNever<
Expand All @@ -73,12 +76,23 @@ export function useActorRef<TLogic extends AnyActorLogic>(
]
): Actor<TLogic> {
const actorRef = useIdleActorRef(machine, options);
const [reactError, setReactError] = useState(() => {
const initialSnapshot: Snapshot<any> = actorRef.getSnapshot();
return initialSnapshot.status === 'error' ? initialSnapshot.error : UNIQUE;
});

if (reactError !== UNIQUE) {
throw reactError;
}

useEffect(() => {
if (!observerOrListener) {
return;
}
let sub = actorRef.subscribe(toObserver(observerOrListener));
const observer = toObserver(observerOrListener);
const errorListener = observer.error;
observer.error = (error) => {
setReactError(error);
errorListener?.(error);
};
let sub = actorRef.subscribe(observer);
return () => {
sub.unsubscribe();
};
Expand Down
18 changes: 15 additions & 3 deletions packages/xstate-react/src/useSelector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
import { AnyActorRef, SnapshotFrom } from 'xstate';
import { AnyActorRef, Snapshot, SnapshotFrom } from 'xstate';

type SyncExternalStoreSubscribe = Parameters<
typeof useSyncExternalStoreWithSelector
Expand All @@ -27,19 +27,31 @@ export function useSelector<
if (!actor) {
return () => {};
}
const { unsubscribe } = actor.subscribe(handleStoreChange);
const { unsubscribe } = actor.subscribe(
handleStoreChange,
handleStoreChange
);
return unsubscribe;
},
[actor]
);

const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]);
const boundSelector: typeof selector = useCallback(
(snapshot: Snapshot<any> | undefined) => {
if (snapshot?.status === 'error') {
throw snapshot.error;
}
return selector(snapshot as never);
},
[selector]
);

const selectedSnapshot = useSyncExternalStoreWithSelector(
subscribe,
boundGetSnapshot,
boundGetSnapshot,
selector,
boundSelector,
compare
);

Expand Down