Skip to content

Commit

Permalink
Merge pull request #20 from statelyai/mellson/hacky-fix-for-generics
Browse files Browse the repository at this point in the history
Fix the generic typing
  • Loading branch information
mellson authored Dec 13, 2023
2 parents 2afbb11 + 11b5d9b commit 9d35220
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/weak-eagles-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@statelyai/sky-react': patch
'@statelyai/sky': patch
---

Fix the generic typing.
4 changes: 2 additions & 2 deletions packages/sky-core/src/actorFromStately.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function actorFromStately<T extends AnyStateMachine>(
onPlayerLeft?: ({ numberOfPlayers }: { numberOfPlayers: number }) => void;
},
skyConfig?: SkyConfigFile<T>,
) {
): Promise<Actor<T>> {
if (!skyConfig) {
throw new Error(
`You need to run xstate sky "src/**/*.ts?(x)" before you can use the Stately Sky actor with url ${url}`,
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function actorFromStately<T extends AnyStateMachine>(
}
case 'actor.start': {
// Start the actor with the initial value from Sky
actor = createActor(machine as any, {
actor = createActor(machine as never, {
snapshot: skyEvent.snapshot,
});

Expand Down
18 changes: 15 additions & 3 deletions packages/sky-react/src/useStatelyActor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { SkyConfigFile, actorFromStately } from '@statelyai/sky';
import { useSelector } from '@xstate/react';
import { useEffect, useState } from 'react';
import { Actor, AnyStateMachine, createActor, fromPromise } from 'xstate';
import {
Actor,
AnyStateMachine,
EventFromLogic,
SnapshotFrom,
createActor,
fromPromise,
} from 'xstate';

export function useStatelyActor<T extends AnyStateMachine>(
options: Parameters<typeof actorFromStately>[0],
skyConfig?: SkyConfigFile<T>,
) {
): readonly [
SnapshotFrom<T>,
((event: EventFromLogic<T>) => void) | undefined,
Actor<T> | undefined,
{ isConnecting: boolean },
] {
if (!skyConfig) {
throw new Error(
`You need to run xstate sky "src/**/*.ts?(x)" before you can use the Stately Sky actor with url ${options.url}`,
Expand All @@ -15,7 +27,7 @@ export function useStatelyActor<T extends AnyStateMachine>(

const [maybeActor, setMaybeActor] = useState<Actor<T>>();
const state = useSelector(
maybeActor ?? createActor(skyConfig.machine as any),
maybeActor ?? createActor(skyConfig.machine as never),
(snapshot) => snapshot,
);

Expand Down

0 comments on commit 9d35220

Please sign in to comment.