@hadoku/game-ui 0.1.64 → 0.1.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lazy.d.ts CHANGED
@@ -1,9 +1,6 @@
1
- import { type ComponentType, type ReactNode } from 'react';
2
- /** A component whose module loads on first draw. `preload()` starts it early and never rejects. */
3
- export interface LazyComponent<P> {
4
- (props: P): ReactNode;
5
- preload(): Promise<void>;
6
- }
1
+ import type { ComponentType } from 'react';
2
+ import { type LazyComponent } from '@wolffm/rooms-client/react';
3
+ export type { LazyComponent };
7
4
  /** How soon after a reload another failed chunk is a real failure rather than a deploy. */
8
5
  export declare const RELOAD_GUARD_MS = 60000;
9
6
  export declare function lazyComponent<P extends object>(load: () => Promise<ComponentType<P>>): LazyComponent<P>;
package/dist/lazy.js CHANGED
@@ -7,57 +7,14 @@
7
7
  * time its component draws, or before, from `preload()`: the screens a player opens next are warmed
8
8
  * once the page is idle (`main.tsx`), so a press rarely waits on the network.
9
9
  *
10
- * `lazyComponent` is `React.lazy` with two differences:
11
- *
12
- * - **A module already loaded draws in the same render.** `React.lazy` suspends once on its first
13
- * render even when `preload()` landed long ago, and a route change is a synchronous update: the
14
- * screen would blank for a frame. Here, once the load has settled, the component is drawn
15
- * directly and nothing suspends.
16
- * - **A failed load reloads the page, once.** The server serves only the current build, so after a
17
- * deploy the open page's chunk names are gone and every load of one fails. A reload fetches the
18
- * new `index.html` (served `no-store`) and lands on the same route. A second failure within
19
- * {@link RELOAD_GUARD_MS} is not a deploy: it is thrown, rather than reloading in a loop.
10
+ * The mechanism is `@wolffm/rooms-client/react`'s `lazyComponent`, the platform's one copy (a
11
+ * loaded module draws without suspending; a failed load reloads the page once for a new build,
12
+ * guarded by a window so it never loops). This binds the app's storage key.
20
13
  */
21
- import { createElement, use } from 'react';
22
- const RELOADED_AT = 'cardtable:reloaded-for-chunk';
14
+ import { lazyComponent as platformLazy, LAZY_RELOAD_GUARD_MS } from '@wolffm/rooms-client/react';
15
+ import { appId } from '@wolffm/ui-shell/app-id';
23
16
  /** How soon after a reload another failed chunk is a real failure rather than a deploy. */
24
- export const RELOAD_GUARD_MS = 60_000;
25
- /** Reloads the page for a new build, unless it just did. Whether it reloaded. */
26
- function reloadForNewBuild() {
27
- try {
28
- const last = Number(sessionStorage.getItem(RELOADED_AT));
29
- if (Date.now() - last < RELOAD_GUARD_MS)
30
- return false;
31
- sessionStorage.setItem(RELOADED_AT, String(Date.now()));
32
- }
33
- catch (err) {
34
- // No storage to remember the reload in, so no way to stop a loop: fail instead.
35
- console.warn('[cardtable] a chunk did not load and there is no session storage to guard a reload with', err);
36
- return false;
37
- }
38
- location.reload();
39
- return true;
40
- }
17
+ export const RELOAD_GUARD_MS = LAZY_RELOAD_GUARD_MS;
41
18
  export function lazyComponent(load) {
42
- let loaded = null;
43
- let loading = null;
44
- /**
45
- * The load a draw waits on, kept once it fails: React's retry reads the same promise and throws its
46
- * error, where a fresh one would suspend again, and load again, forever. Only a draw reloads: a
47
- * preload that fails says so and leaves the load to the draw.
48
- */
49
- let drawing = null;
50
- // A failed preload is not kept: the draw loads again.
51
- const start = () => (loading ??= load().then((component) => (loaded = component), (err) => {
52
- loading = null;
53
- throw err;
54
- }));
55
- const draw = () => (drawing ??= start().catch((err) => {
56
- if (reloadForNewBuild())
57
- return new Promise(() => undefined);
58
- throw err;
59
- }));
60
- const Lazy = (props) => createElement(loaded ?? use(draw()), props);
61
- Lazy.preload = () => start().then(() => undefined, (err) => console.warn('[cardtable] a chunk did not preload; it loads again when drawn', err));
62
- return Lazy;
19
+ return platformLazy(load, { storageKey: () => `${appId()}:reloaded-for-chunk` });
63
20
  }
@@ -1,3 +1,4 @@
1
+ import { type RoomSocketOptions } from '@wolffm/rooms-client';
1
2
  /**
2
3
  * `/cardtable/` → `/cardtable/socket.io/`.
3
4
  *
@@ -9,25 +10,8 @@ export declare function socketPath(base: string): string;
9
10
  /** The path for THIS bundle, from the base Vite was built with. */
10
11
  export declare function defaultSocketPath(): string;
