@hadoku/rooms-client 0.0.0-stage → 0.45.63

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Matthaeus Wolff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ import type { PingSource } from './roomList.js';
2
+ /**
3
+ * Turns a socket.io connection into a `PingSource` — the part of
4
+ * `./roomList.ts` that actually knows about the wire.
5
+ *
6
+ * ## The wire contract is INJECTED, not imported
7
+ *
8
+ * Same posture as `@wolffm/rooms-server`'s `book/feed.ts`: `@wolffm/rooms-protocol`'s
9
+ * `feed.ts` (`ROOM_FEED_CHANNEL`, `parseRoomChanged`) is the canonical contract, but
10
+ * this file takes it as a parameter rather than importing it, so it builds and
11
+ * tests with no dependency on whether a given workspace has re-exported it yet.
12
+ * A caller builds `ClientFeedWire` from the real module.
13
+ */
14
+ export type RoomFeedChanged = {
15
+ room: string;
16
+ } | {
17
+ list: true;
18
+ };
19
+ export interface ClientFeedWire {
20
+ channel: {
21
+ watch: string;
22
+ changed: string;
23
+ };
24
+ /** Parse an inbound `changed` payload off the wire. `null` for anything malformed. */
25
+ parseChanged(raw: unknown): RoomFeedChanged | null;
26
+ }
27
+ /** The part of a socket.io-client `Socket` this file uses. A real `Socket` satisfies this already. */
28
+ export interface FeedSocket {
29
+ on(event: string, handler: (payload?: unknown) => void): void;
30
+ once(event: string, handler: (payload?: unknown) => void): void;
31
+ off(event: string, handler: (payload?: unknown) => void): void;
32
+ emit(event: string, payload: unknown, ack: () => void): void;
33
+ readonly connected: boolean;
34
+ close(): void;
35
+ }
36
+ export interface FeedSocketDeps {
37
+ wire: ClientFeedWire;
38
+ /** Opens a fresh connection. Called once, and again after `close()` is never called by this file — a dropped socket is the caller's transport library's job to reconnect; this just re-sends the watch and treats the reconnect as "heard again". */
39
+ connect(): FeedSocket;
40
+ }
41
+ /**
42
+ * Pings over one socket, shared by every follower. Opened by the first
43
+ * follower and closed once the commit that let go of the last one is over —
44
+ * so a screen that replaces another (list → room) keeps the connection
45
+ * instead of a flash of "disconnected, reconnecting".
46
+ *
47
+ * A follower is "heard" once the server has acknowledged a watch that names
48
+ * it. Until then a change to what it follows could not have been said to it,
49
+ * so the ack doubles as one more read (`PingReason: 'heard'`). A reconnect —
50
+ * `connect`'s own 'connect' event firing again — makes every follower unheard
51
+ * again, for the same reason.
52
+ */
53
+ export declare function createFeedPings(deps: FeedSocketDeps): PingSource;
54
+ //# sourceMappingURL=feedSocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedSocket.d.ts","sourceRoot":"","sources":["../src/feedSocket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA0B,UAAU,EAAE,MAAM,eAAe,CAAC;AAExE;;;;;;;;;;;GAWG;AAEH,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,sFAAsF;IACtF,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAAC;CACpD;AAED,sGAAsG;AACtG,MAAM,WAAW,UAAU;IACzB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IAC9D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IAChE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IAC/D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;IAC7D,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,cAAc,CAAC;IACrB,qPAAqP;IACrP,OAAO,IAAI,UAAU,CAAC;CACvB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,GAAG,UAAU,CAkEhE"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Pings over one socket, shared by every follower. Opened by the first
3
+ * follower and closed once the commit that let go of the last one is over —
4
+ * so a screen that replaces another (list → room) keeps the connection
5
+ * instead of a flash of "disconnected, reconnecting".
6
+ *
7
+ * A follower is "heard" once the server has acknowledged a watch that names
8
+ * it. Until then a change to what it follows could not have been said to it,
9
+ * so the ack doubles as one more read (`PingReason: 'heard'`). A reconnect —
10
+ * `connect`'s own 'connect' event firing again — makes every follower unheard
11
+ * again, for the same reason.
12
+ */
13
+ export function createFeedPings(deps) {
14
+ const followers = new Map();
15
+ let next = 0;
16
+ let socket = null;
17
+ const watchPayload = () => {
18
+ const rooms = new Set();
19
+ let list = false;
20
+ for (const { target } of followers.values()) {
21
+ if (target === 'list')
22
+ list = true;
23
+ else
24
+ rooms.add(target.room);
25
+ }
26
+ return { rooms: [...rooms], list };
27
+ };
28
+ const sendWatch = (to) => {
29
+ const named = [...followers.keys()];
30
+ to.emit(deps.wire.channel.watch, watchPayload(), () => {
31
+ for (const id of named) {
32
+ const f = followers.get(id);
33
+ if (f === undefined || f.heard)
34
+ continue;
35
+ f.heard = true;
36
+ f.onPing('heard');
37
+ }
38
+ });
39
+ };
40
+ const open = () => {
41
+ const opened = deps.connect();
42
+ opened.on('connect', () => {
43
+ for (const f of followers.values())
44
+ f.heard = false;
45
+ sendWatch(opened);
46
+ });
47
+ opened.on(deps.wire.channel.changed, (raw) => {
48
+ const changed = deps.wire.parseChanged(raw);
49
+ if (!changed)
50
+ return;
51
+ for (const f of [...followers.values()]) {
52
+ const matches = 'list' in changed ? f.target === 'list' : f.target !== 'list' && f.target.room === changed.room;
53
+ if (matches)
54
+ f.onPing('changed');
55
+ }
56
+ });
57
+ return opened;
58
+ };
59
+ return {
60
+ subscribe(target, onPing) {
61
+ const id = (next += 1);
62
+ followers.set(id, { target, onPing, heard: false });
63
+ if (socket === null)
64
+ socket = open();
65
+ else if (socket.connected)
66
+ sendWatch(socket);
67
+ return () => {
68
+ followers.delete(id);
69
+ if (socket === null)
70
+ return;
71
+ if (followers.size > 0) {
72
+ if (socket.connected)
73
+ sendWatch(socket);
74
+ return;
75
+ }
76
+ const idle = socket;
77
+ queueMicrotask(() => {
78
+ if (followers.size > 0 || socket !== idle)
79
+ return;
80
+ idle.close();
81
+ socket = null;
82
+ });
83
+ };
84
+ },
85
+ };
86
+ }
@@ -0,0 +1,3 @@
1
+ export { createRoomListClient, type FeedTarget, type PingReason, type PingSource, type RoomListClient, type RoomListDeps, type RoomListListener, type RoomListState, } from './roomList.js';
2
+ export { createFeedPings, type ClientFeedWire, type FeedSocket, type FeedSocketDeps, type RoomFeedChanged, } from './feedSocket.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ /*
2
+ * @wolffm/rooms-client — the browser half of the room feed: subscribe to a
3
+ * room (or the room list), keep a cached read current, survive a reconnect.
4
+ *
5
+ * No framework dependency at this entry. A React hook is the separate
6
+ * `@wolffm/rooms-client/react` entry (`react.ts`), so a consumer with no
7
+ * React on the page never pulls it in.
8
+ *
9
+ * Nothing here imports `@wolffm/rooms-protocol`'s `feed.ts` directly — see
10
+ * `feedSocket.ts`'s header for why the wire contract is a parameter, not an
11
+ * import.
12
+ */
13
+ export { createRoomListClient, } from './roomList.js';
14
+ export { createFeedPings, } from './feedSocket.js';
@@ -0,0 +1,12 @@
1
+ import { type FeedTarget, type RoomListDeps, type RoomListState } from './roomList.js';
2
+ /**
3
+ * Keeps a list current: fetched once, then re-fetched only on a feed ping or
4
+ * the (long) fallback interval — never on a fixed short timer. `target` and
5
+ * the fields of `deps` may change between renders; the underlying client is
6
+ * torn down and rebuilt only when `target` itself changes (room ↔ room, or
7
+ * room ↔ 'list'), via `targetKey`.
8
+ */
9
+ export declare function useRoomList<T>(target: FeedTarget, deps: RoomListDeps<T>): RoomListState<T> & {
10
+ refresh: () => void;
11
+ };
12
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +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,CAyCpH"}
package/dist/react.js ADDED
@@ -0,0 +1,59 @@
1
+ import { useEffect, useRef, useSyncExternalStore } from 'react';
2
+ import { createRoomListClient } from './roomList.js';
3
+ /**
4
+ * A separate entry (`@wolffm/rooms-client/react`), never the package root —
5
+ * same reasoning as `@wolffm/watchparty-ui`'s `room-chrome` split: a consumer
6
+ * with no React in the page (a plain `fetch`-driven list, or a different
7
+ * framework entirely) must not pull React in just by importing the package.
8
+ * React is a peer, and `peerDependenciesMeta` marks it optional.
9
+ */
10
+ function targetKey(target) {
11
+ return target === 'list' ? 'list' : `room:${target.room}`;
12
+ }
13
+ const EMPTY_STATE = { list: null, loading: true, error: null };
14
+ function noopSubscribe() {
15
+ return () => { };
16
+ }
17
+ /**
18
+ * Keeps a list current: fetched once, then re-fetched only on a feed ping or
19
+ * the (long) fallback interval — never on a fixed short timer. `target` and
20
+ * the fields of `deps` may change between renders; the underlying client is
21
+ * torn down and rebuilt only when `target` itself changes (room ↔ room, or
22
+ * room ↔ 'list'), via `targetKey`.
23
+ */
24
+ export function useRoomList(target, deps) {
25
+ const key = targetKey(target);
26
+ // Always the latest `deps`, read lazily inside the client's own calls — so
27
+ // changing `fetchList` (a new closure every render, typically) never tears
28
+ // down the subscription.
29
+ const depsRef = useRef(deps);
30
+ depsRef.current = deps;
31
+ const clientRef = useRef(null);
32
+ if (clientRef.current === null || clientRef.current.key !== key) {
33
+ clientRef.current?.client.close();
34
+ clientRef.current = {
35
+ key,
36
+ client: createRoomListClient(target, {
37
+ pings: depsRef.current.pings,
38
+ fetchList: (signal) => depsRef.current.fetchList(signal),
39
+ fallbackMs: depsRef.current.fallbackMs,
40
+ isVisible: depsRef.current.isVisible,
41
+ onVisible: depsRef.current.onVisible,
42
+ setInterval: depsRef.current.setInterval,
43
+ clearInterval: depsRef.current.clearInterval,
44
+ }),
45
+ };
46
+ }
47
+ const client = clientRef.current.client;
48
+ useEffect(() => {
49
+ // The ref above may already have rebuilt the client between renders (a
50
+ // key change during render); this effect's only job is to close whatever
51
+ // is CURRENT when the component unmounts for good.
52
+ return () => {
53
+ if (clientRef.current?.client === client)
54
+ client.close();
55
+ };
56
+ }, [client]);
57
+ const state = useSyncExternalStore(client ? client.subscribe : noopSubscribe, () => (client ? client.getState() : EMPTY_STATE));
58
+ return { ...state, refresh: () => client.refresh() };
59
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * A list that stays current — read once, then re-read only when told, instead
3
+ * of polling a server on a timer. Modelled on hadoku-cardtable's
4
+ * `apps/ui/src/rooms/feed.ts` (`createRoomFeed`), generalised: this file knows
5
+ * nothing about sockets or any particular wire contract, only about PINGS
6
+ * (`PingSource`, below), so it can be tested — and used — with no network at
7
+ * all. The actual socket is `./feedSocket.ts`.
8
+ *
9
+ * ## What it fetches is the caller's
10
+ *
11
+ * `fetchList` is injected. This package has no opinion on what a "room list"
12
+ * looks like for any given app — that is exactly the thing every caller reads
13
+ * through its own existing route (watchparty's `GET /api/rooms`, say). What
14
+ * changes here is WHEN that read happens, not what it returns.
15
+ *
16
+ * ## A hidden tab reads nothing
17
+ *
18
+ * `isVisible` defaults to the page's own visibility. A hidden tab suppresses
19
+ * both the ping-triggered read and the fallback — there is nobody looking —
20
+ * and a `refresh()` happens once when it becomes visible again, so state is
21
+ * never more than one visibility-change stale.
22
+ */
23
+ export type FeedTarget = {
24
+ room: string;
25
+ } | 'list';
26
+ /** Why a re-read happened: the thing changed, or a ping SOURCE wants a resync (e.g. after a reconnect). */
27
+ export type PingReason = 'changed' | 'heard';
28
+ /** Where "read again" pings come from. Decoupled from any one wire contract — see this file's header. */
29
+ export interface PingSource {
30
+ subscribe(target: FeedTarget, onPing: (why: PingReason) => void): () => void;
31
+ }
32
+ export interface RoomListState<T> {
33
+ list: T | null;
34
+ loading: boolean;
35
+ error: string | null;
36
+ }
37
+ export type RoomListListener<T> = (state: RoomListState<T>) => void;
38
+ export interface RoomListDeps<T> {
39
+ pings: PingSource;
40
+ /** Reads the list. Rejects on a network error; `AbortSignal` is passed through so a `close()` mid-flight cancels cleanly. */
41
+ fetchList: (signal?: AbortSignal) => Promise<T>;
42
+ /** A visible tab re-reads at most this often even with no ping — covers a lost ping. Default 30s. */
43
+ fallbackMs?: number;
44
+ /** Defaults to `!document.hidden`, or `true` where there is no `document`. */
45
+ isVisible?: () => boolean;
46
+ /** Fires when visibility turns from hidden to visible. Defaults to the page's `visibilitychange`. Returns the unsubscribe. */
47
+ onVisible?: (cb: () => void) => () => void;
48
+ setInterval?: (fn: () => void, ms: number) => unknown;
49
+ clearInterval?: (handle: unknown) => void;
50
+ }
51
+ export interface RoomListClient<T> {
52
+ getState(): RoomListState<T>;
53
+ /** Returns the unsubscribe. The listener is called once synchronously, with the CURRENT state. */
54
+ subscribe(listener: RoomListListener<T>): () => void;
55
+ /** Re-read now, regardless of visibility. */
56
+ refresh(): void;
57
+ close(): void;
58
+ }
59
+ export declare function createRoomListClient<T>(target: FeedTarget, deps: RoomListDeps<T>): RoomListClient<T>;
60
+ //# sourceMappingURL=roomList.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roomList.d.ts","sourceRoot":"","sources":["../src/roomList.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC;AAEnD,2GAA2G;AAC3G,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC;AAE7C,yGAAyG;AACzG,MAAM,WAAW,UAAU;IACzB,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CAC9E;AAED,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAEpE,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,KAAK,EAAE,UAAU,CAAC;IAClB,6HAA6H;IAC7H,SAAS,EAAE,CAAC,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAChD,qGAAqG;IACrG,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAC1B,8HAA8H;IAC9H,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;IAC3C,WAAW,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACtD,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;CAC3C;AAED,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B,QAAQ,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC;IAC7B,kGAAkG;IAClG,SAAS,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,CAAC;IACrD,6CAA6C;IAC7C,OAAO,IAAI,IAAI,CAAC;IAChB,KAAK,IAAI,IAAI,CAAC;CACf;AAmBD,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAiEpG"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * A list that stays current — read once, then re-read only when told, instead
3
+ * of polling a server on a timer. Modelled on hadoku-cardtable's
4
+ * `apps/ui/src/rooms/feed.ts` (`createRoomFeed`), generalised: this file knows
5
+ * nothing about sockets or any particular wire contract, only about PINGS
6
+ * (`PingSource`, below), so it can be tested — and used — with no network at
7
+ * all. The actual socket is `./feedSocket.ts`.
8
+ *
9
+ * ## What it fetches is the caller's
10
+ *
11
+ * `fetchList` is injected. This package has no opinion on what a "room list"
12
+ * looks like for any given app — that is exactly the thing every caller reads
13
+ * through its own existing route (watchparty's `GET /api/rooms`, say). What
14
+ * changes here is WHEN that read happens, not what it returns.
15
+ *
16
+ * ## A hidden tab reads nothing
17
+ *
18
+ * `isVisible` defaults to the page's own visibility. A hidden tab suppresses
19
+ * both the ping-triggered read and the fallback — there is nobody looking —
20
+ * and a `refresh()` happens once when it becomes visible again, so state is
21
+ * never more than one visibility-change stale.
22
+ */
23
+ function defaultIsVisible() {
24
+ try {
25
+ return typeof document === 'undefined' ? true : !document.hidden;
26
+ }
27
+ catch {
28
+ return true;
29
+ }
30
+ }
31
+ function defaultOnVisible(cb) {
32
+ if (typeof document === 'undefined' || typeof document.addEventListener !== 'function')
33
+ return () => { };
34
+ const handler = () => { if (!document.hidden)
35
+ cb(); };
36
+ document.addEventListener('visibilitychange', handler);
37
+ return () => document.removeEventListener('visibilitychange', handler);
38
+ }
39
+ const FALLBACK_MS = 30_000;
40
+ export function createRoomListClient(target, deps) {
41
+ const isVisible = deps.isVisible ?? defaultIsVisible;
42
+ const setTimer = deps.setInterval ?? ((fn, ms) => setInterval(fn, ms));
43
+ const clearTimer = deps.clearInterval ?? ((h) => clearInterval(h));
44
+ let state = { list: null, loading: true, error: null };
45
+ const listeners = new Set();
46
+ let controller = null;
47
+ let closed = false;
48
+ const setState = (next) => {
49
+ state = next;
50
+ for (const listener of [...listeners])
51
+ listener(state);
52
+ };
53
+ const load = () => {
54
+ if (closed)
55
+ return;
56
+ controller?.abort();
57
+ const mine = new AbortController();
58
+ controller = mine;
59
+ deps.fetchList(mine.signal).then((list) => {
60
+ if (closed || mine.signal.aborted)
61
+ return;
62
+ setState({ list, loading: false, error: null });
63
+ }, (err) => {
64
+ if (closed || mine.signal.aborted)
65
+ return;
66
+ const message = err instanceof Error ? err.message : 'failed to load';
67
+ setState({ list: state.list, loading: false, error: message });
68
+ });
69
+ };
70
+ load();
71
+ const stopPings = deps.pings.subscribe(target, () => {
72
+ if (isVisible())
73
+ load();
74
+ });
75
+ const stopVisible = (deps.onVisible ?? defaultOnVisible)(() => load());
76
+ const fallback = setTimer(() => {
77
+ if (isVisible())
78
+ load();
79
+ }, deps.fallbackMs ?? FALLBACK_MS);
80
+ return {
81
+ getState() {
82
+ return state;
83
+ },
84
+ subscribe(listener) {
85
+ listeners.add(listener);
86
+ listener(state);
87
+ return () => listeners.delete(listener);
88
+ },
89
+ refresh: load,
90
+ close() {
91
+ if (closed)
92
+ return;
93
+ closed = true;
94
+ controller?.abort();
95
+ stopPings();
96
+ stopVisible();
97
+ clearTimer(fallback);
98
+ listeners.clear();
99
+ },
100
+ };
101
+ }
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@hadoku/rooms-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.45.63",
4
+ "gitHead": "0ccf8bf6bb8756b4a29b88bb50f71701e11b1233",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "description": "Browser-side room feed client: follow a room (or the room list) without polling for it.",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ },
15
+ "./react": {
16
+ "types": "./dist/react.d.ts",
17
+ "import": "./dist/react.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "!dist/.tsbuildinfo"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/WolffM/hadoku-watchparty.git",
27
+ "directory": "packages/rooms-client"
28
+ },
29
+ "dependencies": {
30
+ "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@0.45.63"
31
+ },
32
+ "peerDependencies": {
33
+ "react": ">=18",
34
+ "socket.io-client": "^4.8.3"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "react": {
38
+ "optional": true
39
+ }
40
+ }
41
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.