@hadoku/game-ui 0.1.201 → 0.1.203

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/load.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { type DependencyList } from 'react';
2
+ /** What a failure says: an `Error`'s message, or the thrown value as text. */
3
+ export declare function messageOf(err: unknown): string;
4
+ export interface Loaded<T> {
5
+ value: T | null;
6
+ error: string | null;
7
+ }
8
+ /**
9
+ * Runs `load` on mount and again whenever `deps` change, and returns what it answered. `null`
10
+ * starts nothing and reads as nothing.
11
+ *
12
+ * A change of `deps` clears the value and the error in the render that sees it, so no frame shows
13
+ * the last read's answer for the new deps. A read that answers after unmount, or after a newer read
14
+ * started, is dropped. A rejection is `error`; a read that warns or falls back instead does that in
15
+ * its own `load` and resolves.
16
+ */
17
+ export declare function useLoad<T>(load: (() => Promise<T>) | null, deps: DependencyList): Loaded<T>;
package/dist/load.js ADDED
@@ -0,0 +1,39 @@
1
+ /*
2
+ * One read a component shows: the value it answered, or the error it failed with.
3
+ *
4
+ * The state belongs to the read and to nothing else. A component whose value is also written by a
5
+ * press, a feed or another read keeps its own state instead.
6
+ */
7
+ import { useEffect, useState } from 'react';
8
+ /** What a failure says: an `Error`'s message, or the thrown value as text. */
9
+ export function messageOf(err) {
10
+ return err instanceof Error ? err.message : String(err);
11
+ }
12
+ const NOTHING = { value: null, error: null };
13
+ /** Whether two dependency lists name the same things, as React compares them. */
14
+ const sameDeps = (a, b) => a.length === b.length && a.every((dep, i) => Object.is(dep, b[i]));
15
+ /**
16
+ * Runs `load` on mount and again whenever `deps` change, and returns what it answered. `null`
17
+ * starts nothing and reads as nothing.
18
+ *
19
+ * A change of `deps` clears the value and the error in the render that sees it, so no frame shows
20
+ * the last read's answer for the new deps. A read that answers after unmount, or after a newer read
21
+ * started, is dropped. A rejection is `error`; a read that warns or falls back instead does that in
22
+ * its own `load` and resolves.
23
+ */
24
+ export function useLoad(load, deps) {
25
+ const [landed, setLanded] = useState(null);
26
+ useEffect(() => {
27
+ // A read for older deps is gone for good: coming back to them reads again.
28
+ setLanded(null);
29
+ if (load === null)
30
+ return;
31
+ let live = true;
32
+ load().then((value) => live && setLanded({ deps, loaded: { value, error: null } }), (err) => live && setLanded({ deps, loaded: { value: null, error: messageOf(err) } }));
33
+ return () => {
34
+ live = false;
35
+ };
36
+ // The caller's deps are the effect's: `load` is a fresh closure on every render.
37
+ }, deps);
38
+ return load === null || landed === null || !sameDeps(landed.deps, deps) ? NOTHING : landed.loaded;
39
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-ui",
3
- "version": "0.1.201",
4
- "gitHead": "28319eceb8ef1ed1a54d2fe4b3b21cc9cf8b7be0",
3
+ "version": "0.1.203",
4
+ "gitHead": "fc8197973688846175b63ab11d03d77ce76b1510",
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.",
@@ -53,14 +53,14 @@
53
53
  "react-dom": "^19.2.8"
54
54
  },
55
55
  "dependencies": {
56
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.205",
57
- "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.205",
56
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.207",
57
+ "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.207",
58
58
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.3",
59
- "@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.150",
60
- "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.150",
59
+ "@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.151",
60
+ "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.151",
61
61
  "@wolffm/task-ui-components": "npm:@hadoku/task-ui-components@^4.23.51",
62
62
  "@wolffm/themes": "npm:@hadoku/themes@^5.14.21",
63
- "@wolffm/watchparty-ui": "npm:@hadoku/watchparty-ui@^0.45.150",
63
+ "@wolffm/watchparty-ui": "npm:@hadoku/watchparty-ui@^0.45.151",
64
64
  "socket.io-client": "^4.8.3",
65
65
  "zod": "4.5.4"
66
66
  },