session.ts
⎇
Raw
1import { createContext, useContext } from "solid-js";
2import type { Accessor } from "solid-js";
3import type { Me, PersonState } from "./api";
4
5/**
6 * What the routed pages need from the shell.
7 *
8 * A context rather than props because route components are constructed by the
9 * router and cannot be handed anything. It holds the *live* store, so both pages
10 * share one polling loop rather than starting one each.
11 */
12export interface SessionValue {
13 me: Me;
14 people: Record<number, PersonState>;
15 order: Accessor<number[]>;
16 serverTime: Accessor<number>;
17 selected: Accessor<number | null>;
18 select: (id: number) => void;
19 trail: Accessor<[number, number][] | null>;
20}
21
22export const SessionContext = createContext<SessionValue>();
23
24export function useSession(): SessionValue {
25 const value = useContext(SessionContext);
26 if (!value) throw new Error("useSession outside the shell");
27 return value;
28}
29