Skip to content

Commit

Permalink
Merge pull request #8 from statelyai/mellson/sta-5416-sky-api-v005
Browse files Browse the repository at this point in the history
Add number of players to the API
  • Loading branch information
mellson authored Oct 27, 2023
2 parents 2b17400 + 7c9c673 commit 51d7e33
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"changeset": "changeset",
"release": "changeset publish",
"version": "changeset version",
"copy-sky": "cp -r packages/sky-core/ ../studio/node_modules/@statelyai/sky",
"copy-sky-react": "cp -r packages/sky-react/ ../studio/node_modules/@statelyai/sky-react",
"copy-sky": "cp -r packages/sky-core/ ../sky-starter-app/node_modules/@statelyai/sky",
"copy-sky-react": "cp -r packages/sky-react/ ../sky-starter-app/node_modules/@statelyai/sky-react",
"dev": "yarn build && yarn copy-sky && yarn copy-sky-react"
},
"preconstruct": {
Expand Down
6 changes: 3 additions & 3 deletions packages/sky-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statelyai/sky",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"main": "dist/statelyai-sky.cjs.js",
"module": "dist/statelyai-sky.esm.js",
Expand Down Expand Up @@ -30,8 +30,8 @@
},
"homepage": "https://github.com/statelyai/sky/tree/main/packages/sky-core#readme",
"dependencies": {
"partysocket": "0.0.9",
"partysocket": "0.0.12",
"superjson": "1.13.1",
"xstate": "5.0.0-beta.33"
"xstate": "5.0.0-beta.37"
}
}
12 changes: 12 additions & 0 deletions packages/sky-core/src/actorFromStately.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export async function actorFromStately<T extends AnyStateMachine>(
url,
sessionId,
runOnSky = true,
onPlayerJoined,
onPlayerLeft,
}: {
apiKey?: string;
url: string;
sessionId: string;
runOnSky?: boolean;
onPlayerJoined?: ({ numberOfPlayers }: { numberOfPlayers: number }) => void;
onPlayerLeft?: ({ numberOfPlayers }: { numberOfPlayers: number }) => void;
},
skyConfig?: SkyConfigFile<T>,
) {
Expand Down Expand Up @@ -73,6 +77,14 @@ export async function actorFromStately<T extends AnyStateMachine>(
const skyEvent = superjson.parse<SkyServerEvent>(evt.data);
if (skyEvent.apiKey !== apiKey) return;
switch (skyEvent.type) {
case 'player.joined': {
onPlayerJoined?.({ numberOfPlayers: skyEvent.numberOfPlayers });
break;
}
case 'player.left': {
onPlayerLeft?.({ numberOfPlayers: skyEvent.numberOfPlayers });
break;
}
case 'actor.error': {
throw new Error(skyEvent.error);
}
Expand Down
12 changes: 11 additions & 1 deletion packages/sky-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ type InternalSkyServerActorEvent =
| { type: 'actor.stop'; event: AnyEventObject }
| { type: 'actor.error'; error: string };

type MultiplayerSkyEvent = {
type: 'player.joined' | 'player.left';
numberOfPlayers: number;
};

export type SkyServerMultiplayerEvent = SafeSkyEvent & MultiplayerSkyEvent;

export type SkyServerActorEvent = SafeSkyEvent & InternalSkyServerActorEvent;

export type SkyServerEvent = SkyServerSimulateEvent | SkyServerActorEvent;
export type SkyServerEvent =
| SkyServerSimulateEvent
| SkyServerActorEvent
| SkyServerMultiplayerEvent;
6 changes: 3 additions & 3 deletions packages/sky-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statelyai/sky-react",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"main": "dist/statelyai-sky-react.cjs.js",
"module": "dist/statelyai-sky-react.esm.js",
Expand Down Expand Up @@ -30,9 +30,9 @@
},
"homepage": "https://github.com/statelyai/sky/tree/main/packages/sky-react#readme",
"dependencies": {
"@statelyai/sky": "0.0.4",
"@statelyai/sky": "0.0.5",
"@xstate/react": "4.0.0-beta.10",
"xstate": "5.0.0-beta.33"
"xstate": "5.0.0-beta.37"
},
"devDependencies": {
"@types/react": "^18.2.22",
Expand Down
34 changes: 9 additions & 25 deletions packages/sky-react/src/useStatelyActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@ import { useEffect, useState } from 'react';
import { Actor, AnyStateMachine, createActor, fromPromise } from 'xstate';

export function useStatelyActor<T extends AnyStateMachine>(
{
apiKey,
url,
sessionId,
runOnSky = true,
}: {
apiKey?: string;
url: string;
sessionId: string;
runOnSky?: boolean;
},
options: Parameters<typeof actorFromStately>[0],
skyConfig?: SkyConfigFile<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}`,
`You need to run xstate sky "src/**/*.ts?(x)" before you can use the Stately Sky actor with url ${options.url}`,
);
}

Expand All @@ -31,25 +21,19 @@ export function useStatelyActor<T extends AnyStateMachine>(

useEffect(() => {
const subscription = createActor(
fromPromise(() =>
actorFromStately(
{
url,
apiKey,
sessionId,
runOnSky,
},
skyConfig,
),
),
fromPromise(() => actorFromStately(options, skyConfig)),
)
.start()
.subscribe((s) => {
s.output?.start();
return setMaybeActor(s.output);
});
return () => subscription.unsubscribe();
}, [apiKey, runOnSky, sessionId, skyConfig, url]);
}, [options.url, options.sessionId, skyConfig]);

return [state, maybeActor?.send, maybeActor] as const;
const send = maybeActor?.send;
const isConnecting = send === undefined;

const sky = { isConnecting };
return [state, send, maybeActor, sky] as const;
}
18 changes: 8 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2445,12 +2445,10 @@ parse-json@^5.0.0, parse-json@^5.1.0:
json-parse-even-better-errors "^2.3.0"
lines-and-columns "^1.1.6"

partysocket@0.0.9:
version "0.0.9"
resolved "https://registry.yarnpkg.com/partysocket/-/partysocket-0.0.9.tgz#8c898ae5697bac76d3c3569c4671401c3b575cea"
integrity sha512-SVFtPseMU9DC7fd7MdMx/y9MgWFMteWBgL0wVDQ90nCRBzt/pD1x8ovIqsCFK7uo1KXOXvqJuf9vjTIGPw77Wg==
dependencies:
react "^18.2.0"
partysocket@0.0.12:
version "0.0.12"
resolved "https://registry.yarnpkg.com/partysocket/-/partysocket-0.0.12.tgz#97285d8047121fe82d8294f46dea037f579c5b22"
integrity sha512-20fikH08aHl7oJvRD7QLftzHvCEqxHf6y/ZiO0wltkl2cbNbtZQuDno9CLrqIQG2t22UWFHjiSqj/rT1In36Eg==

path-exists@^4.0.0:
version "4.0.0"
Expand Down Expand Up @@ -3216,10 +3214,10 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

xstate@5.0.0-beta.33:
version "5.0.0-beta.33"
resolved "https://registry.yarnpkg.com/xstate/-/xstate-5.0.0-beta.33.tgz#2f25ce90cc0d7b1c84a5183347f5cecb980e065e"
integrity sha512-zHwbY2d2GGrsIySUCybrlq6YAPGM20yKpvliroDqfSbwa255Z1d7RYLkbbxiLx8SnEwDpWVple7JTXkjOw3JLA==
xstate@5.0.0-beta.37:
version "5.0.0-beta.37"
resolved "https://registry.yarnpkg.com/xstate/-/xstate-5.0.0-beta.37.tgz#617dc79ec2dc6b30b2567da93c0559bf2d595b34"
integrity sha512-pbdl6piQ7H+KGd8TKxo5htomMkP85HXLVfTuUy6J0JMPvI8srum3YD+QINx3Ea4QAib8tqk28u70BNq2opnoIw==

y18n@^4.0.0:
version "4.0.3"
Expand Down

0 comments on commit 51d7e33

Please sign in to comment.