react-websocket 4.19.1 → 4.21.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/core.d.cts CHANGED
@@ -28,7 +28,10 @@ type Status = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
28
28
  interface Snapshot<In> {
29
29
  /** Connection state. */
30
30
  readonly status: Status;
31
- /** Most recent message (after `parse`/`json` and `schema`), kept across reconnects. */
31
+ /**
32
+ * Most recent message (after `parse`/`json` and `schema`), kept across reconnects and
33
+ * cleared by `close()`.
34
+ */
32
35
  readonly lastMessage: In | undefined;
33
36
  /**
34
37
  * Consecutive failed attempts since the last stable connection. A connection that closes
@@ -432,9 +435,10 @@ interface WebSocketClient<In = unknown, Out = unknown> {
432
435
  */
433
436
  connect(): void;
434
437
  /**
435
- * Close the connection and stop reconnecting. The status becomes `closed` and the
436
- * send queue is cleared. Mounted hooks stay mounted; call `connect()` or `reconnect()`
437
- * to start again. Closed while no hook holds it, the next hook to mount starts it again.
438
+ * Close the connection and stop reconnecting. The status becomes `closed`, and the send
439
+ * queue, a `migrate()` target and `lastMessage` are cleared, so call it on logout. Mounted
440
+ * hooks stay mounted; call `connect()` or `reconnect()` to start again. Closed while no hook
441
+ * holds it, the next hook to mount starts it again.
438
442
  *
439
443
  * Like `WebSocket.close()`, it throws for a code or reason the socket rejects (browsers take
440
444
  * `1000` or `3000`–`4999`, and a reason up to 123 bytes), and then nothing has changed.
@@ -443,7 +447,8 @@ interface WebSocketClient<In = unknown, Out = unknown> {
443
447
  close(code?: number, reason?: string): void;
444
448
  /**
445
449
  * Drop the current socket (if any) and connect again immediately, resetting the
446
- * backoff. Also restarts a `closed` client.
450
+ * backoff. Also restarts a `closed` client. The send queue and a `migrate()` target are
451
+ * kept: when another user signs in, call `close()` and then `connect()` instead.
447
452
  */
448
453
  reconnect(): void;
449
454
  /**
@@ -622,6 +627,12 @@ interface WebSocketFeeds<In> extends WebSocketTopics {
622
627
  on(type: 'message', listener: (message: In, event: MessageEvent) => void): () => void;
623
628
  /** The sequence number of the last message delivered on `topic`, while it's held. */
624
629
  position(topic: string): number | undefined;
630
+ /**
631
+ * Forget the position of `topic`, or of every topic, e.g. on logout or when another user
632
+ * signs in: the next subscription sends `after: undefined`, so the server sends that user's
633
+ * messages from the start, and none is dropped as a duplicate of the previous user's.
634
+ */
635
+ reset(topic?: string): void;
625
636
  }
626
637
  /**
627
638
  * Subscribe to topics whose messages carry a sequence number, and get each message once, in
@@ -1150,6 +1161,13 @@ interface WebSocketRuns<Req, T> {
1150
1161
  subscribe(listener: () => void): () => void;
1151
1162
  /** Forget a run, aborting it first if it's streaming. */
1152
1163
  delete(runId: string): void;
1164
+ /**
1165
+ * Forget every run, e.g. on logout or when another user signs in: their values, unsent start
1166
+ * requests and pending cancels, here and in `storage`. Sends nothing, so nothing of the
1167
+ * previous user's goes out on a later connection; `abort()` runs that should stop on the
1168
+ * server first, while connected.
1169
+ */
1170
+ clear(): void;
1153
1171
  /** Stop following runs, and release the connection. Remembered runs stay remembered. */
1154
1172
  dispose(): void;
1155
1173
  }
package/dist/core.d.ts CHANGED
@@ -28,7 +28,10 @@ type Status = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
28
28
  interface Snapshot<In> {
29
29
  /** Connection state. */
30
30
  readonly status: Status;
31
- /** Most recent message (after `parse`/`json` and `schema`), kept across reconnects. */
31
+ /**
32
+ * Most recent message (after `parse`/`json` and `schema`), kept across reconnects and
33
+ * cleared by `close()`.
34
+ */
32
35
  readonly lastMessage: In | undefined;
33
36
  /**
34
37
  * Consecutive failed attempts since the last stable connection. A connection that closes
@@ -432,9 +435,10 @@ interface WebSocketClient<In = unknown, Out = unknown> {
432
435
  */
433
436
  connect(): void;
434
437
  /**
435
- * Close the connection and stop reconnecting. The status becomes `closed` and the
436
- * send queue is cleared. Mounted hooks stay mounted; call `connect()` or `reconnect()`
437
- * to start again. Closed while no hook holds it, the next hook to mount starts it again.
438
+ * Close the connection and stop reconnecting. The status becomes `closed`, and the send
439
+ * queue, a `migrate()` target and `lastMessage` are cleared, so call it on logout. Mounted
440
+ * hooks stay mounted; call `connect()` or `reconnect()` to start again. Closed while no hook
441
+ * holds it, the next hook to mount starts it again.
438
442
  *
439
443
  * Like `WebSocket.close()`, it throws for a code or reason the socket rejects (browsers take
440
444
  * `1000` or `3000`–`4999`, and a reason up to 123 bytes), and then nothing has changed.
@@ -443,7 +447,8 @@ interface WebSocketClient<In = unknown, Out = unknown> {
443
447
  close(code?: number, reason?: string): void;
444
448
  /**
445
449
  * Drop the current socket (if any) and connect again immediately, resetting the
446
- * backoff. Also restarts a `closed` client.
450
+ * backoff. Also restarts a `closed` client. The send queue and a `migrate()` target are
451
+ * kept: when another user signs in, call `close()` and then `connect()` instead.
447
452
  */
448
453
  reconnect(): void;
449
454
  /**
@@ -622,6 +627,12 @@ interface WebSocketFeeds<In> extends WebSocketTopics {
622
627
  on(type: 'message', listener: (message: In, event: MessageEvent) => void): () => void;
623
628
  /** The sequence number of the last message delivered on `topic`, while it's held. */
624
629
  position(topic: string): number | undefined;
630
+ /**
631
+ * Forget the position of `topic`, or of every topic, e.g. on logout or when another user
632
+ * signs in: the next subscription sends `after: undefined`, so the server sends that user's
633
+ * messages from the start, and none is dropped as a duplicate of the previous user's.
634
+ */
635
+ reset(topic?: string): void;
625
636
  }
