@fluixi/realtime 0.0.0-stage → 1.0.0-alpha.109

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.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -2
  3. package/dist/bus.cjs +127 -0
  4. package/dist/bus.d.ts +58 -0
  5. package/dist/bus.d.ts.map +1 -0
  6. package/dist/bus.js +118 -0
  7. package/dist/bus.mjs +8 -0
  8. package/dist/channel-protocol.cjs +42 -0
  9. package/dist/channel-protocol.d.ts +40 -0
  10. package/dist/channel-protocol.d.ts.map +1 -0
  11. package/dist/channel-protocol.js +24 -0
  12. package/dist/channel-protocol.mjs +8 -0
  13. package/dist/channel-server.cjs +291 -0
  14. package/dist/channel-server.d.ts +29 -0
  15. package/dist/channel-server.d.ts.map +1 -0
  16. package/dist/channel-server.js +122 -0
  17. package/dist/channel-server.mjs +9 -0
  18. package/dist/channels.cjs +426 -0
  19. package/dist/channels.d.ts +38 -0
  20. package/dist/channels.d.ts.map +1 -0
  21. package/dist/channels.js +82 -0
  22. package/dist/channels.mjs +9 -0
  23. package/dist/chunk-4CEAXWJN.mjs +31 -0
  24. package/dist/chunk-AWS4MP5D.mjs +107 -0
  25. package/dist/chunk-DNVSSCJA.mjs +22 -0
  26. package/dist/chunk-GAAZQJJ4.mjs +145 -0
  27. package/dist/chunk-MGON7IDW.mjs +77 -0
  28. package/dist/chunk-Q3NDGSX6.mjs +107 -0
  29. package/dist/chunk-UAZIXYV5.mjs +315 -0
  30. package/dist/chunk-X73D2DE2.mjs +19 -0
  31. package/dist/endpoint.cjs +165 -0
  32. package/dist/endpoint.d.ts +80 -0
  33. package/dist/endpoint.d.ts.map +1 -0
  34. package/dist/endpoint.js +168 -0
  35. package/dist/endpoint.mjs +8 -0
  36. package/dist/index.cjs +445 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +4 -0
  40. package/dist/index.mjs +29 -0
  41. package/dist/serializers.cjs +39 -0
  42. package/dist/serializers.d.ts +12 -0
  43. package/dist/serializers.d.ts.map +1 -0
  44. package/dist/serializers.js +16 -0
  45. package/dist/serializers.mjs +8 -0
  46. package/dist/server.cjs +385 -0
  47. package/dist/server.d.ts +5 -0
  48. package/dist/server.d.ts.map +1 -0
  49. package/dist/server.js +3 -0
  50. package/dist/server.mjs +20 -0
  51. package/dist/socket.cjs +362 -0
  52. package/dist/socket.d.ts +137 -0
  53. package/dist/socket.d.ts.map +1 -0
  54. package/dist/socket.js +351 -0
  55. package/dist/socket.mjs +16 -0
  56. package/dist/transport.cjs +51 -0
  57. package/dist/transport.d.ts +33 -0
  58. package/dist/transport.d.ts.map +1 -0
  59. package/dist/transport.js +36 -0
  60. package/dist/transport.mjs +8 -0
  61. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  62. package/package.json +65 -3