11
12
  /**
12
- * A dropped socket tries again after a quarter second, and never waits more than a second between
13
- * tries. socket.io's default (1 s doubling to 5 s) let a socket sit out up to five seconds after a
14
- * deploy's restart had already finished. The edge holds each try's handshake through the restart
15
- * (hadoku_site's `proxy.ts`, `holdThroughRestart`), so a short delay costs a few held requests, not
16
- * a storm of failed ones, and the socket lands the moment the new server listens.
13
+ * How every socket on the page connects: websocket first with long-polling only as a fallback, and a
14
+ * reconnect after 250 ms, never more than 1 s apart. The platform's one setting, shared with
15
+ * watchparty's sockets: `@wolffm/rooms-client`'s `roomSocketOptions`, whose header says why.
17
16
  */
18
- export declare const RECONNECT_DELAY_MS = 250;
19
- export declare const RECONNECT_DELAY_MAX_MS = 1000;
20
- /**
21
- * How every socket on the page connects: a websocket first, and long-polling only if the websocket
22
- * fails to open (`tryAllTransports`), so a network that refuses websockets still connects. socket.io's
23
- * default starts on long-polling and upgrades, which cost each connection three or four extra round
24
- * trips through the edge before its first frame, each a run of the edge worker. Watchparty connects
25
- * websocket-first through the same edge.
26
- */
27
- export declare function socketOptions(path?: string): {
28
- path: string;
29
- transports: ('websocket' | 'polling')[];
30
- tryAllTransports: true;
31
- reconnectionDelay: number;
32
- reconnectionDelayMax: number;
33
- };
17
+ export declare function socketOptions(path?: string): RoomSocketOptions;
@@ -3,6 +3,7 @@
3
3
  * reconnect pacing. Not any one game's: the draft, the match, the room feed and the room's session
4
4
  * claim all connect this way.
5
5
  */
6
+ import { roomSocketOptions } from '@wolffm/rooms-client';
6
7
  /**
7
8
  * `/cardtable/` → `/cardtable/socket.io/`.
8
9
  *
@@ -20,21 +21,10 @@ export function defaultSocketPath() {
20
21
  return socketPath(typeof base === 'string' ? base : '/');
21
22
  }
22
23
  /**
23
- * A dropped socket tries again after a quarter second, and never waits more than a second between
24
- * tries. socket.io's default (1 s doubling to 5 s) let a socket sit out up to five seconds after a
25
- * deploy's restart had already finished. The edge holds each try's handshake through the restart
26
- * (hadoku_site's `proxy.ts`, `holdThroughRestart`), so a short delay costs a few held requests, not
27
- * a storm of failed ones, and the socket lands the moment the new server listens.
28
- */
29
- export const RECONNECT_DELAY_MS = 250;
30
- export const RECONNECT_DELAY_MAX_MS = 1000;
31
- /**
32
- * How every socket on the page connects: a websocket first, and long-polling only if the websocket
33
- * fails to open (`tryAllTransports`), so a network that refuses websockets still connects. socket.io's
34
- * default starts on long-polling and upgrades, which cost each connection three or four extra round
35
- * trips through the edge before its first frame, each a run of the edge worker. Watchparty connects
36
- * websocket-first through the same edge.
24
+ * How every socket on the page connects: websocket first with long-polling only as a fallback, and a
25
+ * reconnect after 250 ms, never more than 1 s apart. The platform's one setting, shared with
26
+ * watchparty's sockets: `@wolffm/rooms-client`'s `roomSocketOptions`, whose header says why.
37
27
  */
38
28
  export function socketOptions(path = defaultSocketPath()) {
39
- return { path, transports: ['websocket', 'polling'], tryAllTransports: true, reconnectionDelay: RECONNECT_DELAY_MS, reconnectionDelayMax: RECONNECT_DELAY_MAX_MS };
29
+ return roomSocketOptions(path);
40
30
  }
@@ -1,6 +1,4 @@
1
- /** The stored id this page may keep, given how it was loaded: only a reload's; exported for its test. */
2
- export declare function storedCandidate(navigation: string, stored: string | null): string | null;
3
1
  /** This tab's id once no other live tab claims it: what a player's claim and joins carry. */
4
- export declare function settledTabId(): Promise<string>;
2
+ export declare const settledTabId: () => Promise<string>;
5
3
  /** Run `use` with this tab's settled id: now when it is known, else once the probe has answered. */
6
- export declare function withSettledTabId(use: (id: string) => void): void;
4
+ export declare const withSettledTabId: (use: (id: string) => void) => void;
@@ -7,84 +7,14 @@
7
7
  * reaching the server (match m-ca7bad04b715, 2026-10-28) is replaced by its own tab's reconnect at
8
8
  * once. A second tab is another holder, and is asked before it takes over.
9
9
  *
