@hadoku/rooms-client 0.45.120 → 0.45.122
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/lazy.d.ts +21 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +56 -0
- package/dist/react.d.ts +2 -0
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +2 -0
- package/dist/socketOptions.d.ts +24 -0
- package/dist/socketOptions.d.ts.map +1 -0
- package/dist/socketOptions.js +29 -0
- package/dist/useGameBundle.d.ts +27 -0
- package/dist/useGameBundle.d.ts.map +1 -0
- package/dist/useGameBundle.js +42 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { createRoomListClient, type FeedTarget, type PingReason, type PingSource, type RoomListClient, type RoomListDeps, type RoomListListener, type RoomListState, } from './roomList.js';
|
|
2
2
|
export { createFeedPings, type ClientFeedWire, type FeedSocket, type FeedSocketDeps, type RoomFeedChanged, } from './feedSocket.js';
|
|
3
3
|
export { mountGameBundle, moduleActivity, resolveGameBundleUrl, type BundleActivity, type GameBundleHandle, } from './bundle-host.js';
|
|
4
|
+
export { roomSocketOptions, ROOM_SOCKET_RECONNECT_DELAY_MS, ROOM_SOCKET_RECONNECT_DELAY_MAX_MS, type RoomSocketOptions, } from './socketOptions.js';
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,oBAAoB,EACpB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,aAAa,GACnB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,eAAe,EACf,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,eAAe,GACrB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,gBAAgB,GACtB,MAAM,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,oBAAoB,EACpB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,aAAa,GACnB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,eAAe,EACf,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,eAAe,GACrB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,gBAAgB,GACtB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,iBAAiB,EACjB,8BAA8B,EAC9B,kCAAkC,EAClC,KAAK,iBAAiB,GACvB,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -13,3 +13,4 @@
|
|
|
13
13
|
export { createRoomListClient, } from './roomList.js';
|
|
14
14
|
export { createFeedPings, } from './feedSocket.js';
|
|
15
15
|
export { mountGameBundle, moduleActivity, resolveGameBundleUrl, } from './bundle-host.js';
|
|
16
|
+
export { roomSocketOptions, ROOM_SOCKET_RECONNECT_DELAY_MS, ROOM_SOCKET_RECONNECT_DELAY_MAX_MS, } from './socketOptions.js';
|
package/dist/lazy.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
7
|
+
export interface LazyOptions {
|
|
8
|
+
/** The sessionStorage key the last reload's time is kept under, per app (`cardtable:reloaded-for-chunk`). Read when needed. */
|
|
9
|
+
storageKey: string | (() => string);
|
|
10
|
+
/** Default `location.reload()`; a test passes its own. */
|
|
11
|
+
reload?: () => void;
|
|
12
|
+
/** Default: the page's `sessionStorage`. */
|
|
13
|
+
storage?: () => Pick<Storage, 'getItem' | 'setItem'>;
|
|
14
|
+
now?: () => number;
|
|
15
|
+
}
|
|
16
|
+
/** How soon after a reload another failed chunk is a real failure rather than a deploy. */
|
|
17
|
+
export declare const LAZY_RELOAD_GUARD_MS = 60000;
|
|
18
|
+
/** Reloads the page for a new build, unless it just did. Whether it reloaded. Exported for its test. */
|
|
19
|
+
export declare function reloadForNewBuild(opts: LazyOptions): boolean;
|
|
20
|
+
export declare function lazyComponent<P extends object>(load: () => Promise<ComponentType<P>>, opts: LazyOptions): LazyComponent<P>;
|
|
21
|
+
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAuB,KAAK,aAAa,EAAE,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEhF,mGAAmG;AACnG,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC;IACtB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW;IAC1B,+HAA+H;IAC/H,UAAU,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;IACpC,0DAA0D;IAC1D,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IACpB,4CAA4C;IAC5C,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC,OAAO,EAAE,SAAS,GAAG,SAAS,CAAC,CAAC;IACrD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,2FAA2F;AAC3F,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAE3C,wGAAwG;AACxG,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAe5D;AAED,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,GAAG,aAAa,CAAC,CAAC,CAAC,CA8B1H"}
|
package/dist/lazy.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A component whose code loads when it is first drawn, or earlier on purpose (`preload()`), for any
|
|
3
|
+
* app on the platform. `React.lazy` with two differences:
|
|
4
|
+
*
|
|
5
|
+
* - **A module already loaded draws in the same render.** `React.lazy` suspends once on its first
|
|
6
|
+
* render even when the module landed long ago, and a route change is a synchronous update: the
|
|
7
|
+
* screen would blank for a frame. Here, once the load has settled, the component is drawn
|
|
8
|
+
* directly and nothing suspends.
|
|
9
|
+
* - **A failed load reloads the page, once.** A server serves only its current build, so after a
|
|
10
|
+
* deploy an open page's chunk names are gone and every load of one fails. A reload fetches the
|
|
11
|
+
* new entry and lands on the same route. A second failure within `LAZY_RELOAD_GUARD_MS` is not a
|
|
12
|
+
* deploy: it is thrown, rather than reloading in a loop. The guard is a window, not a flag, so a
|
|
13
|
+
* tab that reloaded for one deploy still recovers from the next; and where there is no session
|
|
14
|
+
* storage to remember the reload in (a sandboxed frame), it throws rather than risk a loop.
|
|
15
|
+
*/
|
|
16
|
+
import { createElement, lazy } from 'react';
|
|
17
|
+
/** How soon after a reload another failed chunk is a real failure rather than a deploy. */
|
|
18
|
+
export const LAZY_RELOAD_GUARD_MS = 60_000;
|
|
19
|
+
/** Reloads the page for a new build, unless it just did. Whether it reloaded. Exported for its test. */
|
|
20
|
+
export function reloadForNewBuild(opts) {
|
|
21
|
+
const now = (opts.now ?? Date.now)();
|
|
22
|
+
try {
|
|
23
|
+
const key = typeof opts.storageKey === 'function' ? opts.storageKey() : opts.storageKey;
|
|
24
|
+
const store = (opts.storage ?? (() => sessionStorage))();
|
|
25
|
+
const last = Number(store.getItem(key));
|
|
26
|
+
if (now - last < LAZY_RELOAD_GUARD_MS)
|
|
27
|
+
return false;
|
|
28
|
+
store.setItem(key, String(now));
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
// No storage to remember the reload in, so no way to stop a loop: fail instead.
|
|
32
|
+
console.warn('[lazy] a chunk did not load and there is no session storage to guard a reload with', err);
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
(opts.reload ?? (() => location.reload()))();
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
export function lazyComponent(load, opts) {
|
|
39
|
+
let loaded = null;
|
|
40
|
+
let loading = null;
|
|
41
|
+
// A failed preload is not kept: the draw loads again.
|
|
42
|
+
const start = () => (loading ??= load().then((component) => (loaded = component), (err) => {
|
|
43
|
+
loading = null;
|
|
44
|
+
throw err;
|
|
45
|
+
}));
|
|
46
|
+
// Only a draw reloads: a preload that fails says so and leaves the load to the draw. React.lazy
|
|
47
|
+
// keeps the one promise, so a failed draw throws its error to the boundary rather than looping.
|
|
48
|
+
const Drawn = lazy(() => start().then((component) => ({ default: component }), (err) => {
|
|
49
|
+
if (reloadForNewBuild(opts))
|
|
50
|
+
return new Promise(() => undefined);
|
|
51
|
+
throw err;
|
|
52
|
+
}));
|
|
53
|
+
const Lazy = (props) => createElement((loaded ?? Drawn), props);
|
|
54
|
+
Lazy.preload = () => start().then(() => undefined, (err) => console.warn('[lazy] a chunk did not preload; it loads again when drawn', err));
|
|
55
|
+
return Lazy;
|
|
56
|
+
}
|
package/dist/react.d.ts
CHANGED
|
@@ -9,4 +9,6 @@ import { type FeedTarget, type RoomListDeps, type RoomListState } from './roomLi
|
|
|
9
9
|
export declare function useRoomList<T>(target: FeedTarget, deps: RoomListDeps<T>): RoomListState<T> & {
|
|
10
10
|
refresh: () => void;
|
|
11
11
|
};
|
|
12
|
+
export { lazyComponent, LAZY_RELOAD_GUARD_MS, type LazyComponent, type LazyOptions } from './lazy.js';
|
|
13
|
+
export { useGameBundle, type UseGameBundleOptions } from './useGameBundle.js';
|
|
12
14
|
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,KAAK,UAAU,EAAuB,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAmBlI;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,GAAG;IAAE,OAAO,EAAE,MAAM,IAAI,CAAA;CAAE,CAsDpH"}
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,KAAK,UAAU,EAAuB,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAmBlI;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,GAAG;IAAE,OAAO,EAAE,MAAM,IAAI,CAAA;CAAE,CAsDpH;AAED,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,KAAK,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAEtG,OAAO,EAAE,aAAa,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/react.js
CHANGED
|
@@ -70,3 +70,5 @@ export function useRoomList(target, deps) {
|
|
|
70
70
|
const state = useSyncExternalStore(client ? client.subscribe : noopSubscribe, () => (client ? client.getState() : EMPTY_STATE));
|
|
71
71
|
return { ...state, refresh: () => client.refresh() };
|
|
72
72
|
}
|
|
73
|
+
export { lazyComponent, LAZY_RELOAD_GUARD_MS } from './lazy.js';
|
|
74
|
+
export { useGameBundle } from './useGameBundle.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A dropped socket tries again after a quarter second, and never waits more than a second between
|
|
3
|
+
* tries. socket.io's default (1 s doubling to 5 s) let a socket sit out up to five seconds after a
|
|
4
|
+
* deploy's restart had already finished. The edge holds each try's handshake through the restart
|
|
5
|
+
* (hadoku_site's `proxy.ts`, `holdThroughRestart`), so a short delay costs a few held requests, not
|
|
6
|
+
* a storm of failed ones, and the socket lands the moment the new server listens.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ROOM_SOCKET_RECONNECT_DELAY_MS = 250;
|
|
9
|
+
export declare const ROOM_SOCKET_RECONNECT_DELAY_MAX_MS = 1000;
|
|
10
|
+
export interface RoomSocketOptions {
|
|
11
|
+
path: string;
|
|
12
|
+
transports: ('websocket' | 'polling')[];
|
|
13
|
+
tryAllTransports: true;
|
|
14
|
+
reconnectionDelay: number;
|
|
15
|
+
reconnectionDelayMax: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A websocket first, and long-polling only if the websocket fails to open (`tryAllTransports`), so
|
|
19
|
+
* a network that refuses websockets still connects. socket.io's default starts on long-polling and
|
|
20
|
+
* upgrades, which costs each connection three or four extra round trips through the edge before
|
|
21
|
+
* its first frame. Spread the result into `io(url, …)`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function roomSocketOptions(path: string): RoomSocketOptions;
|
|
24
|
+
//# sourceMappingURL=socketOptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socketOptions.d.ts","sourceRoot":"","sources":["../src/socketOptions.ts"],"names":[],"mappings":"AAMA;;;;;;GAMG;AACH,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAClD,eAAO,MAAM,kCAAkC,OAAO,CAAC;AAEvD,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,CAAC;IACxC,gBAAgB,EAAE,IAAI,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oBAAoB,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,iBAAiB,CAQjE"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* How a room socket connects, for every app on the platform: its transports and its reconnect
|
|
3
|
+
* pacing. The path is the app's own (where its server's socket.io is mounted), so it is the one
|
|
4
|
+
* argument.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* A dropped socket tries again after a quarter second, and never waits more than a second between
|
|
8
|
+
* tries. socket.io's default (1 s doubling to 5 s) let a socket sit out up to five seconds after a
|
|
9
|
+
* deploy's restart had already finished. The edge holds each try's handshake through the restart
|
|
10
|
+
* (hadoku_site's `proxy.ts`, `holdThroughRestart`), so a short delay costs a few held requests, not
|
|
11
|
+
* a storm of failed ones, and the socket lands the moment the new server listens.
|
|
12
|
+
*/
|
|
13
|
+
export const ROOM_SOCKET_RECONNECT_DELAY_MS = 250;
|
|
14
|
+
export const ROOM_SOCKET_RECONNECT_DELAY_MAX_MS = 1000;
|
|
15
|
+
/**
|
|
16
|
+
* A websocket first, and long-polling only if the websocket fails to open (`tryAllTransports`), so
|
|
17
|
+
* a network that refuses websockets still connects. socket.io's default starts on long-polling and
|
|
18
|
+
* upgrades, which costs each connection three or four extra round trips through the edge before
|
|
19
|
+
* its first frame. Spread the result into `io(url, …)`.
|
|
20
|
+
*/
|
|
21
|
+
export function roomSocketOptions(path) {
|
|
22
|
+
return {
|
|
23
|
+
path,
|
|
24
|
+
transports: ['websocket', 'polling'],
|
|
25
|
+
tryAllTransports: true,
|
|
26
|
+
reconnectionDelay: ROOM_SOCKET_RECONNECT_DELAY_MS,
|
|
27
|
+
reconnectionDelayMax: ROOM_SOCKET_RECONNECT_DELAY_MAX_MS,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
import type { GameBundleContext } from '@wolffm/rooms-protocol';
|
|
3
|
+
import { type GameBundleHandle } from './bundle-host.js';
|
|
4
|
+
export interface UseGameBundleOptions {
|
|
5
|
+
/** The bundle's module. null: nothing to mount (no game, or one this viewer dismissed). */
|
|
6
|
+
load: (() => Promise<unknown>) | null;
|
|
7
|
+
/** The context; null until it can be told truthfully (identity, the room). Nothing mounts before. */
|
|
8
|
+
ctx: GameBundleContext | null;
|
|
9
|
+
/**
|
|
10
|
+
* A value whose change mounts the bundle afresh, as a new game: the game's id, a layer, a match
|
|
11
|
+
* number. Every other context change goes to the mounted bundle's `update()`, never a remount.
|
|
12
|
+
*/
|
|
13
|
+
mountKey?: unknown;
|
|
14
|
+
/** A failed import, a module with no `mount`, a `mount` or `update` that throws. Never thrown. */
|
|
15
|
+
onError: (message: string, cause?: unknown) => void;
|
|
16
|
+
/** The mounted bundle can (or no longer can) draw settings of its own (`GameBundleHandle.mountSettings`). */
|
|
17
|
+
onSettingsAvailable?: (available: boolean, handle: GameBundleHandle) => void;
|
|
18
|
+
onSettingsError?: (message: string, cause?: unknown) => void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A game bundle in a React slot: `mountGameBundle` mounted into the element the returned ref is
|
|
22
|
+
* put on, once there is something to load and a context to give it; `update(ctx)` on every later
|
|
23
|
+
* context change; disposed on unmount, on a new `load` or `mountKey`, and when either goes null.
|
|
24
|
+
* The callbacks are read at call time, so passing fresh closures each render is fine.
|
|
25
|
+
*/
|
|
26
|
+
export declare function useGameBundle<E extends HTMLElement = HTMLDivElement>(opts: UseGameBundleOptions): RefObject<E | null>;
|
|
27
|
+
//# sourceMappingURL=useGameBundle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useGameBundle.d.ts","sourceRoot":"","sources":["../src/useGameBundle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAEhE,OAAO,EAAmB,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE1E,MAAM,WAAW,oBAAoB;IACnC,2FAA2F;IAC3F,IAAI,EAAE,CAAC,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IACtC,qGAAqG;IACrG,GAAG,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,kGAAkG;IAClG,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACpD,6GAA6G;IAC7G,mBAAmB,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAC7E,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CAC9D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,cAAc,EAAE,IAAI,EAAE,oBAAoB,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,CAkCrH"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { mountGameBundle } from './bundle-host.js';
|
|
3
|
+
/**
|
|
4
|
+
* A game bundle in a React slot: `mountGameBundle` mounted into the element the returned ref is
|
|
5
|
+
* put on, once there is something to load and a context to give it; `update(ctx)` on every later
|
|
6
|
+
* context change; disposed on unmount, on a new `load` or `mountKey`, and when either goes null.
|
|
7
|
+
* The callbacks are read at call time, so passing fresh closures each render is fine.
|
|
8
|
+
*/
|
|
9
|
+
export function useGameBundle(opts) {
|
|
10
|
+
const hostRef = useRef(null);
|
|
11
|
+
const handleRef = useRef(null);
|
|
12
|
+
const latest = useRef(opts);
|
|
13
|
+
latest.current = opts;
|
|
14
|
+
const ready = opts.ctx !== null;
|
|
15
|
+
const { load, ctx, mountKey } = opts;
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const el = hostRef.current;
|
|
18
|
+
const mountCtx = latest.current.ctx;
|
|
19
|
+
if (el === null || load === null || mountCtx === null)
|
|
20
|
+
return;
|
|
21
|
+
const handle = mountGameBundle({
|
|
22
|
+
load,
|
|
23
|
+
el,
|
|
24
|
+
ctx: mountCtx,
|
|
25
|
+
onError: (message, cause) => latest.current.onError(message, cause),
|
|
26
|
+
onSettingsAvailable: latest.current.onSettingsAvailable === undefined
|
|
27
|
+
? undefined
|
|
28
|
+
: (available) => latest.current.onSettingsAvailable?.(available, handle),
|
|
29
|
+
onSettingsError: (message, cause) => latest.current.onSettingsError?.(message, cause),
|
|
30
|
+
});
|
|
31
|
+
handleRef.current = handle;
|
|
32
|
+
return () => {
|
|
33
|
+
handle.dispose();
|
|
34
|
+
handleRef.current = null;
|
|
35
|
+
};
|
|
36
|
+
}, [load, ready, mountKey]);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (ctx !== null)
|
|
39
|
+
handleRef.current?.update(ctx);
|
|
40
|
+
}, [ctx]);
|
|
41
|
+
return hostRef;
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/rooms-client",
|
|
3
|
-
"version": "0.45.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.45.122",
|
|
4
|
+
"gitHead": "5d41d7adc0bc5bdb7f310f9cb738038b488fbf92",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Browser-side room feed client: follow a room (or the room list) without polling for it.",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"directory": "packages/rooms-client"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@0.45.
|
|
30
|
+
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@0.45.122"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": ">=18",
|