@@ -0,0 +1,362 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/socket.ts
21
+ var socket_exports = {};
22
+ __export(socket_exports, {
23
+ CONNECTION_STATUSES: () => CONNECTION_STATUSES,
24
+ REALTIME_ERROR_CODES: () => REALTIME_ERROR_CODES,
25
+ RealtimeError: () => RealtimeError,
26
+ backoff: () => backoff,
27
+ createWebSocket: () => createWebSocket
28
+ });
29
+ module.exports = __toCommonJS(socket_exports);
30
+ var import_reactive = require("@fluixi/reactive");
31
+
32
+ // src/serializers.ts
33
+ var raw = {
34
+ encode: (message) => message,
35
+ decode: (data) => data
36
+ };
37
+
38
+ // src/transport.ts
39
+ function globalTransport() {
40
+ const Native = globalThis.WebSocket;
41
+ return Native ? (url, protocols) => new Native(url, protocols) : void 0;
42
+ }
43
+ function byteSize(data) {
44
+ if (typeof data === "string") {
45
+ return data.length < 1024 ? new TextEncoder().encode(data).length : utf8Length(data);
46
+ }
47
+ if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data.byteLength;
48
+ if (typeof Blob !== "undefined" && data instanceof Blob) return data.size;
49
+ return data.byteLength ?? 0;
50
+ }
51
+ function utf8Length(text) {
52
+ let bytes = 0;
53
+ for (let i = 0; i < text.length; i++) {
54
+ const unit = text.charCodeAt(i);
55
+ if (unit < 128) bytes += 1;
56
+ else if (unit < 2048) bytes += 2;
57
+ else if (unit >= 55296 && unit <= 56319 && i + 1 < text.length) {
58
+ bytes += 4;
59
+ i++;
60
+ } else bytes += 3;
61
+ }
62
+ return bytes;
63
+ }
64
+
65
+ // src/socket.ts
66
+ var CONNECTION_STATUSES = ["idle", "connecting", "open", "reconnecting", "closing", "closed"];
67
+ var REALTIME_ERROR_CODES = [
68
+ "connect-failed",
69
+ "connect-timeout",
70
+ "connection-error",
71
+ "heartbeat-timeout",
72
+ "message-too-large",
73
+ "decode-failed",
74
+ "reconnect-exhausted"
75
+ ];
76
+ var RealtimeError = class extends Error {
77
+ constructor(code, message, options) {
78
+ super(message);
79
+ this.name = "RealtimeError";
80
+ this.code = code;
81
+ if (options && "cause" in options) this.cause = options.cause;
82
+ }
83
+ };
84
+ var NO_RECONNECT_CODES = /* @__PURE__ */ new Set([1e3, 1008]);
85
+ var CLOSE_TOO_LARGE = 1009;
86
+ var CLOSE_HEARTBEAT = 4e3;
87
+ var CLOSE_TIMEOUT = 4001;
88
+ var BUFFER_POLL_MS = 100;
89
+ function createWebSocket(url, options = {}) {
90
+ const serializer = options.serializer ?? raw;
91
+ const policy = reconnectPolicy(options.reconnect);
92
+ const connectTimeout = options.connectTimeout ?? 1e4;
93
+ const queueLimit = options.queueLimit ?? 100;
94
+ const maxMessageSize = options.maxMessageSize ?? 1024 * 1024;
95
+ const keepLatest = options.keepLatest ?? true;
96
+ const transport = options.transport ?? (typeof document === "undefined" ? void 0 : globalTransport());
97
+ const status = (0, import_reactive.signal)("idle");
98
+ const data = (0, import_reactive.signal)(void 0, { equals: false });
99
+ const error = (0, import_reactive.signal)(void 0);
100
+ const info = (0, import_reactive.signal)({ url: void 0, protocol: "", attempt: 0, closeCode: void 0, closeReason: void 0 });
101
+ const buffered = (0, import_reactive.signal)(0);
102
+ const subscribers = /* @__PURE__ */ new Set();
103
+ const listeners = {
104
+ open: /* @__PURE__ */ new Set(),
105
+ close: /* @__PURE__ */ new Set(),
106
+ error: /* @__PURE__ */ new Set(),
107
+ discard: /* @__PURE__ */ new Set()
108
+ };
109
+ const queue = [];
110
+ let socket;
111
+ let generation = 0;
112
+ let failures = 0;
113
+ let disposed = false;
114
+ let autoConnect = options.connect !== false && !!transport;
115
+ let connectTimer;
116
+ let retryTimer;
117
+ let beatTimer;
118
+ let silenceTimer;
119
+ let bufferTimer;
120
+ const emit = (event, ...args) => {
121
+ for (const listener of [...listeners[event]]) call(listener, args);
122
+ };
123
+ const fail = (code, message, cause) => {
124
+ const failure = new RealtimeError(code, message, cause === void 0 ? void 0 : { cause });
125
+ error.set(failure);
126
+ emit("error", failure);
127
+ };
128
+ const patchInfo = (patch) => info.set((current) => ({ ...current, ...patch }));
129
+ const clearTimers = () => {
130
+ clearTimeout(connectTimer);
131
+ clearInterval(beatTimer);
132
+ clearTimeout(silenceTimer);
133
+ clearTimeout(bufferTimer);
134
+ connectTimer = beatTimer = silenceTimer = bufferTimer = void 0;
135
+ };
136
+ const watchSilence = () => {
137
+ if (!options.heartbeat) return;
138
+ clearTimeout(silenceTimer);
139
+ const timeout = options.heartbeat.timeout ?? options.heartbeat.interval * 2;
140
+ silenceTimer = setTimeout(() => {
141
+ fail("heartbeat-timeout", `No frame for ${timeout} ms`);
142
+ drop(CLOSE_HEARTBEAT, "heartbeat timeout");
143
+ }, timeout);
144
+ };
145
+ const trackBuffer = () => {
146
+ clearTimeout(bufferTimer);
147
+ const amount = socket?.bufferedAmount ?? 0;
148
+ buffered.set(amount);
149
+ if (amount > 0) bufferTimer = setTimeout(trackBuffer, BUFFER_POLL_MS);
150
+ };
151
+ const transmit = (frame) => {
152
+ socket.send(frame);
153
+ trackBuffer();
154
+ };
155
+ const discardQueue = () => {
156
+ if (!queue.length) return;
157
+ const lost2 = queue.splice(0).map((entry) => entry.message);
158
+ emit("discard", lost2);
159
+ };
160
+ const lost = (close) => {
161
+ clearTimers();
162
+ socket = void 0;
163
+ patchInfo({ closeCode: close.code, closeReason: close.reason });
164
+ const retry = policy && !disposed && policy.when(close);
165
+ if (retry && failures < policy.attempts) {
166
+ const delay = backoff(policy, failures);
167
+ failures += 1;
168
+ patchInfo({ attempt: failures });
169
+ status.set("reconnecting");
170
+ emit("close", close);
171
+ retryTimer = setTimeout(open, delay);
172
+ return;
173
+ }
174
+ status.set("closed");
175
+ if (retry) fail("reconnect-exhausted", `Gave up after ${failures} attempts`);
176
+ discardQueue();
177
+ emit("close", close);
178
+ };
179
+ const drop = (code, reason) => {
180
+ const current = socket;
181
+ generation += 1;
182
+ if (current) {
183
+ current.onopen = current.onmessage = current.onerror = current.onclose = null;
184
+ try {
185
+ current.close(code, reason);
186
+ } catch {
187
+ }
188
+ }
189
+ lost({ code, reason, wasClean: false });
190
+ };
191
+ const open = async () => {
192
+ retryTimer = void 0;
193
+ const attempt = ++generation;
194
+ status.set("connecting");
195
+ let address;
196
+ try {
197
+ address = typeof url === "function" ? await url() : url;
198
+ } catch (cause) {
199
+ if (attempt !== generation) return;
200
+ fail("connect-failed", "Could not get the connection address", cause);
201
+ return lost({ code: 1006, reason: "", wasClean: false });
202
+ }
203
+ if (attempt !== generation) return;
204
+ patchInfo({ url: address });
205
+ let next;
206
+ try {
207
+ next = transport(address, options.protocols);
208
+ } catch (cause) {
209
+ fail("connect-failed", `Could not open ${address}`, cause);
210
+ status.set("closed");
211
+ discardQueue();
212
+ return;
213
+ }
214
+ socket = next;
215
+ next.binaryType = options.binaryType ?? "arraybuffer";
216
+ connectTimer = setTimeout(() => {
217
+ fail("connect-timeout", `Not open after ${connectTimeout} ms`);
218
+ drop(CLOSE_TIMEOUT, "connect timeout");
219
+ }, connectTimeout);
220
+ next.onopen = () => {
221
+ if (attempt !== generation) return;
222
+ clearTimeout(connectTimer);
223
+ failures = 0;
224
+ (0, import_reactive.batch)(() => {
225
+ error.set(void 0);
226
+ patchInfo({ protocol: next.protocol, attempt: 0, closeCode: void 0, closeReason: void 0 });
227
+ status.set("open");
228
+ });
229
+ for (const entry of queue.splice(0)) transmit(entry.frame);
230
+ if (options.heartbeat) {
231
+ const { interval, message } = options.heartbeat;
232
+ if (message !== void 0) beatTimer = setInterval(() => socket && transmit(serializer.encode(message)), interval);
233
+ watchSilence();
234
+ }
235
+ emit("open");
236
+ };
237
+ next.onmessage = (event) => {
238
+ if (attempt !== generation) return;
239
+ watchSilence();
240
+ const frame = event.data;
241
+ if (byteSize(frame) > maxMessageSize) {
242
+ fail("message-too-large", `Received a message over ${maxMessageSize} bytes`);
243
+ return drop(CLOSE_TOO_LARGE, "message too large");
244
+ }
245
+ let message;
246
+ try {
247
+ message = serializer.decode(frame);
248
+ } catch (cause) {
249
+ return fail("decode-failed", "Could not decode a message", cause);
250
+ }
251
+ if (keepLatest) data.set(() => message);
252
+ for (const subscriber of [...subscribers]) call(subscriber, [message]);
253
+ };
254
+ next.onerror = () => attempt === generation && fail("connection-error", "The connection failed");
255
+ next.onclose = (close) => {
256
+ if (attempt !== generation) return;
257
+ lost({ code: close.code, reason: close.reason, wasClean: close.wasClean });
258
+ };
259
+ };
260
+ const connection = {
261
+ status,
262
+ connected: () => status() === "open",
263
+ data,
264
+ error,
265
+ info,
266
+ buffered,
267
+ connect() {
268
+ autoConnect = false;
269
+ if (disposed || !transport) return;
270
+ const now = (0, import_reactive.untrack)(status);
271
+ if (now === "open" || now === "connecting") return;
272
+ if (now === "closing") drop(1e3, "");
273
+ clearTimeout(retryTimer);
274
+ failures = 0;
275
+ void open();
276
+ },
277
+ send(message) {
278
+ const now = (0, import_reactive.untrack)(status);
279
+ if (disposed || now === "idle" && !autoConnect || now === "closing" || now === "closed") return "closed";
280
+ const frame = serializer.encode(message);
281
+ if (byteSize(frame) > maxMessageSize) return "too-large";
282
+ if (now === "open") {
283
+ transmit(frame);
284
+ return "sent";
285
+ }
286
+ if (queue.length >= queueLimit) return "queue-full";
287
+ queue.push({ message, frame });
288
+ return "queued";
289
+ },
290
+ subscribe(handler) {
291
+ subscribers.add(handler);
292
+ const stop = () => void subscribers.delete(handler);
293
+ if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(stop);
294
+ return stop;
295
+ },
296
+ on(event, handler) {
297
+ const set = listeners[event];
298
+ set.add(handler);
299
+ const stop = () => void set.delete(handler);
300
+ if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(stop);
301
+ return stop;
302
+ },
303
+ close(code = 1e3, reason = "") {
304
+ autoConnect = false;
305
+ clearTimeout(retryTimer);
306
+ retryTimer = void 0;
307
+ const current = socket;
308
+ generation += 1;
309
+ clearTimers();
310
+ if (!current) {
311
+ if ((0, import_reactive.untrack)(status) !== "idle") status.set("closed");
312
+ discardQueue();
313
+ return;
314
+ }
315
+ status.set("closing");
316
+ current.onopen = current.onmessage = current.onerror = null;
317
+ current.onclose = (close) => {
318
+ socket = void 0;
319
+ patchInfo({ closeCode: close.code, closeReason: close.reason });
320
+ status.set("closed");
321
+ emit("close", close);
322
+ };
323
+ discardQueue();
324
+ current.close(code, reason);
325
+ },
326
+ dispose() {
327
+ if (disposed) return;
328
+ connection.close();
329
+ disposed = true;
330
+ subscribers.clear();
331
+ for (const set of Object.values(listeners)) set.clear();
332
+ }
333
+ };
334
+ if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(() => connection.dispose());
335
+ if (autoConnect) queueMicrotask(() => autoConnect && connection.connect());
336
+ return connection;
337
+ }
338
+ function reconnectPolicy(option) {
339
+ if (option === false) return void 0;
340
+ const given = option === true || option === void 0 ? {} : option;
341
+ return {
342
+ attempts: given.attempts ?? Infinity,
343
+ delay: given.delay ?? 500,
344
+ maxDelay: given.maxDelay ?? 3e4,
345
+ factor: given.factor ?? 2,
346
+ jitter: given.jitter ?? true,
347
+ when: given.when ?? ((close) => !NO_RECONNECT_CODES.has(close.code))
348
+ };
349
+ }
350
+ function backoff(policy, failures) {
351
+ const full = Math.min(policy.maxDelay, policy.delay * policy.factor ** failures);
352
+ return policy.jitter ? full / 2 + Math.random() * (full / 2) : full;
353
+ }
354
+ function call(handler, args) {
355
+ try {
356
+ handler(...args);
357
+ } catch (failure) {
358
+ queueMicrotask(() => {
359
+ throw failure;
360
+ });
361
+ }
362
+ }
@@ -0,0 +1,137 @@
1
+ import { type Accessor } from '@fluixi/reactive';
2
+ import { type Serializer } from './serializers.js';
3
+ import { type CloseInfo, type RawData, type SendData, type WebSocketTransport } from './transport.js';
4
+ export declare const CONNECTION_STATUSES: readonly ["idle", "connecting", "open", "reconnecting", "closing", "closed"];
5
+ /**
6
+ * - `idle`: never connected, or on the server, where nothing connects.
7
+ * - `reconnecting`: waiting before the next attempt.
8
+ * - `closed`: closed on request, refused by `reconnect.when`, or out of attempts.
9
+ */
10
+ export type ConnectionStatus = (typeof CONNECTION_STATUSES)[number];
11
+ export declare const REALTIME_ERROR_CODES: readonly ["connect-failed", "connect-timeout", "connection-error", "heartbeat-timeout", "message-too-large", "decode-failed", "reconnect-exhausted"];
12
+ export type RealtimeErrorCode = (typeof REALTIME_ERROR_CODES)[number];
13
+ export declare class RealtimeError extends Error {
14
+ readonly code: RealtimeErrorCode;
15
+ /** What failed underneath, when something did. */
16
+ readonly cause?: unknown;
17
+ constructor(code: RealtimeErrorCode, message: string, options?: {
18
+ cause?: unknown;
19
+ });
20
+ }
21
+ /**
22
+ * What `send` did with a message. Only `sent` means the transport took it; `queued` means it
23
+ * waits for the connection to open.
24
+ */
25
+ export type SendResult = 'sent' | 'queued' | 'queue-full' | 'too-large' | 'closed';
26
+ export interface ReconnectPolicy {
27
+ /** Attempts after a lost connection before giving up. Default: Infinity */
28
+ attempts?: number;
29
+ /** First delay, in ms. Default: 500 */
30
+ delay?: number;
31
+ /** Longest delay, in ms. Default: 30000 */
32
+ maxDelay?: number;
33
+ /** Growth of the delay per failed attempt. Default: 2 */
34
+ factor?: number;
35
+ /** Spread each delay over its upper half, so clients cut off together do not return together. Default: true */
36
+ jitter?: boolean;
37
+ /**
38
+ * Whether a close calls for another attempt. Default: everything but 1000 (normal) and 1008
39
+ * (policy violation, what a server answers when the person may not connect).
40
+ */
41
+ when?: (close: CloseInfo) => boolean;
42
+ }
43
+ export interface Heartbeat<Out> {
44
+ /** How often `message` is sent while open, in ms. */
45
+ interval: number;
46
+ /** Sent every `interval`. Without it nothing is sent and only silence is watched. */
47
+ message?: Out;
48
+ /**
49
+ * Silence that counts as a dead connection, in ms: no frame for that long drops the socket and
50
+ * reconnects. The server must send something at least that often. Default: twice `interval`
51
+ */
52
+ timeout?: number;
53
+ }
54
+ export interface WebSocketOptions<In, Out> {
55
+ protocols?: string | string[];
56
+ /** Encoding of messages; frames pass as they are without one. */
57
+ serializer?: Serializer<In, Out>;
58
+ /**
59
+ * Connect once created, in a browser. The attempt starts when the code running now has
60
+ * finished, so a hydrating page still reads `idle`, as the server rendered it; messages sent
61
+ * before then are queued. Default: true
62
+ */
63
+ connect?: boolean;
64
+ /** Form of binary frames. Default: 'arraybuffer' */
65
+ binaryType?: 'arraybuffer' | 'blob';
66
+ /** Reconnection after a lost connection, or false for none. Default: on, with the defaults of {@link ReconnectPolicy} */
67
+ reconnect?: ReconnectPolicy | boolean;
68
+ /** Time an attempt has to open, in ms. Default: 10000 */
69
+ connectTimeout?: number;
70
+ heartbeat?: Heartbeat<Out>;
71
+ /** Messages kept while not open, sent in order once open; 0 refuses them. Default: 100 */
72
+ queueLimit?: number;
73
+ /** Largest message either way, in bytes; a larger incoming one closes with 1009. Default: 1 MiB */
74
+ maxMessageSize?: number;
75
+ /** Keep the last message in `data()`. Off for streams consumed through `subscribe` only. Default: true */
76
+ keepLatest?: boolean;
77
+ /** Opens the socket. Default: the runtime's `WebSocket`, in a browser only. */
78
+ transport?: WebSocketTransport;
79
+ }
80
+ export interface ConnectionInfo {
81
+ /** The address of the current or last attempt. */
82
+ url: string | undefined;
83
+ /** The subprotocol the server chose. */
84
+ protocol: string;
85
+ /** Failed attempts since the connection was last open. */
86
+ attempt: number;
87
+ closeCode: number | undefined;
88
+ closeReason: string | undefined;
89
+ }
90
+ export interface ConnectionEvents<Out> {
91
+ open: () => void;
92
+ close: (close: CloseInfo) => void;
93
+ error: (error: RealtimeError) => void;
94
+ /** Queued messages that will never be sent: the connection closed for good before opening. */
95
+ discard: (messages: Out[]) => void;
96
+ }
97
+ export interface WebSocketConnection<In, Out> {
98
+ status: Accessor<ConnectionStatus>;
99
+ connected: Accessor<boolean>;
100
+ /** The last message received, when `keepLatest` is on. */
101
+ data: Accessor<In | undefined>;
102
+ /** The last error, cleared when the connection opens. */
103
+ error: Accessor<RealtimeError | undefined>;
104
+ info: Accessor<ConnectionInfo>;
105
+ /**
106
+ * Bytes the transport still holds after the last send. The browser fires no event as this
107
+ * drains, so it is read after each send and then polled until it reaches zero.
108
+ */
109
+ buffered: Accessor<number>;
110
+ /** Opens the connection; nothing happens while open or connecting, or on the server. */
111
+ connect(): void;
112
+ send(message: Out): SendResult;
113
+ /** Calls `handler` with every message. Inside a reactive owner, unsubscribes with it. */
114
+ subscribe(handler: (message: In) => void): () => void;
115
+ on<E extends keyof ConnectionEvents<Out>>(event: E, handler: ConnectionEvents<Out>[E]): () => void;
116
+ /** Closes without reconnecting; `connect()` opens again. */
117
+ close(code?: number, reason?: string): void;
118
+ /** Closes for good and drops every subscriber. Runs with the owner that created the connection. */
119
+ dispose(): void;
120
+ }
121
+ /**
122
+ * A WebSocket connection whose state is signals.
123
+ *
124
+ * Created inside a reactive owner (a component), it closes with that owner; created outside one
125
+ * (a module, an app-wide service), it lives until `dispose()`, and components consume it with
126
+ * `subscribe` and `on`, which end with theirs. On the server, without a `transport`, nothing
127
+ * connects; in the browser the first attempt waits for the running task, hydration included.
128
+ */
129
+ export declare function createWebSocket(url: string | (() => string | Promise<string>), options?: WebSocketOptions<RawData, SendData> & {
130
+ serializer?: undefined;
131
+ }): WebSocketConnection<RawData, SendData>;
132
+ export declare function createWebSocket<In, Out>(url: string | (() => string | Promise<string>), options: WebSocketOptions<In, Out> & {
133
+ serializer: Serializer<In, Out>;
134
+ }): WebSocketConnection<In, Out>;
135
+ /** The wait before attempt `failures + 1`. */
136
+ export declare function backoff(policy: Required<ReconnectPolicy>, failures: number): number;
137
+ //# sourceMappingURL=socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket.d.ts","sourceRoot":"","sources":["../src/socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,KAAK,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAO,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAA6B,KAAK,SAAS,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAsB,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAErJ,eAAO,MAAM,mBAAmB,8EAA+E,CAAC;AAChH;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpE,eAAO,MAAM,oBAAoB,sJAQvB,CAAC;AACX,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,oBAAoB,CAA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