10
- * One id per page load, held in memory: every socket this page opens — every reconnect — carries the
11
- * same id. A reload keeps it, read back from sessionStorage. Only a reload: a duplicate, a restored
12
- * tab, a new tab and a link navigate as something other than `reload`, so they mint their own. And
13
- * because a duplicated tab starts with a COPY of the original's sessionStorage, the stored id is
14
- * also probed (`@wolffm/rooms-protocol`'s `settleTabId`): if another live tab answers that it uses
15
- * it, this one mints a fresh one. Every id the page sends is the settled one: an id handed out
16
- * before the probe answered could be the duplicate's, and a join carrying it would hold the room
17
- * for the other tab while this one's claim was asked about itself. `settledTabId` waits for it, and
18
- * `withSettledTabId` hands it over at once when it is known (every page but a reload's first 150 ms).
10
+ * The mechanism (one id per page load, kept across a reload in sessionStorage, a duplicated tab
11
+ * probed and given its own) is `@wolffm/rooms-protocol`'s `createTabIdentity`, the same one
12
+ * watchparty's tabs use. This binds the app's storage key (`<appId>:tab-id`).
19
13
  */
20
- import { answerTabProbes, browserTabChannel, isTabId, mintTabId, settleTabId } from '@wolffm/rooms-protocol';
21
- const STORAGE_KEY = 'cardtable:tab-id';
22
- /** How this page was loaded: `reload`, or anything else (`navigate`, `back_forward`, `prerender`, unknown). */
23
- function loadedBy() {
24
- const entry = typeof performance === 'undefined' ? undefined : performance.getEntriesByType?.('navigation')[0];
25
- return entry !== undefined && 'type' in entry ? String(entry.type) : 'unknown';
26
- }
27
- function storage() {
28
- try {
29
- return typeof sessionStorage === 'undefined' ? null : sessionStorage;
30
- }
31
- catch (err) {
32
- // A sandboxed frame throws on the accessor: the id then lives for this page only.
33
- console.warn('[tab-id] no sessionStorage', err);
34
- return null;
35
- }
36
- }
37
- /** The stored id this page may keep, given how it was loaded: only a reload's; exported for its test. */
38
- export function storedCandidate(navigation, stored) {
39
- return navigation === 'reload' && isTabId(stored) ? stored : null;
40
- }
41
- let provisional = null;
42
- let settled = null;
43
- let settling = null;
44
- function begin() {
45
- if (settling !== null)
46
- return settling;
47
- const store = storage();
48
- let stored = null;
49
- try {
50
- stored = store?.getItem(STORAGE_KEY) ?? null;
51
- }
52
- catch (err) {
53
- console.warn('[tab-id] could not read the stored id', err);
54
- }
55
- const candidate = storedCandidate(loadedBy(), stored);
56
- provisional = candidate ?? mintTabId();
57
- let channel = null;
58
- try {
59
- channel = browserTabChannel();
60
- }
61
- catch (err) {
62
- console.warn('[tab-id] no BroadcastChannel', err);
63
- }
64
- if (channel !== null)
65
- answerTabProbes(channel, () => settled ?? provisional);
66
- // A freshly minted id is nobody else's: only a kept one is probed.
67
- settling = settleTabId({ candidate: provisional, channel: candidate === null ? null : channel }).then((id) => {
68
- settled = id;
69
- try {
70
- store?.setItem(STORAGE_KEY, id);
71
- }
72
- catch (err) {
73
- console.warn('[tab-id] could not store the id', err);
74
- }
75
- return id;
76
- });
77
- return settling;
78
- }
14
+ import { createTabIdentity } from '@wolffm/rooms-protocol';
15
+ import { appId } from '@wolffm/ui-shell/app-id';
16
+ const tab = createTabIdentity({ storageKey: () => `${appId()}:tab-id` });
79
17
  /** This tab's id once no other live tab claims it: what a player's claim and joins carry. */
80
- export function settledTabId() {
81
- return begin();
82
- }
18
+ export const settledTabId = tab.settledTabId;
83
19
  /** Run `use` with this tab's settled id: now when it is known, else once the probe has answered. */
84
- export function withSettledTabId(use) {
85
- const pending = begin();
86
- if (settled !== null)
87
- use(settled);
88
- else
89
- void pending.then(use);
90
- }
20
+ export const withSettledTabId = tab.withSettledTabId;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-ui",
3
- "version": "0.1.64",
4
- "gitHead": "378aa15437dcc5b0f243f8314b71a8925e6ae52d",
3
+ "version": "0.1.65",
4
+ "gitHead": "e08e1963c06c1be454d8a685188301337d0c2c2d",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The browser parts any hosted game shares, above ui-shell: the room shell and session, seats and looks, the room route, the front page's generic pieces (gate, polls, gallery, patch notes), the bundle slot and relay channel a game's client mounts in, settings and trim screens, the HTTP and socket helpers, and the generic components (modal, popover, avatar, action button, radio cards). Knows no game; Magic's UI (games/magic/ui) plugs in, composed by the site (apps/ui). MIT; imports only react, react-dom, socket.io-client, @wolffm/* and the platform packages.",
@@ -49,8 +49,8 @@
49
49
  "react-dom": "^19.2.8"
50
50
  },
51
51
  "dependencies": {
52
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.68",
53
- "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.68",
52
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.69",
53
+ "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.69",
54
54
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.1.30",
55
55
  "@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.121",
56
56
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.121",