diff --git a/pkg/client/src/components/Table.tsx b/pkg/client/src/components/Table.tsx index 770ae44..1711849 100644 --- a/pkg/client/src/components/Table.tsx +++ b/pkg/client/src/components/Table.tsx @@ -1,6 +1,6 @@ import type { TWsIn, TWsOut } from "@games/server/src/table"; import games from "@games/shared/games/index"; -import { fromPromise, pool, Property, Stream } from "kefir"; +import { pool, Property, Stream } from "kefir"; import { Accessor, createContext, @@ -43,8 +43,10 @@ export const TableContext = createContext<{ export default (props: { tableKey: string }) => { // #region Websocket declaration + let ws: ReturnType["subscribe"]> | undefined = + undefined; const wsEvents = pool(); - let sendWs: (msg: TWsIn) => void = () => {}; + const sendWs = (msg: TWsIn) => ws?.send(msg); // #endregion @@ -88,15 +90,14 @@ export default (props: { tableKey: string }) => { const [ready, setReady] = createSignal(false); onMount(() => { - const ws = api.ws(props).subscribe(); + ws = api.ws(props).subscribe(); ws.on("open", () => { wsEvents.plug(fromWebsocket(ws)); - sendWs = ws.send.bind(ws); createEffect(() => sendWs({ ready: ready() })); createEffect(() => sendWs({ name: name() })); }); - onCleanup(() => ws.close()); + onCleanup(() => ws?.close()); }); const GamePicker = () => {