626
637
  /**
627
638
  * Subscribe to topics whose messages carry a sequence number, and get each message once, in
@@ -1150,6 +1161,13 @@ interface WebSocketRuns<Req, T> {
1150
1161
  subscribe(listener: () => void): () => void;
1151
1162
  /** Forget a run, aborting it first if it's streaming. */
1152
1163
  delete(runId: string): void;
1164
+ /**
1165
+ * Forget every run, e.g. on logout or when another user signs in: their values, unsent start
1166
+ * requests and pending cancels, here and in `storage`. Sends nothing, so nothing of the
1167
+ * previous user's goes out on a later connection; `abort()` runs that should stop on the
1168
+ * server first, while connected.
1169
+ */
1170
+ clear(): void;
1153
1171
  /** Stop following runs, and release the connection. Remembered runs stay remembered. */
1154
1172
  dispose(): void;
1155
1173
  }
package/dist/core.js CHANGED
@@ -574,7 +574,8 @@ function createClient(url, options = {}) {
574
574
  unheldStop = refs === 0;
575
575
  set({
576
576
  status: "closed",
577
- attempt: 0
577
+ attempt: 0,
578
+ lastMessage: void 0
578
579
  });
579
580
  rejectReady(/* @__PURE__ */ new Error("WebSocket was closed"));
580
581
  }
@@ -849,6 +850,15 @@ function createWebSocketFeeds(client, options) {
849
850
  };
850
851
  },
851
852
  position: (topic) => last.get(topic),
853
+ reset(topic) {
854
+ if (topic === void 0) {
855
+ last.clear();
856
+ resuming.clear();
857
+ } else {
858
+ last.delete(topic);
859
+ resuming.delete(topic);
860
+ }
861
+ },
852
862
  dispose() {
853
863
  topics.dispose();
854
864
  for (const off of offs) off();
@@ -1437,6 +1447,7 @@ function createWebSocketRuns(client, options) {
1437
1447
  const starting = /* @__PURE__ */ new Map();
1438
1448
  const policies = /* @__PURE__ */ new Map();
1439
1449
  const releases = /* @__PURE__ */ new Map();
1450
+ const cancels = /* @__PURE__ */ new Set();
1440
1451
  const listeners = /* @__PURE__ */ new Set();
1441
1452
  let list = [];
1442
1453
  const feeds = createWebSocketFeeds(client, {
@@ -1500,11 +1511,18 @@ function createWebSocketRuns(client, options) {
1500
1511
  if (!message) return;
1501
1512
  if (client.socket?.readyState === 1) return void client.send(message(runId));
1502
1513
  const release = client.acquire();
1514
+ const drop = () => {
1515
+ cancels.delete(drop);
1516
+ off();
1517
+ release();
1518
+ };
1503
1519
  const off = client.on("open", () => {
1520
+ cancels.delete(drop);
1504
1521
  off();
1505
1522
  client.send(message(runId));
1506
1523
  release();
1507
1524
  });
1525
+ cancels.add(drop);
1508
1526
  }
1509
1527
  function abort(runId) {
1510
1528
  if (runs.get(runId)?.status !== "streaming") return;
@@ -1593,6 +1611,21 @@ function createWebSocketRuns(client, options) {
1593
1611
  for (const listener of [...listeners]) listener();
1594
1612
  }
1595
1613
  },
1614
+ clear() {
1615
+ for (const release of releases.values()) release();
1616
+ releases.clear();
1617
+ for (const drop of [...cancels]) drop();
1618
+ stale.length = 0;
1619
+ starting.clear();
1620
+ policies.clear();
1621
+ try {
1622
+ storage?.removeItem(key);
1623
+ } catch {}
1624
+ if (!runs.size) return;
1625
+ runs.clear();
1626
+ list = [];
1627
+ for (const listener of [...listeners]) listener();
1628
+ },
1596
1629
  dispose() {
1597
1630
  for (const release of releases.values()) release();
1598
1631
  releases.clear();