react-websocket 3.2.0 → 3.3.0

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.cjs CHANGED
@@ -1,173 +1,239 @@
1
1
  'use client';
2
- "use strict";
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
-
22
- // src/index.ts
23
- var src_exports = {};
24
- __export(src_exports, {
25
- useWebSocket: () => useWebSocket,
26
- useWebSocketEvent: () => useWebSocketEvent,
27
- useWebSocketLatency: () => useWebSocketLatency,
28
- useWebSocketState: () => useWebSocketState,
29
- useWebSocketStatus: () => useWebSocketStatus,
30
- useWebSocketTopic: () => useWebSocketTopic
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ let __core_cjs = require("./core.cjs");
4
+ let react = require("react");
5
+ //#region src/react.ts
6
+ const IDLE = Object.freeze({
7
+ status: "idle",
8
+ lastMessage: void 0,
9
+ attempt: 0
31
10
  });
32
- module.exports = __toCommonJS(src_exports);
33
- __reExport(src_exports, require("./core.cjs"), module.exports);
34
-
35
- // src/react.ts
36
- var import_react = require("react");
37
- var import_core = require("./core.cjs");
38
- var IDLE = Object.freeze({ status: "idle", lastMessage: void 0, attempt: 0 });
39
- var idleSnapshot = () => IDLE;
40
- var idleStatus = () => "idle";
41
- var NO_LATENCY = Object.freeze({ rtt: void 0, offset: void 0 });
42
- var noLatency = () => NO_LATENCY;
43
- var noopSubscribe = () => () => {
44
- };
45
- var noopSend = () => false;
46
- var noop = () => {
47
- };
48
- var ON_TEARDOWN = /* @__PURE__ */ Symbol.for("react-websocket.onTeardown");
49
- var registry = /* @__PURE__ */ new Map();
11
+ const idleSnapshot = () => IDLE;
12
+ const idleStatus = () => "idle";
13
+ const NO_LATENCY = Object.freeze({
14
+ rtt: void 0,
15
+ offset: void 0
16
+ });
17
+ const noLatency = () => NO_LATENCY;
18
+ const NO_REQUESTS = [];
19
+ const noRequests = () => NO_REQUESTS;
20
+ const noopSubscribe = () => () => {};
21
+ const noopSend = () => false;
22
+ const noop = () => {};
23
+ const ON_TEARDOWN = Symbol.for("react-websocket.onTeardown");
24
+ const registry = /* @__PURE__ */ new Map();
50
25
  function sharedClient(url, options) {
51
- if (typeof window === "undefined") return (0, import_core.createWebSocket)(url, options);
52
- const key = `${url}\0${JSON.stringify(options.protocols ?? null)}`;
53
- let client = registry.get(key);
54
- if (!client) {
55
- const created = (0, import_core.createWebSocket)(url, {
56
- ...options,
57
- [ON_TEARDOWN]: () => {
58
- if (registry.get(key) === created) registry.delete(key);
59
- }
60
- });
61
- registry.set(key, created);
62
- client = created;
63
- }
64
- return client;
26
+ if (typeof window === "undefined") return (0, __core_cjs.createWebSocket)(url, options);
27
+ const key = `${url}\u0000${JSON.stringify(options.protocols ?? null)}`;
28
+ let client = registry.get(key);
29
+ if (!client) {
30
+ const created = (0, __core_cjs.createWebSocket)(url, {
31
+ ...options,
32
+ [ON_TEARDOWN]: () => {
33
+ if (registry.get(key) === created) registry.delete(key);
34
+ }
35
+ });
36
+ registry.set(key, created);
37
+ client = created;
38
+ }
39
+ return client;
65
40
  }
41
+ /** Always-current value for use inside effects and event listeners (a `useEffectEvent` stand-in). */
66
42
  function useLatest(value) {
67
- const ref = (0, import_react.useRef)(value);
68
- (0, import_react.useInsertionEffect)(() => {
69
- ref.current = value;
70
- });
71
- return ref;
43
+ const ref = (0, react.useRef)(value);
44
+ (0, react.useInsertionEffect)(() => {
45
+ ref.current = value;
46
+ });
47
+ return ref;
72
48
  }
73
49
  function useHold(client, enabled) {
74
- (0, import_react.useEffect)(() => {
75
- if (client && enabled) return client.acquire();
76
- }, [client, enabled]);
50
+ (0, react.useEffect)(() => {
51
+ if (client && enabled) return client.acquire();
52
+ }, [client, enabled]);
77
53
  }
78
54
  function useWebSocket(target, options = {}) {
79
- const client = target == null ? null : typeof target === "string" ? sharedClient(target, options) : target;
80
- const enabled = options.enabled ?? true;
81
- const snapshot = (0, import_react.useSyncExternalStore)(
82
- client ? client.subscribe : noopSubscribe,
83
- client ? client.getSnapshot : idleSnapshot,
84
- idleSnapshot
85
- );
86
- const handlers = useLatest(options);
87
- (0, import_react.useEffect)(() => {
88
- if (!client || !enabled) return;
89
- const offs = [
90
- client.on("open", (event) => handlers.current.onOpen?.(event)),
91
- client.on("message", (message, event) => handlers.current.onMessage?.(message, event)),
92
- client.on("close", (info) => handlers.current.onClose?.(info)),
93
- client.on("error", (error) => handlers.current.onError?.(error))
94
- ];
95
- return () => offs.forEach((off) => off());
96
- }, [client, enabled, handlers]);
97
- useHold(client, enabled);
98
- return {
99
- ...snapshot,
100
- send: client ? client.send : noopSend,
101
- close: client ? client.close : noop,
102
- reconnect: client ? client.reconnect : noop,
103
- client
104
- };
55
+ const client = target == null ? null : typeof target === "string" ? sharedClient(target, options) : target;
56
+ const enabled = options.enabled ?? true;
57
+ const snapshot = (0, react.useSyncExternalStore)(client ? client.subscribe : noopSubscribe, client ? client.getSnapshot : idleSnapshot, idleSnapshot);
58
+ const handlers = useLatest(options);
59
+ (0, react.useEffect)(() => {
60
+ if (!client || !enabled) return;
61
+ const offs = [
62
+ client.on("open", (event) => handlers.current.onOpen?.(event)),
63
+ client.on("message", (message, event) => handlers.current.onMessage?.(message, event)),
64
+ client.on("close", (info) => handlers.current.onClose?.(info)),
65
+ client.on("error", (error) => handlers.current.onError?.(error))
66
+ ];
67
+ return () => offs.forEach((off) => off());
68
+ }, [
69
+ client,
70
+ enabled,
71
+ handlers
72
+ ]);
73
+ useHold(client, enabled);
74
+ return {
75
+ ...snapshot,
76
+ send: client ? client.send : noopSend,
77
+ close: client ? client.close : noop,
78
+ reconnect: client ? client.reconnect : noop,
79
+ client
80
+ };
105
81
  }
82
+ /**
83
+ * Listen to a client's events without re-rendering the component. The listener always
84
+ * sees the latest props and state. Keeps the connection open while mounted.
85
+ *
86
+ * @example
87
+ * ```tsx
88
+ * useWebSocketEvent(chat, 'message', message => setMessages(list => [...list, message]));
89
+ * ```
90
+ */
106
91
  function useWebSocketEvent(client, type, listener) {
107
- const latest = useLatest(listener);
108
- (0, import_react.useEffect)(() => {
109
- if (!client) return;
110
- const off = client.on(type, ((...args) => latest.current(...args)));
111
- const release = client.acquire();
112
- return () => {
113
- off();
114
- release();
115
- };
116
- }, [client, type, latest]);
92
+ const latest = useLatest(listener);
93
+ (0, react.useEffect)(() => {
94
+ if (!client) return;
95
+ const off = client.on(type, ((...args) => latest.current(...args)));
96
+ const release = client.acquire();
97
+ return () => {
98
+ off();
99
+ release();
100
+ };
101
+ }, [
102
+ client,
103
+ type,
104
+ latest
105
+ ]);
117
106
  }
107
+ /**
108
+ * The connection status of a client. Re-renders only when the status changes, not on
109
+ * messages. Keeps the connection open while mounted.
110
+ *
111
+ * @example
112
+ * ```tsx
113
+ * const status = useWebSocketStatus(chat); // 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed'
114
+ * ```
115
+ */
118
116
  function useWebSocketStatus(client) {
119
- const status = (0, import_react.useSyncExternalStore)(
120
- client ? client.subscribe : noopSubscribe,
121
- client ? () => client.getSnapshot().status : idleStatus,
122
- idleStatus
123
- );
124
- useHold(client ?? null, true);
125
- return status;
117
+ const status = (0, react.useSyncExternalStore)(client ? client.subscribe : noopSubscribe, client ? () => client.getSnapshot().status : idleStatus, idleStatus);
118
+ useHold(client ?? null, true);
119
+ return status;
126
120
  }
127
- var identity = (value) => value;
121
+ const identity = (value) => value;
128
122
  function useWebSocketState(store, selector = identity, isEqual = Object.is) {
129
- const [getSelection, getServerSelection] = (0, import_react.useMemo)(() => {
130
- const memoized = (read) => {
131
- let memo;
132
- return () => {
133
- const state = read();
134
- if (memo && Object.is(memo.state, state)) return memo.selection;
135
- const selection2 = selector(state);
136
- if (memo && isEqual(memo.selection, selection2)) {
137
- memo = { state, selection: memo.selection };
138
- return memo.selection;
139
- }
140
- memo = { state, selection: selection2 };
141
- return selection2;
142
- };
143
- };
144
- return [memoized(store.getState), memoized(() => store.initialState)];
145
- }, [store, selector, isEqual]);
146
- const selection = (0, import_react.useSyncExternalStore)(store.subscribe, getSelection, getServerSelection);
147
- useHold(store.client, true);
148
- return selection;
123
+ const [getSelection, getServerSelection] = (0, react.useMemo)(() => {
124
+ const memoized = (read) => {
125
+ let memo;
126
+ return () => {
127
+ const state = read();
128
+ if (memo && Object.is(memo.state, state)) return memo.selection;
129
+ const selection = selector(state);
130
+ if (memo && isEqual(memo.selection, selection)) {
131
+ memo = {
132
+ state,
133
+ selection: memo.selection
134
+ };
135
+ return memo.selection;
136
+ }
137
+ memo = {
138
+ state,
139
+ selection
140
+ };
141
+ return selection;
142
+ };
143
+ };
144
+ return [memoized(store.getState), memoized(() => store.initialState)];
145
+ }, [
146
+ store,
147
+ selector,
148
+ isEqual
149
+ ]);
150
+ const selection = (0, react.useSyncExternalStore)(store.subscribe, getSelection, getServerSelection);
151
+ useHold(store.client, true);
152
+ return selection;
149
153
  }
154
+ /**
155
+ * Subscribe to one topic (or several) while mounted, through a `createWebSocketTopics`
156
+ * manager. Components holding the same topic share one subscription, and it is sent again
157
+ * after every reconnect. Keeps the connection open while mounted. `null` holds nothing.
158
+ *
159
+ * @example
160
+ * ```tsx
161
+ * useWebSocketTopic(topics, `prices:${symbol}`);
162
+ * useWebSocketTopic(topics, watchlist.map(symbol => `prices:${symbol}`));
163
+ * ```
164
+ */
150
165
  function useWebSocketTopic(topics, topic) {
151
- const key = topic == null ? null : JSON.stringify(typeof topic === "string" ? [topic] : topic);
152
- (0, import_react.useEffect)(() => {
153
- if (!topics || key === null) return;
154
- const releases = JSON.parse(key).map((t) => topics.acquire(t));
155
- return () => releases.forEach((release) => release());
156
- }, [topics, key]);
166
+ const key = topic == null ? null : JSON.stringify(typeof topic === "string" ? [topic] : topic);
167
+ (0, react.useEffect)(() => {
168
+ if (!topics || key === null) return;
169
+ const releases = JSON.parse(key).map((t) => topics.acquire(t));
170
+ return () => releases.forEach((release) => release());
171
+ }, [topics, key]);
157
172
  }
173
+ /**
174
+ * Read round-trip time and server clock offset from a `createWebSocketLatency` instance.
175
+ * Re-renders when a measurement changes, about once per ping interval. Keeps the connection
176
+ * open while mounted. On the server, and during hydration, both values are `undefined`.
177
+ *
178
+ * @example
179
+ * ```tsx
180
+ * const { rtt } = useWebSocketLatency(latency);
181
+ * return <span>{rtt === undefined ? '–' : `${Math.round(rtt)} ms`}</span>;
182
+ * ```
183
+ */
158
184
  function useWebSocketLatency(latency) {
159
- const snapshot = (0, import_react.useSyncExternalStore)(latency.subscribe, latency.getSnapshot, noLatency);
160
- useHold(latency.client, true);
161
- return snapshot;
185
+ const snapshot = (0, react.useSyncExternalStore)(latency.subscribe, latency.getSnapshot, noLatency);
186
+ useHold(latency.client, true);
187
+ return snapshot;
162
188
  }
163
- // Annotate the CommonJS export names for ESM import in node:
164
- 0 && (module.exports = {
165
- useWebSocket,
166
- useWebSocketEvent,
167
- useWebSocketLatency,
168
- useWebSocketState,
169
- useWebSocketStatus,
170
- useWebSocketTopic,
171
- ...require("./core.cjs")
189
+ /**
190
+ * Requests for `method` that a person answers, e.g. an agent asking to run a tool. While mounted,
191
+ * it registers `method` on the responder without a handler, so new requests wait here instead of
192
+ * getting a `NotFoundError` reply. Call `respond` or `reject` on one to answer it. Re-renders
193
+ * when the list changes. Keeps the connection open while mounted. `null` holds nothing.
194
+ *
195
+ * Requests stay listed after unmount until they are answered, time out, are cancelled or lose
196
+ * their connection, so a remounted dialog shows them again. On the server the list is empty.
197
+ *
198
+ * @example
199
+ * ```tsx
200
+ * const [request] = useAgentApproval(tools, 'approve');
201
+ * if (!request) return null;
202
+ * return (
203
+ * <dialog open>
204
+ * {request.params.summary}
205
+ * <button onClick={() => request.respond({ approved: true })}>Approve</button>
206
+ * <button onClick={() => request.respond({ approved: false })}>Deny</button>
207
+ * </dialog>
208
+ * );
209
+ * ```
210
+ */
211
+ function useAgentApproval(responder, method) {
212
+ const waiting = (0, react.useSyncExternalStore)(responder ? responder.subscribe : noopSubscribe, responder ? responder.getSnapshot : noRequests, noRequests);
213
+ (0, react.useEffect)(() => {
214
+ if (responder && method != null) return responder.onRequest(method);
215
+ }, [responder, method]);
216
+ useHold(responder?.client ?? null, method != null);
217
+ return (0, react.useMemo)(() => {
218
+ const mine = waiting.filter((request) => request.method === method);
219
+ return mine.length ? mine : NO_REQUESTS;
220
+ }, [waiting, method]);
221
+ }
222
+ //#endregion
223
+ exports.useAgentApproval = useAgentApproval;
224
+ exports.useWebSocket = useWebSocket;
225
+ exports.useWebSocketEvent = useWebSocketEvent;
226
+ exports.useWebSocketLatency = useWebSocketLatency;
227
+ exports.useWebSocketState = useWebSocketState;
228
+ exports.useWebSocketStatus = useWebSocketStatus;
229
+ exports.useWebSocketTopic = useWebSocketTopic;
230
+ Object.keys(__core_cjs).forEach(function(k) {
231
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
232
+ enumerable: true,
233
+ get: function() {
234
+ return __core_cjs[k];
235
+ }
236
+ });
172
237
  });
238
+
173
239
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/react.ts"],"sourcesContent":["export * from './core';\nexport {\n useWebSocket,\n useWebSocketEvent,\n useWebSocketLatency,\n useWebSocketState,\n useWebSocketStatus,\n useWebSocketTopic\n} from './react';\nexport type { UseWebSocketOptions, UseWebSocketResult, WebSocketHandlers } from './react';\n","import { useEffect, useInsertionEffect, useMemo, useRef, useSyncExternalStore } from 'react';\n// Import the client through the `core` entry: the build keeps it external, so both entries\n// share one copy of it (and of `MessageError`).\nimport { createWebSocket } from './core';\nimport type { Latency, WebSocketLatency } from './latency';\nimport type { WebSocketState } from './state';\nimport type { WebSocketTopics } from './topics';\nimport type {\n CloseInfo,\n EventMap,\n SendOptions,\n Snapshot,\n Status,\n WebSocketClient,\n WebSocketOptions\n} from './types';\n\n/**\n * Per-component event handlers. They always see the component's latest props and\n * state, and changing them never reconnects.\n */\nexport interface WebSocketHandlers<In> {\n /** The socket opened (initially or after a reconnect). */\n onOpen?: (event: Event) => void;\n /** A message arrived and passed `parse`/`json` and `schema`. */\n onMessage?: (message: In, event: MessageEvent) => void;\n /** A socket closed. Called whether or not it will be retried. */\n onClose?: (close: CloseInfo) => void;\n /** A socket error `Event`, a `MessageError`, or an `Error` from the constructor or `url` function. */\n onError?: (error: Event | Error) => void;\n /**\n * `false` releases this component's hold on the connection and stops its handlers.\n * The connection closes once nothing else holds it.\n * @default true\n */\n enabled?: boolean;\n}\n\n/** Options for `useWebSocket(url, options)`: connection options plus handlers. */\nexport interface UseWebSocketOptions<In, Out>\n extends WebSocketOptions<In, Out>, WebSocketHandlers<In> {}\n\n/** What `useWebSocket` returns: the current {@link Snapshot} plus stable actions. */\nexport interface UseWebSocketResult<In, Out> extends Snapshot<In> {\n /**\n * Send now, or queue until open (see {@link SendOptions} for volatile and keyed sends).\n * Returns `true` if sent immediately. Stable identity.\n */\n send: (message: Out, options?: SendOptions) => boolean;\n /** Close and stop reconnecting (for every component sharing the connection). Stable identity. */\n close: (code?: number, reason?: string) => void;\n /** Drop the socket and connect again right away. Stable identity. */\n reconnect: () => void;\n /** The underlying client, or `null` when the URL is `null`/`undefined`. */\n client: WebSocketClient<In, Out> | null;\n}\n\nconst IDLE: Snapshot<never> = Object.freeze({ status: 'idle', lastMessage: undefined, attempt: 0 });\nconst idleSnapshot = () => IDLE;\nconst idleStatus = (): Status => 'idle';\nconst NO_LATENCY: Latency = Object.freeze({ rtt: undefined, offset: undefined });\nconst noLatency = () => NO_LATENCY;\nconst noopSubscribe = () => () => {};\nconst noopSend = () => false;\nconst noop = () => {};\n\n// Same key as `ON_TEARDOWN` in ./client, which this entry must not import directly.\nconst ON_TEARDOWN = Symbol.for('react-websocket.onTeardown');\n\n// Hooks given the same URL (and protocols) share one connection.\nconst registry = new Map<string, WebSocketClient<any, any>>();\n\nfunction sharedClient<In, Out>(url: string, options: WebSocketOptions<In, Out>) {\n // Don't let server renders accumulate clients that can never be torn down.\n if (typeof window === 'undefined') return createWebSocket(url, options);\n const key = `${url}\\u0000${JSON.stringify(options.protocols ?? null)}`;\n let client = registry.get(key) as WebSocketClient<In, Out> | undefined;\n if (!client) {\n const created: WebSocketClient<In, Out> = createWebSocket(url, {\n ...options,\n [ON_TEARDOWN]: () => {\n if (registry.get(key) === created) registry.delete(key);\n }\n } as WebSocketOptions<In, Out>);\n registry.set(key, created);\n client = created;\n }\n return client;\n}\n\n/** Always-current value for use inside effects and event listeners (a `useEffectEvent` stand-in). */\nfunction useLatest<T>(value: T) {\n const ref = useRef(value);\n useInsertionEffect(() => {\n ref.current = value;\n });\n return ref;\n}\n\nfunction useHold(client: WebSocketClient<any, any> | null, enabled: boolean) {\n useEffect(() => {\n if (client && enabled) return client.acquire();\n }, [client, enabled]);\n}\n\n/**\n * Connect to a WebSocket and re-render with its status and latest message.\n *\n * - With a URL, every component using the same URL (and `protocols`) shares one\n * connection. Its options come from whichever component created it first.\n * - With a client from `createWebSocket()`, options are the client's; only handlers\n * and `enabled` apply here.\n * - With `null` or `undefined`, nothing connects and the status stays `idle`.\n *\n * The connection opens on mount and closes one tick after the last component using it\n * unmounts, so StrictMode and `<Activity>` don't open duplicate sockets.\n *\n * Re-renders on status changes and on every message. For high-frequency streams use\n * `useWebSocketEvent` and `useWebSocketStatus` instead.\n *\n * @example\n * ```tsx\n * const { status, lastMessage, send } = useWebSocket<Price, Order>('wss://api.example.com/prices', {\n * json: true,\n * onMessage: price => console.log(price)\n * });\n * ```\n */\nexport function useWebSocket<In = unknown, Out = unknown>(\n url: string | null | undefined,\n options?: UseWebSocketOptions<In, Out>\n): UseWebSocketResult<In, Out>;\nexport function useWebSocket<In, Out>(\n client: WebSocketClient<In, Out> | null | undefined,\n options?: WebSocketHandlers<In>\n): UseWebSocketResult<In, Out>;\nexport function useWebSocket<In, Out>(\n target: string | WebSocketClient<In, Out> | null | undefined,\n options: UseWebSocketOptions<In, Out> = {}\n): UseWebSocketResult<In, Out> {\n const client =\n target == null ? null : typeof target === 'string' ? sharedClient(target, options) : target;\n const enabled = options.enabled ?? true;\n\n const snapshot = useSyncExternalStore<Snapshot<In>>(\n client ? client.subscribe : noopSubscribe,\n client ? client.getSnapshot : idleSnapshot,\n idleSnapshot\n );\n\n const handlers = useLatest(options);\n useEffect(() => {\n if (!client || !enabled) return;\n const offs = [\n client.on('open', event => handlers.current.onOpen?.(event)),\n client.on('message', (message, event) => handlers.current.onMessage?.(message, event)),\n client.on('close', info => handlers.current.onClose?.(info)),\n client.on('error', error => handlers.current.onError?.(error))\n ];\n return () => offs.forEach(off => off());\n }, [client, enabled, handlers]);\n useHold(client, enabled);\n\n return {\n ...snapshot,\n send: client ? client.send : noopSend,\n close: client ? client.close : noop,\n reconnect: client ? client.reconnect : noop,\n client\n };\n}\n\n/**\n * Listen to a client's events without re-rendering the component. The listener always\n * sees the latest props and state. Keeps the connection open while mounted.\n *\n * @example\n * ```tsx\n * useWebSocketEvent(chat, 'message', message => setMessages(list => [...list, message]));\n * ```\n */\nexport function useWebSocketEvent<In, K extends keyof EventMap<In>>(\n client: WebSocketClient<In, any> | null | undefined,\n type: K,\n listener: EventMap<In>[K]\n): void {\n const latest = useLatest(listener);\n useEffect(() => {\n if (!client) return;\n const off = client.on(type, ((...args: unknown[]) =>\n (latest.current as (...a: unknown[]) => void)(...args)) as EventMap<In>[K]);\n const release = client.acquire();\n return () => {\n off();\n release();\n };\n }, [client, type, latest]);\n}\n\n/**\n * The connection status of a client. Re-renders only when the status changes, not on\n * messages. Keeps the connection open while mounted.\n *\n * @example\n * ```tsx\n * const status = useWebSocketStatus(chat); // 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed'\n * ```\n */\nexport function useWebSocketStatus(client: WebSocketClient<any, any> | null | undefined): Status {\n const status = useSyncExternalStore(\n client ? client.subscribe : noopSubscribe,\n client ? () => client.getSnapshot().status : idleStatus,\n idleStatus\n );\n useHold(client ?? null, true);\n return status;\n}\n\nconst identity = <T>(value: T) => value;\n\n/**\n * Read state built from messages by `createWebSocketState`. Re-renders only when the\n * selected value changes (by `isEqual`, default `Object.is`), however often messages\n * arrive. Keeps the connection open while mounted.\n *\n * On the server, and during hydration, it reads from `initialState`.\n *\n * @example\n * ```tsx\n * const acme = useWebSocketState(priceState, state => state.ACME);\n * ```\n */\nexport function useWebSocketState<S>(store: WebSocketState<S>): S;\nexport function useWebSocketState<S, T>(\n store: WebSocketState<S>,\n selector: (state: S) => T,\n isEqual?: (a: T, b: T) => boolean\n): T;\nexport function useWebSocketState<S, T>(\n store: WebSocketState<S>,\n selector: (state: S) => T = identity as (state: S) => T,\n isEqual: (a: T, b: T) => boolean = Object.is\n): T {\n const [getSelection, getServerSelection] = useMemo(() => {\n // Return the previous selection while the state is unchanged or the result is equal,\n // so inline selectors that build objects don't re-render (or loop) on every message.\n const memoized = (read: () => S) => {\n let memo: { state: S; selection: T } | undefined;\n return () => {\n const state = read();\n if (memo && Object.is(memo.state, state)) return memo.selection;\n const selection = selector(state);\n if (memo && isEqual(memo.selection, selection)) {\n memo = { state, selection: memo.selection };\n return memo.selection;\n }\n memo = { state, selection };\n return selection;\n };\n };\n return [memoized(store.getState), memoized(() => store.initialState)] as const;\n }, [store, selector, isEqual]);\n\n const selection = useSyncExternalStore(store.subscribe, getSelection, getServerSelection);\n useHold(store.client, true);\n return selection;\n}\n\n/**\n * Subscribe to one topic (or several) while mounted, through a `createWebSocketTopics`\n * manager. Components holding the same topic share one subscription, and it is sent again\n * after every reconnect. Keeps the connection open while mounted. `null` holds nothing.\n *\n * @example\n * ```tsx\n * useWebSocketTopic(topics, `prices:${symbol}`);\n * useWebSocketTopic(topics, watchlist.map(symbol => `prices:${symbol}`));\n * ```\n */\nexport function useWebSocketTopic(\n topics: WebSocketTopics | null | undefined,\n topic: string | readonly string[] | null | undefined\n): void {\n // A stable key, so a new array with the same topics doesn't resubscribe.\n const key = topic == null ? null : JSON.stringify(typeof topic === 'string' ? [topic] : topic);\n useEffect(() => {\n if (!topics || key === null) return;\n const releases = (JSON.parse(key) as string[]).map(t => topics.acquire(t));\n return () => releases.forEach(release => release());\n }, [topics, key]);\n}\n\n/**\n * Read round-trip time and server clock offset from a `createWebSocketLatency` instance.\n * Re-renders when a measurement changes, about once per ping interval. Keeps the connection\n * open while mounted. On the server, and during hydration, both values are `undefined`.\n *\n * @example\n * ```tsx\n * const { rtt } = useWebSocketLatency(latency);\n * return <span>{rtt === undefined ? '–' : `${Math.round(rtt)} ms`}</span>;\n * ```\n */\nexport function useWebSocketLatency(latency: WebSocketLatency): Latency {\n const snapshot = useSyncExternalStore(latency.subscribe, latency.getSnapshot, noLatency);\n useHold(latency.client, true);\n return snapshot;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,uBAAd;;;ACAA,mBAAqF;AAGrF,kBAAgC;AAsDhC,IAAM,OAAwB,OAAO,OAAO,EAAE,QAAQ,QAAQ,aAAa,QAAW,SAAS,EAAE,CAAC;AAClG,IAAM,eAAe,MAAM;AAC3B,IAAM,aAAa,MAAc;AACjC,IAAM,aAAsB,OAAO,OAAO,EAAE,KAAK,QAAW,QAAQ,OAAU,CAAC;AAC/E,I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js","names":["createWebSocket","useRef","useSyncExternalStore","useMemo"],"sources":["../src/react.ts"],"sourcesContent":["import { useEffect, useInsertionEffect, useMemo, useRef, useSyncExternalStore } from 'react';\n// Import the client through the `core` entry: the build keeps it external, so both entries\n// share one copy of it (and of `MessageError`).\nimport { createWebSocket } from './core';\nimport type { Latency, WebSocketLatency } from './latency';\n// Types only: a value import from anything but `./core` would bundle a second copy into this entry.\nimport type { IncomingRequest, WebSocketResponder } from './responder';\nimport type { WebSocketState } from './state';\nimport type { WebSocketTopics } from './topics';\nimport type {\n CloseInfo,\n EventMap,\n SendOptions,\n Snapshot,\n Status,\n WebSocketClient,\n WebSocketOptions\n} from './types';\n\n/**\n * Per-component event handlers. They always see the component's latest props and\n * state, and changing them never reconnects.\n */\nexport interface WebSocketHandlers<In> {\n /** The socket opened (initially or after a reconnect). */\n onOpen?: (event: Event) => void;\n /** A message arrived and passed `parse`/`json` and `schema`. */\n onMessage?: (message: In, event: MessageEvent) => void;\n /** A socket closed. Called whether or not it will be retried. */\n onClose?: (close: CloseInfo) => void;\n /** A socket error `Event`, a `MessageError`, or an `Error` from the constructor or `url` function. */\n onError?: (error: Event | Error) => void;\n /**\n * `false` releases this component's hold on the connection and stops its handlers.\n * The connection closes once nothing else holds it.\n * @default true\n */\n enabled?: boolean;\n}\n\n/** Options for `useWebSocket(url, options)`: connection options plus handlers. */\nexport interface UseWebSocketOptions<In, Out>\n extends WebSocketOptions<In, Out>, WebSocketHandlers<In> {}\n\n/** What `useWebSocket` returns: the current {@link Snapshot} plus stable actions. */\nexport interface UseWebSocketResult<In, Out> extends Snapshot<In> {\n /**\n * Send now, or queue until open (see {@link SendOptions} for volatile and keyed sends).\n * Returns `true` if sent immediately. Stable identity.\n */\n send: (message: Out, options?: SendOptions) => boolean;\n /** Close and stop reconnecting (for every component sharing the connection). Stable identity. */\n close: (code?: number, reason?: string) => void;\n /** Drop the socket and connect again right away. Stable identity. */\n reconnect: () => void;\n /** The underlying client, or `null` when the URL is `null`/`undefined`. */\n client: WebSocketClient<In, Out> | null;\n}\n\nconst IDLE: Snapshot<never> = Object.freeze({ status: 'idle', lastMessage: undefined, attempt: 0 });\nconst idleSnapshot = () => IDLE;\nconst idleStatus = (): Status => 'idle';\nconst NO_LATENCY: Latency = Object.freeze({ rtt: undefined, offset: undefined });\nconst noLatency = () => NO_LATENCY;\n// Not frozen: a call would keep it in bundles that never use the hook.\nconst NO_REQUESTS: readonly never[] = [];\nconst noRequests = () => NO_REQUESTS;\nconst noopSubscribe = () => () => {};\nconst noopSend = () => false;\nconst noop = () => {};\n\n// Same key as `ON_TEARDOWN` in ./client, which this entry must not import directly.\nconst ON_TEARDOWN = Symbol.for('react-websocket.onTeardown');\n\n// Hooks given the same URL (and protocols) share one connection.\nconst registry = new Map<string, WebSocketClient<any, any>>();\n\nfunction sharedClient<In, Out>(url: string, options: WebSocketOptions<In, Out>) {\n // Don't let server renders accumulate clients that can never be torn down.\n if (typeof window === 'undefined') return createWebSocket(url, options);\n const key = `${url}\\u0000${JSON.stringify(options.protocols ?? null)}`;\n let client = registry.get(key) as WebSocketClient<In, Out> | undefined;\n if (!client) {\n const created: WebSocketClient<In, Out> = createWebSocket(url, {\n ...options,\n [ON_TEARDOWN]: () => {\n if (registry.get(key) === created) registry.delete(key);\n }\n } as WebSocketOptions<In, Out>);\n registry.set(key, created);\n client = created;\n }\n return client;\n}\n\n/** Always-current value for use inside effects and event listeners (a `useEffectEvent` stand-in). */\nfunction useLatest<T>(value: T) {\n const ref = useRef(value);\n useInsertionEffect(() => {\n ref.current = value;\n });\n return ref;\n}\n\nfunction useHold(client: WebSocketClient<any, any> | null, enabled: boolean) {\n useEffect(() => {\n if (client && enabled) return client.acquire();\n }, [client, enabled]);\n}\n\n/**\n * Connect to a WebSocket and re-render with its status and latest message.\n *\n * - With a URL, every component using the same URL (and `protocols`) shares one\n * connection. Its options come from whichever component created it first.\n * - With a client from `createWebSocket()`, options are the client's; only handlers\n * and `enabled` apply here.\n * - With `null` or `undefined`, nothing connects and the status stays `idle`.\n *\n * The connection opens on mount and closes one tick after the last component using it\n * unmounts, so StrictMode and `<Activity>` don't open duplicate sockets.\n *\n * Re-renders on status changes and on every message. For high-frequency streams use\n * `useWebSocketEvent` and `useWebSocketStatus` instead.\n *\n * @example\n * ```tsx\n * const { status, lastMessage, send } = useWebSocket<Price, Order>('wss://api.example.com/prices', {\n * json: true,\n * onMessage: price => console.log(price)\n * });\n * ```\n */\nexport function useWebSocket<In = unknown, Out = unknown>(\n url: string | null | undefined,\n options?: UseWebSocketOptions<In, Out>\n): UseWebSocketResult<In, Out>;\nexport function useWebSocket<In, Out>(\n client: WebSocketClient<In, Out> | null | undefined,\n options?: WebSocketHandlers<In>\n): UseWebSocketResult<In, Out>;\nexport function useWebSocket<In, Out>(\n target: string | WebSocketClient<In, Out> | null | undefined,\n options: UseWebSocketOptions<In, Out> = {}\n): UseWebSocketResult<In, Out> {\n const client =\n target == null ? null : typeof target === 'string' ? sharedClient(target, options) : target;\n const enabled = options.enabled ?? true;\n\n const snapshot = useSyncExternalStore<Snapshot<In>>(\n client ? client.subscribe : noopSubscribe,\n client ? client.getSnapshot : idleSnapshot,\n idleSnapshot\n );\n\n const handlers = useLatest(options);\n useEffect(() => {\n if (!client || !enabled) return;\n const offs = [\n client.on('open', event => handlers.current.onOpen?.(event)),\n client.on('message', (message, event) => handlers.current.onMessage?.(message, event)),\n client.on('close', info => handlers.current.onClose?.(info)),\n client.on('error', error => handlers.current.onError?.(error))\n ];\n return () => offs.forEach(off => off());\n }, [client, enabled, handlers]);\n useHold(client, enabled);\n\n return {\n ...snapshot,\n send: client ? client.send : noopSend,\n close: client ? client.close : noop,\n reconnect: client ? client.reconnect : noop,\n client\n };\n}\n\n/**\n * Listen to a client's events without re-rendering the component. The listener always\n * sees the latest props and state. Keeps the connection open while mounted.\n *\n * @example\n * ```tsx\n * useWebSocketEvent(chat, 'message', message => setMessages(list => [...list, message]));\n * ```\n */\nexport function useWebSocketEvent<In, K extends keyof EventMap<In>>(\n client: WebSocketClient<In, any> | null | undefined,\n type: K,\n listener: EventMap<In>[K]\n): void {\n const latest = useLatest(listener);\n useEffect(() => {\n if (!client) return;\n const off = client.on(type, ((...args: unknown[]) =>\n (latest.current as (...a: unknown[]) => void)(...args)) as EventMap<In>[K]);\n const release = client.acquire();\n return () => {\n off();\n release();\n };\n }, [client, type, latest]);\n}\n\n/**\n * The connection status of a client. Re-renders only when the status changes, not on\n * messages. Keeps the connection open while mounted.\n *\n * @example\n * ```tsx\n * const status = useWebSocketStatus(chat); // 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed'\n * ```\n */\nexport function useWebSocketStatus(client: WebSocketClient<any, any> | null | undefined): Status {\n const status = useSyncExternalStore(\n client ? client.subscribe : noopSubscribe,\n client ? () => client.getSnapshot().status : idleStatus,\n idleStatus\n );\n useHold(client ?? null, true);\n return status;\n}\n\nconst identity = <T>(value: T) => value;\n\n/**\n * Read state built from messages by `createWebSocketState`. Re-renders only when the\n * selected value changes (by `isEqual`, default `Object.is`), however often messages\n * arrive. Keeps the connection open while mounted.\n *\n * On the server, and during hydration, it reads from `initialState`.\n *\n * @example\n * ```tsx\n * const acme = useWebSocketState(priceState, state => state.ACME);\n * ```\n */\nexport function useWebSocketState<S>(store: WebSocketState<S>): S;\nexport function useWebSocketState<S, T>(\n store: WebSocketState<S>,\n selector: (state: S) => T,\n isEqual?: (a: T, b: T) => boolean\n): T;\nexport function useWebSocketState<S, T>(\n store: WebSocketState<S>,\n selector: (state: S) => T = identity as (state: S) => T,\n isEqual: (a: T, b: T) => boolean = Object.is\n): T {\n const [getSelection, getServerSelection] = useMemo(() => {\n // Return the previous selection while the state is unchanged or the result is equal,\n // so inline selectors that build objects don't re-render (or loop) on every message.\n const memoized = (read: () => S) => {\n let memo: { state: S; selection: T } | undefined;\n return () => {\n const state = read();\n if (memo && Object.is(memo.state, state)) return memo.selection;\n const selection = selector(state);\n if (memo && isEqual(memo.selection, selection)) {\n memo = { state, selection: memo.selection };\n return memo.selection;\n }\n memo = { state, selection };\n return selection;\n };\n };\n return [memoized(store.getState), memoized(() => store.initialState)] as const;\n }, [store, selector, isEqual]);\n\n const selection = useSyncExternalStore(store.subscribe, getSelection, getServerSelection);\n useHold(store.client, true);\n return selection;\n}\n\n/**\n * Subscribe to one topic (or several) while mounted, through a `createWebSocketTopics`\n * manager. Components holding the same topic share one subscription, and it is sent again\n * after every reconnect. Keeps the connection open while mounted. `null` holds nothing.\n *\n * @example\n * ```tsx\n * useWebSocketTopic(topics, `prices:${symbol}`);\n * useWebSocketTopic(topics, watchlist.map(symbol => `prices:${symbol}`));\n * ```\n */\nexport function useWebSocketTopic(\n topics: WebSocketTopics | null | undefined,\n topic: string | readonly string[] | null | undefined\n): void {\n // A stable key, so a new array with the same topics doesn't resubscribe.\n const key = topic == null ? null : JSON.stringify(typeof topic === 'string' ? [topic] : topic);\n useEffect(() => {\n if (!topics || key === null) return;\n const releases = (JSON.parse(key) as string[]).map(t => topics.acquire(t));\n return () => releases.forEach(release => release());\n }, [topics, key]);\n}\n\n/**\n * Read round-trip time and server clock offset from a `createWebSocketLatency` instance.\n * Re-renders when a measurement changes, about once per ping interval. Keeps the connection\n * open while mounted. On the server, and during hydration, both values are `undefined`.\n *\n * @example\n * ```tsx\n * const { rtt } = useWebSocketLatency(latency);\n * return <span>{rtt === undefined ? '–' : `${Math.round(rtt)} ms`}</span>;\n * ```\n */\nexport function useWebSocketLatency(latency: WebSocketLatency): Latency {\n const snapshot = useSyncExternalStore(latency.subscribe, latency.getSnapshot, noLatency);\n useHold(latency.client, true);\n return snapshot;\n}\n\n/**\n * Requests for `method` that a person answers, e.g. an agent asking to run a tool. While mounted,\n * it registers `method` on the responder without a handler, so new requests wait here instead of\n * getting a `NotFoundError` reply. Call `respond` or `reject` on one to answer it. Re-renders\n * when the list changes. Keeps the connection open while mounted. `null` holds nothing.\n *\n * Requests stay listed after unmount until they are answered, time out, are cancelled or lose\n * their connection, so a remounted dialog shows them again. On the server the list is empty.\n *\n * @example\n * ```tsx\n * const [request] = useAgentApproval(tools, 'approve');\n * if (!request) return null;\n * return (\n * <dialog open>\n * {request.params.summary}\n * <button onClick={() => request.respond({ approved: true })}>Approve</button>\n * <button onClick={() => request.respond({ approved: false })}>Deny</button>\n * </dialog>\n * );\n * ```\n */\nexport function useAgentApproval<P, R>(\n responder: WebSocketResponder<P, R> | null | undefined,\n method: string | null | undefined\n): readonly IncomingRequest<P, R>[] {\n const waiting = useSyncExternalStore(\n responder ? responder.subscribe : noopSubscribe,\n responder ? responder.getSnapshot : noRequests,\n noRequests\n );\n useEffect(() => {\n if (responder && method != null) return responder.onRequest(method);\n }, [responder, method]);\n useHold(responder?.client ?? null, method != null);\n return useMemo(() => {\n const mine = waiting.filter(request => request.method === method);\n return mine.length ? mine : NO_REQUESTS;\n }, [waiting, method]);\n}\n"],"mappings":";;;;;AA2DA,MAAM,OAAwB,OAAO,OAAO;CAAE,QAAQ;CAAQ,aAAa,KAAA;CAAW,SAAS;AAAE,CAAC;AAClG,MAAM,qBAAqB;AAC3B,MAAM,mBAA2B;AACjC,MAAM,aAAsB,OAAO,OAAO;CAAE,KAAK,KAAA;CAAW,QAAQ,KAAA;AAAU,CAAC;AAC/E,MAAM,kBAAkB;AAExB,MAAM,cAAgC,CAAC;AACvC,MAAM,mBAAmB;AACzB,MAAM,4BAA4B,CAAC;AACnC,MAAM,iBAAiB;AACvB,MAAM,aAAa,CAAC;AAGpB,MAAM,cAAc,OAAO,IAAI,4BAA4B;AAG3D,MAAM,2BAAW,IAAI,IAAuC;AAE5D,SAAS,aAAsB,KAAa,SAAoC;CAE9E,IAAI,OAAO,WAAW,aAAa,QAAA,GAAOA,WAAAA,gBAAAA,CAAgB,KAAK,OAAO;CACtE,MAAM,MAAM,GAAG,IAAI,QAAQ,KAAK,UAAU,QAAQ,aAAa,IAAI;CACnE,IAAI,SAAS,SAAS,IAAI,GAAG;CAC7B,IAAI,CAAC,QAAQ;EACX,MAAM,WAAA,GAAoCA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