react-websocket 3.6.0 → 4.18.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/README.md +34 -5
- package/dist/ag-ui.cjs +421 -0
- package/dist/ag-ui.cjs.map +1 -0
- package/dist/ag-ui.d.cts +493 -0
- package/dist/ag-ui.d.ts +171 -0
- package/dist/ag-ui.js +418 -0
- package/dist/ag-ui.js.map +1 -0
- package/dist/core.cjs +727 -162
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +501 -62
- package/dist/core.d.ts +501 -62
- package/dist/core.js +723 -163
- package/dist/core.js.map +1 -1
- package/dist/devtools.cjs +186 -0
- package/dist/devtools.cjs.map +1 -0
- package/dist/devtools.d.cts +32 -0
- package/dist/devtools.d.ts +32 -0
- package/dist/devtools.js +185 -0
- package/dist/devtools.js.map +1 -0
- package/dist/index.cjs +214 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +705 -56
- package/dist/index.d.ts +421 -30
- package/dist/index.js +211 -21
- package/dist/index.js.map +1 -1
- package/dist/outbox.cjs +222 -0
- package/dist/outbox.cjs.map +1 -0
- package/dist/outbox.d.cts +127 -0
- package/dist/outbox.d.ts +127 -0
- package/dist/outbox.js +219 -0
- package/dist/outbox.js.map +1 -0
- package/dist/replay.cjs +324 -0
- package/dist/replay.cjs.map +1 -0
- package/dist/replay.d.cts +124 -0
- package/dist/replay.d.ts +124 -0
- package/dist/replay.js +322 -0
- package/dist/replay.js.map +1 -0
- package/dist/tabs.cjs +336 -0
- package/dist/tabs.cjs.map +1 -0
- package/dist/tabs.d.cts +81 -0
- package/dist/tabs.d.ts +81 -0
- package/dist/tabs.js +335 -0
- package/dist/tabs.js.map +1 -0
- package/dist/timeline.d.cts +1 -213
- package/dist/types-BEecFqIB.d.cts +268 -0
- package/dist/webtransport.cjs +243 -0
- package/dist/webtransport.cjs.map +1 -0
- package/dist/webtransport.d.cts +67 -0
- package/dist/webtransport.d.ts +67 -0
- package/dist/webtransport.js +242 -0
- package/dist/webtransport.js.map +1 -0
- package/package.json +139 -3
package/dist/timeline.d.cts
CHANGED
|
@@ -1,216 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Where to connect: a URL, or a function that returns one (sync or async).
|
|
4
|
-
*
|
|
5
|
-
* A function is called before **every** connection attempt, including reconnects,
|
|
6
|
-
* so it can put a fresh auth token in the URL. If it throws or rejects, that counts
|
|
7
|
-
* as a failed attempt and is retried with backoff.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* createWebSocket(async () => `wss://api.example.com/live?token=${await getToken()}`);
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
type UrlSource = string | (() => string | Promise<string>);
|
|
15
|
-
/**
|
|
16
|
-
* Connection state.
|
|
17
|
-
*
|
|
18
|
-
* - `idle`: not connected and not trying to be. Nobody is using the client yet, or everyone stopped.
|
|
19
|
-
* - `connecting`: the first attempt is in progress.
|
|
20
|
-
* - `open`: connected. `send()` goes out immediately.
|
|
21
|
-
* - `reconnecting`: the connection dropped (or an attempt failed) and a retry is scheduled or in progress.
|
|
22
|
-
* - `closed`: stopped for good, by `close()` or after giving up on reconnecting.
|
|
23
|
-
* It stays closed until `connect()` or `reconnect()` is called, or every hook unmounts.
|
|
24
|
-
*/
|
|
25
|
-
type Status = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
|
|
26
|
-
/** The client's state as seen by React. A new object is created on every change. */
|
|
27
|
-
interface Snapshot<In> {
|
|
28
|
-
/** Connection state. */
|
|
29
|
-
readonly status: Status;
|
|
30
|
-
/** Most recent message (after `parse`/`json` and `schema`), kept across reconnects. */
|
|
31
|
-
readonly lastMessage: In | undefined;
|
|
32
|
-
/** Consecutive failed attempts since the last successful open. `0` while open. */
|
|
33
|
-
readonly attempt: number;
|
|
34
|
-
}
|
|
35
|
-
/** Details of a closed connection, from the browser's `CloseEvent`. */
|
|
36
|
-
interface CloseInfo {
|
|
37
|
-
/** Close code: `1000` is a normal close, `1006` an abnormal one (e.g. network loss), `4000`–`4999` are yours. */
|
|
38
|
-
readonly code: number;
|
|
39
|
-
/** Close reason sent by the side that closed the connection. Often empty. */
|
|
40
|
-
readonly reason: string;
|
|
41
|
-
/** Whether the closing handshake completed. */
|
|
42
|
-
readonly wasClean: boolean;
|
|
43
|
-
}
|
|
44
|
-
/** Per-message options for `send()`. */
|
|
45
|
-
interface SendOptions {
|
|
46
|
-
/**
|
|
47
|
-
* Drop the message instead of queueing it when the socket isn't open. For data that
|
|
48
|
-
* is stale by the time a reconnect finishes, such as player input or cursor positions.
|
|
49
|
-
* @default false
|
|
50
|
-
*/
|
|
51
|
-
volatile?: boolean;
|
|
52
|
-
/**
|
|
53
|
-
* Keep only the newest queued message with this key. Sending again with the same key
|
|
54
|
-
* while disconnected replaces the earlier one, which moves to the back of the queue
|
|
55
|
-
* (or of the `priority` messages, for a priority one).
|
|
56
|
-
* For state where only the latest value matters, such as "my position is now (x, y)".
|
|
57
|
-
*/
|
|
58
|
-
key?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Queue this message ahead of every message queued without `priority`, after earlier
|
|
61
|
-
* priority messages. For control messages that must not wait behind a backlog, such as
|
|
62
|
-
* cancel, interrupt or a token refresh. Messages sent from `open` listeners still go first.
|
|
63
|
-
*
|
|
64
|
-
* It only matters while the socket isn't open: once a message is handed to the browser,
|
|
65
|
-
* nothing can overtake it. To keep that buffer short, send bulky streams with `maxBuffered`.
|
|
66
|
-
* @default false
|
|
67
|
-
*/
|
|
68
|
-
priority?: boolean;
|
|
69
|
-
/**
|
|
70
|
-
* Drop the message instead of sending it while more than this many bytes are already
|
|
71
|
-
* waiting in the browser's send buffer (`socket.bufferedAmount`). For high-rate streams on
|
|
72
|
-
* a slow link, such as audio frames or per-tick input, where a late message is worse than
|
|
73
|
-
* none. `send()` returns `false`, and `onDrop` isn't called.
|
|
74
|
-
*
|
|
75
|
-
* Only checked while the socket is open: a message queued while disconnected is sent on
|
|
76
|
-
* open regardless. `bufferedAmount` includes what you sent earlier in the same task, so set
|
|
77
|
-
* it well above one burst of messages.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* voice.send(frame, { volatile: true, maxBuffered: 64 * 1024 });
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
maxBuffered?: number;
|
|
85
|
-
/**
|
|
86
|
-
* Called if the message is dropped from the queue without being sent: replaced by a
|
|
87
|
-
* newer message with the same `key`, or evicted by `queue.limit`. Not called when the
|
|
88
|
-
* queue is cleared because the client closed or gave up, or when `send()` drops the
|
|
89
|
-
* message right away (`volatile` while not open, or `maxBuffered`) and returns `false`.
|
|
90
|
-
*/
|
|
91
|
-
onDrop?: () => void;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Reported via the `error` event (and `onError`) when an incoming message can't be
|
|
95
|
-
* decoded by `parse`/`json` or fails `schema` validation. The message is not delivered.
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```ts
|
|
99
|
-
* onError(error) {
|
|
100
|
-
* if (error instanceof MessageError) report(error.data, error.issues);
|
|
101
|
-
* }
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
declare class MessageError extends Error {
|
|
105
|
-
/** The raw `event.data` that failed. */
|
|
106
|
-
readonly data: unknown;
|
|
107
|
-
/** Schema validation issues, when the message failed `schema`. */
|
|
108
|
-
readonly issues: ReadonlyArray<{
|
|
109
|
-
readonly message: string;
|
|
110
|
-
}> | undefined;
|
|
111
|
-
constructor(message: string, data: unknown, options?: {
|
|
112
|
-
cause?: unknown;
|
|
113
|
-
issues?: ReadonlyArray<{
|
|
114
|
-
readonly message: string;
|
|
115
|
-
}>;
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
/** Events emitted by a client. Subscribe with `client.on(type, listener)` or `useWebSocketEvent`. */
|
|
119
|
-
interface EventMap<In> {
|
|
120
|
-
/**
|
|
121
|
-
* The socket opened (initially or after a reconnect). Listeners run before queued
|
|
122
|
-
* messages are sent, so a handshake sent from here goes out first.
|
|
123
|
-
*/
|
|
124
|
-
open: (event: Event) => void;
|
|
125
|
-
/** A message arrived and passed `parse` and `schema`. */
|
|
126
|
-
message: (message: In, event: MessageEvent) => void;
|
|
127
|
-
/** A socket closed, whether it will be retried or not. */
|
|
128
|
-
close: (close: CloseInfo) => void;
|
|
129
|
-
/**
|
|
130
|
-
* A socket error `Event`, a {@link MessageError} for an undecodable or invalid message,
|
|
131
|
-
* or the `Error` thrown by the `WebSocket` constructor or a `url` function.
|
|
132
|
-
*/
|
|
133
|
-
error: (error: Event | MessageError | Error) => void;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* A WebSocket connection that reconnects, queues, decodes and can be shared.
|
|
137
|
-
* Create one with `createWebSocket()`.
|
|
138
|
-
*
|
|
139
|
-
* The connection is reference-counted: it opens when the first hook mounts (or
|
|
140
|
-
* `ready` is read) and closes one tick after the last hook unmounts. Calling
|
|
141
|
-
* `connect()` keeps it open regardless, until `close()`.
|
|
142
|
-
*
|
|
143
|
-
* @typeParam In - Type of incoming messages.
|
|
144
|
-
* @typeParam Out - Type accepted by `send()`.
|
|
145
|
-
*/
|
|
146
|
-
interface WebSocketClient<In = unknown, Out = unknown> {
|
|
147
|
-
/** The URL (or URL function) the client was created with. */
|
|
148
|
-
readonly url: UrlSource;
|
|
149
|
-
/**
|
|
150
|
-
* A promise that resolves once the socket has opened. Reading it starts an idle client.
|
|
151
|
-
*
|
|
152
|
-
* It stays resolved through automatic reconnects, so `use(client.ready)` shows a
|
|
153
|
-
* Suspense fallback only for the first connection, never on a network blip; use
|
|
154
|
-
* `status` to show reconnects. It rejects when the client gives up reconnecting, when
|
|
155
|
-
* `close()` is called, and during a server render. After that, `connect()` or
|
|
156
|
-
* `reconnect()` make it a new pending promise.
|
|
157
|
-
*
|
|
158
|
-
* It is safe to pass to React 19's `use()`: the same promise is returned until its state
|
|
159
|
-
* changes, and it carries `status`/`value`/`reason` so `use()` doesn't suspend when it is
|
|
160
|
-
* already settled.
|
|
161
|
-
*/
|
|
162
|
-
readonly ready: Promise<void>;
|
|
163
|
-
/** The current underlying `WebSocket`, if any. An escape hatch; prefer the client's methods. */
|
|
164
|
-
readonly socket: WebSocket | null;
|
|
165
|
-
/** Subscribe to snapshot changes. Compatible with `useSyncExternalStore`. Returns an unsubscribe function. */
|
|
166
|
-
subscribe(listener: () => void): () => void;
|
|
167
|
-
/** The current {@link Snapshot}. Returns the same object until something changes. */
|
|
168
|
-
getSnapshot(): Snapshot<In>;
|
|
169
|
-
/**
|
|
170
|
-
* Listen to an event. Returns an unsubscribe function. Listener errors are re-thrown
|
|
171
|
-
* asynchronously, so one failing listener doesn't break the others.
|
|
172
|
-
*/
|
|
173
|
-
on<K extends keyof EventMap<In>>(type: K, listener: EventMap<In>[K]): () => void;
|
|
174
|
-
/**
|
|
175
|
-
* Send a message (encoded with `serialize`/`json` if configured). If the socket isn't
|
|
176
|
-
* open, the message is queued and sent on open (see the `queue` option), unless
|
|
177
|
-
* `options.volatile` is set.
|
|
178
|
-
* @returns `true` if it was sent immediately, `false` if it was queued or dropped
|
|
179
|
-
* (`volatile` while not open, or `maxBuffered` while open).
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```ts
|
|
183
|
-
* game.send({ type: 'input', dx, dy }, { volatile: true }); // never replayed after a reconnect
|
|
184
|
-
* game.send({ type: 'position', x, y }, { key: 'position' }); // only the newest is queued
|
|
185
|
-
* game.send({ type: 'leave' }, { priority: true }); // ahead of everything already queued
|
|
186
|
-
* ```
|
|
187
|
-
*/
|
|
188
|
-
send(message: Out, options?: SendOptions): boolean;
|
|
189
|
-
/**
|
|
190
|
-
* Start connecting, and keep the connection open even when no hook is using the
|
|
191
|
-
* client, until `close()` is called. Does nothing if already connecting or open.
|
|
192
|
-
*/
|
|
193
|
-
connect(): void;
|
|
194
|
-
/**
|
|
195
|
-
* Close the connection and stop reconnecting. The status becomes `closed` and the
|
|
196
|
-
* send queue is cleared. Mounted hooks stay mounted; call `connect()` or `reconnect()`
|
|
197
|
-
* to start again.
|
|
198
|
-
* @param code - Close code. @default 1000
|
|
199
|
-
*/
|
|
200
|
-
close(code?: number, reason?: string): void;
|
|
201
|
-
/**
|
|
202
|
-
* Drop the current socket (if any) and connect again immediately, resetting the
|
|
203
|
-
* backoff. Also restarts a `closed` client.
|
|
204
|
-
*/
|
|
205
|
-
reconnect(): void;
|
|
206
|
-
/**
|
|
207
|
-
* Hold the connection open until the returned function is called. This is what the
|
|
208
|
-
* hooks use; call it yourself to integrate with non-React code.
|
|
209
|
-
* @returns A release function (safe to call more than once).
|
|
210
|
-
*/
|
|
211
|
-
acquire(): () => void;
|
|
212
|
-
}
|
|
213
|
-
//#endregion
|
|
1
|
+
import { t as WebSocketClient } from "./types-BEecFqIB.cjs";
|
|
214
2
|
//#region src/latency.d.ts
|
|
215
3
|
/** Latest latency measurements. A new object is created on every change. */
|
|
216
4
|
interface Latency {
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Where to connect: a URL, or a function that returns one (sync or async).
|
|
4
|
+
*
|
|
5
|
+
* A function is called before **every** connection attempt, including reconnects,
|
|
6
|
+
* so it can put a fresh auth token in the URL. If it throws or rejects, that counts
|
|
7
|
+
* as a failed attempt and is retried with backoff.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* createWebSocket(async () => `wss://api.example.com/live?token=${await getToken()}`);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
type UrlSource = string | (() => string | Promise<string>);
|
|
15
|
+
/**
|
|
16
|
+
* Connection state.
|
|
17
|
+
*
|
|
18
|
+
* - `idle`: not connected and not trying to be. Nobody is using the client yet, or everyone stopped.
|
|
19
|
+
* - `connecting`: the first attempt is in progress.
|
|
20
|
+
* - `open`: connected. `send()` goes out immediately.
|
|
21
|
+
* - `reconnecting`: the connection dropped (or an attempt failed) and a retry is scheduled or in progress.
|
|
22
|
+
* - `closed`: stopped for good, by `close()` or after giving up on reconnecting.
|
|
23
|
+
* It stays closed until `connect()` or `reconnect()` is called, or every hook unmounts. If
|
|
24
|
+
* nothing held it when it stopped, the next hook to mount starts it again.
|
|
25
|
+
*/
|
|
26
|
+
type Status = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
|
|
27
|
+
/** The client's state as seen by React. A new object is created on every change. */
|
|
28
|
+
interface Snapshot<In> {
|
|
29
|
+
/** Connection state. */
|
|
30
|
+
readonly status: Status;
|
|
31
|
+
/** Most recent message (after `parse`/`json` and `schema`), kept across reconnects. */
|
|
32
|
+
readonly lastMessage: In | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Consecutive failed attempts since the last stable connection. A connection that closes
|
|
35
|
+
* within `reconnect.minUptime` of opening counts as failed. `0` while open.
|
|
36
|
+
*/
|
|
37
|
+
readonly attempt: number;
|
|
38
|
+
}
|
|
39
|
+
/** Details of a closed connection, from the browser's `CloseEvent`. */
|
|
40
|
+
interface CloseInfo {
|
|
41
|
+
/** Close code: `1000` is a normal close, `1006` an abnormal one (e.g. network loss), `4000`–`4999` are yours. */
|
|
42
|
+
readonly code: number;
|
|
43
|
+
/** Close reason sent by the side that closed the connection. Often empty. */
|
|
44
|
+
readonly reason: string;
|
|
45
|
+
/** Whether the closing handshake completed. */
|
|
46
|
+
readonly wasClean: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** Options for `client.migrate()`. */
|
|
49
|
+
interface MigrateOptions {
|
|
50
|
+
/**
|
|
51
|
+
* Where to connect instead: a URL, or a function called before the connection, like the
|
|
52
|
+
* client's own `url`. Later reconnects go there too, until the client is closed, gives up or
|
|
53
|
+
* is torn down.
|
|
54
|
+
* @default the client's current URL
|
|
55
|
+
*/
|
|
56
|
+
url?: UrlSource;
|
|
57
|
+
/**
|
|
58
|
+
* Move at a random moment within this many ms, so that clients asked to move together don't
|
|
59
|
+
* all reconnect at once.
|
|
60
|
+
* @default 0
|
|
61
|
+
*/
|
|
62
|
+
within?: number;
|
|
63
|
+
}
|
|
64
|
+
/** Per-message options for `send()`. */
|
|
65
|
+
interface SendOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Drop the message instead of queueing it when the socket isn't open. For data that
|
|
68
|
+
* is stale by the time a reconnect finishes, such as player input or cursor positions.
|
|
69
|
+
* @default false
|
|
70
|
+
*/
|
|
71
|
+
volatile?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Keep only the newest queued message with this key. Sending again with the same key
|
|
74
|
+
* while disconnected replaces the earlier one, which moves to the back of the queue
|
|
75
|
+
* (or of the `priority` messages, for a priority one).
|
|
76
|
+
* For state where only the latest value matters, such as "my position is now (x, y)".
|
|
77
|
+
*/
|
|
78
|
+
key?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Queue this message ahead of every message queued without `priority`, after earlier
|
|
81
|
+
* priority messages. For control messages that must not wait behind a backlog, such as
|
|
82
|
+
* cancel, interrupt or a token refresh. Messages sent from `open` listeners still go first.
|
|
83
|
+
*
|
|
84
|
+
* It only matters while the socket isn't open: once a message is handed to the browser,
|
|
85
|
+
* nothing can overtake it. To keep that buffer short, send bulky streams with `maxBuffered`.
|
|
86
|
+
* @default false
|
|
87
|
+
*/
|
|
88
|
+
priority?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Drop the message instead of sending it while more than this many bytes are already
|
|
91
|
+
* waiting in the browser's send buffer (`socket.bufferedAmount`). For high-rate streams on
|
|
92
|
+
* a slow link, such as audio frames or per-tick input, where a late message is worse than
|
|
93
|
+
* none. `send()` returns `false`, and `onDrop` isn't called.
|
|
94
|
+
*
|
|
95
|
+
* Only checked while the socket is open: a message queued while disconnected is sent on
|
|
96
|
+
* open regardless. `bufferedAmount` includes what you sent earlier in the same task, so set
|
|
97
|
+
* it well above one burst of messages.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* voice.send(frame, { volatile: true, maxBuffered: 64 * 1024 });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
maxBuffered?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Drop the message instead of sending it if it has been queued for longer than this many
|
|
107
|
+
* ms, for messages that go stale during a long outage, such as "typing" or a price quote.
|
|
108
|
+
* Overrides `queue.maxAge`. Checked when the socket opens and whenever another message is
|
|
109
|
+
* queued; `onDrop` is called then.
|
|
110
|
+
*/
|
|
111
|
+
maxAge?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Called if the message is dropped from the queue without being sent: replaced by a
|
|
114
|
+
* newer message with the same `key`, evicted by `queue.limit`, or older than `maxAge`. Not
|
|
115
|
+
* called when the queue is cleared because the client closed or gave up, or when `send()`
|
|
116
|
+
* drops the message right away (`volatile` while not open, or `maxBuffered`) and returns
|
|
117
|
+
* `false`.
|
|
118
|
+
*/
|
|
119
|
+
onDrop?: () => void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Reported via the `error` event (and `onError`) when an incoming message can't be
|
|
123
|
+
* decoded by `parse`/`json` or fails `schema` validation. The message is not delivered.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* onError(error) {
|
|
128
|
+
* if (error instanceof MessageError) report(error.data, error.issues);
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare class MessageError extends Error {
|
|
133
|
+
/** The raw `event.data` that failed. */
|
|
134
|
+
readonly data: unknown;
|
|
135
|
+
/** Schema validation issues, when the message failed `schema`. */
|
|
136
|
+
readonly issues: ReadonlyArray<{
|
|
137
|
+
readonly message: string;
|
|
138
|
+
}> | undefined;
|
|
139
|
+
constructor(message: string, data: unknown, options?: {
|
|
140
|
+
cause?: unknown;
|
|
141
|
+
issues?: ReadonlyArray<{
|
|
142
|
+
readonly message: string;
|
|
143
|
+
}>;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/** Events emitted by a client. Subscribe with `client.on(type, listener)` or `useWebSocketEvent`. */
|
|
147
|
+
interface EventMap<In> {
|
|
148
|
+
/**
|
|
149
|
+
* The socket opened (initially or after a reconnect). Listeners run before queued
|
|
150
|
+
* messages are sent, so a handshake sent from here goes out first.
|
|
151
|
+
*/
|
|
152
|
+
open: (event: Event) => void;
|
|
153
|
+
/** A message arrived and passed `parse` and `schema`. */
|
|
154
|
+
message: (message: In, event: MessageEvent) => void;
|
|
155
|
+
/** A socket closed, whether it will be retried or not. */
|
|
156
|
+
close: (close: CloseInfo) => void;
|
|
157
|
+
/**
|
|
158
|
+
* A socket error `Event`, a {@link MessageError} for an undecodable or invalid message,
|
|
159
|
+
* or the `Error` thrown by the `WebSocket` constructor or a `url` function.
|
|
160
|
+
*/
|
|
161
|
+
error: (error: Event | MessageError | Error) => void;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* A WebSocket connection that reconnects, queues, decodes and can be shared.
|
|
165
|
+
* Create one with `createWebSocket()`.
|
|
166
|
+
*
|
|
167
|
+
* The connection is reference-counted: it opens when the first hook mounts (or
|
|
168
|
+
* `ready` is read) and closes one tick after the last hook unmounts. Calling
|
|
169
|
+
* `connect()` keeps it open regardless, until `close()`.
|
|
170
|
+
*
|
|
171
|
+
* @typeParam In - Type of incoming messages.
|
|
172
|
+
* @typeParam Out - Type accepted by `send()`.
|
|
173
|
+
*/
|
|
174
|
+
interface WebSocketClient<In = unknown, Out = unknown> {
|
|
175
|
+
/** The URL (or URL function) the client was created with. */
|
|
176
|
+
readonly url: UrlSource;
|
|
177
|
+
/**
|
|
178
|
+
* A promise that resolves once the socket has opened. Reading it starts an idle client.
|
|
179
|
+
*
|
|
180
|
+
* It stays resolved through automatic reconnects, so `use(client.ready)` shows a
|
|
181
|
+
* Suspense fallback only for the first connection, never on a network blip; use
|
|
182
|
+
* `status` to show reconnects. It rejects when the client gives up reconnecting, when
|
|
183
|
+
* `close()` is called, and during a server render. After that, `connect()` or
|
|
184
|
+
* `reconnect()` make it a new pending promise.
|
|
185
|
+
*
|
|
186
|
+
* It is safe to pass to React 19's `use()`: the same promise is returned until its state
|
|
187
|
+
* changes, and it carries `status`/`value`/`reason` so `use()` doesn't suspend when it is
|
|
188
|
+
* already settled.
|
|
189
|
+
*/
|
|
190
|
+
readonly ready: Promise<void>;
|
|
191
|
+
/** The current underlying `WebSocket`, if any. An escape hatch; prefer the client's methods. */
|
|
192
|
+
readonly socket: WebSocket | null;
|
|
193
|
+
/** Subscribe to snapshot changes. Compatible with `useSyncExternalStore`. Returns an unsubscribe function. */
|
|
194
|
+
subscribe(listener: () => void): () => void;
|
|
195
|
+
/** The current {@link Snapshot}. Returns the same object until something changes. */
|
|
196
|
+
getSnapshot(): Snapshot<In>;
|
|
197
|
+
/**
|
|
198
|
+
* Listen to an event. Returns an unsubscribe function. Listener errors are re-thrown
|
|
199
|
+
* asynchronously, so one failing listener doesn't break the others.
|
|
200
|
+
*/
|
|
201
|
+
on<K extends keyof EventMap<In>>(type: K, listener: EventMap<In>[K]): () => void;
|
|
202
|
+
/**
|
|
203
|
+
* Send a message (encoded with `serialize`/`json` if configured). If the socket isn't
|
|
204
|
+
* open, the message is queued and sent on open (see the `queue` option), unless
|
|
205
|
+
* `options.volatile` is set.
|
|
206
|
+
* @returns `true` if it was sent immediately, `false` if it was queued or dropped
|
|
207
|
+
* (`volatile` while not open, or `maxBuffered` while open).
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* game.send({ type: 'input', dx, dy }, { volatile: true }); // never replayed after a reconnect
|
|
212
|
+
* game.send({ type: 'position', x, y }, { key: 'position' }); // only the newest is queued
|
|
213
|
+
* game.send({ type: 'leave' }, { priority: true }); // ahead of everything already queued
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
send(message: Out, options?: SendOptions): boolean;
|
|
217
|
+
/**
|
|
218
|
+
* Start connecting, and keep the connection open even when no hook is using the
|
|
219
|
+
* client, until `close()` is called. Does nothing if already connecting or open.
|
|
220
|
+
*/
|
|
221
|
+
connect(): void;
|
|
222
|
+
/**
|
|
223
|
+
* Close the connection and stop reconnecting. The status becomes `closed` and the
|
|
224
|
+
* send queue is cleared. Mounted hooks stay mounted; call `connect()` or `reconnect()`
|
|
225
|
+
* to start again. Closed while no hook holds it, the next hook to mount starts it again.
|
|
226
|
+
*
|
|
227
|
+
* Like `WebSocket.close()`, it throws for a code or reason the socket rejects (browsers take
|
|
228
|
+
* `1000` or `3000`–`4999`, and a reason up to 123 bytes), and then nothing has changed.
|
|
229
|
+
* @param code - Close code. @default 1000
|
|
230
|
+
*/
|
|
231
|
+
close(code?: number, reason?: string): void;
|
|
232
|
+
/**
|
|
233
|
+
* Drop the current socket (if any) and connect again immediately, resetting the
|
|
234
|
+
* backoff. Also restarts a `closed` client.
|
|
235
|
+
*/
|
|
236
|
+
reconnect(): void;
|
|
237
|
+
/**
|
|
238
|
+
* Move to a new connection without a gap, for example when the server asks its clients to
|
|
239
|
+
* before a deploy. At a random moment within `options.within` ms, it opens a new connection
|
|
240
|
+
* (to `options.url`, if given) and, once that one is open, closes the current one with code
|
|
241
|
+
* `1000`. Until then the current connection keeps working, so the status stays `open` and
|
|
242
|
+
* `send()` doesn't queue.
|
|
243
|
+
*
|
|
244
|
+
* `open` listeners run for the new connection, as after any reconnect, so handshakes and
|
|
245
|
+
* topic subscriptions are sent again. Requests the old connection hadn't answered fail as on
|
|
246
|
+
* a lost connection. If the new connection fails before opening, the current one stays. If
|
|
247
|
+
* the client isn't open when the moment comes, only the URL changes, for the next attempt.
|
|
248
|
+
* A later call replaces one that hasn't moved yet.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* // The server sends { type: 'reconnect', within: 30000 } before it shuts down.
|
|
253
|
+
* live.on('message', message => {
|
|
254
|
+
* if (message.type === 'reconnect') live.migrate({ within: message.within, url: message.url });
|
|
255
|
+
* });
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
migrate(options?: MigrateOptions): void;
|
|
259
|
+
/**
|
|
260
|
+
* Hold the connection open until the returned function is called. This is what the
|
|
261
|
+
* hooks use; call it yourself to integrate with non-React code.
|
|
262
|
+
* @returns A release function (safe to call more than once).
|
|
263
|
+
*/
|
|
264
|
+
acquire(): () => void;
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
export { WebSocketClient as t };
|
|
268
|
+
//# sourceMappingURL=types-BEecFqIB.d.cts.map
|