@rocicorp/zero 0.25.10 → 0.25.11-canary.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.
@@ -36,7 +36,7 @@ import { send } from "../util/socket.js";
36
36
  import { ActiveClientsManager } from "./active-clients-manager.js";
37
37
  import { Internal, ClientClosed, InvalidMessage, CleanClose, AbruptClose, ConnectTimeout, UnexpectedBaseCookie, NoSocketOrigin, Hidden, PullTimeout, PingTimeout } from "./client-error-kind-enum.js";
38
38
  import { ConnectionManager, throwIfConnectionError } from "./connection-manager.js";
39
- import { Connected, Closed, Disconnected, Connecting, Error as Error$1, NeedsAuth } from "./connection-status-enum.js";
39
+ import { Connected, Connecting, Closed, Disconnected, Error as Error$1, NeedsAuth } from "./connection-status-enum.js";
40
40
  import { ConnectionImpl } from "./connection.js";
41
41
  import { ZeroContext } from "./context.js";
42
42
  import { makeCRUDMutateBatch, addTableCRUDProperties } from "./crud.js";
@@ -58,6 +58,7 @@ import { getServer } from "./server-option.js";
58
58
  import { version } from "./version.js";
59
59
  import { PokeHandler } from "./zero-poke-handler.js";
60
60
  import { ZeroRep, fromReplicacheAuthToken, toReplicacheAuthToken } from "./zero-rep.js";
61
+ import { AbortError } from "../../../shared/src/abort-error.js";
61
62
  const RUN_LOOP_INTERVAL_MS = 5e3;
62
63
  const DEFAULT_PING_TIMEOUT_MS = 5e3;
63
64
  const PULL_TIMEOUT_MS = 5e3;
@@ -147,6 +148,7 @@ class Zero {
147
148
  #connectErrorCount = 0;
148
149
  #abortPingTimeout = () => {
149
150
  };
151
+ #forceEnableRefresh = false;
150
152
  /**
151
153
  * The timeout in milliseconds for ping operations. Controls both:
152
154
  * - How long to wait in idle before sending a ping
@@ -334,6 +336,7 @@ class Zero {
334
336
  enableMutationRecovery: false,
335
337
  enablePullAndPushInOpen: false,
336
338
  // Zero calls push in its connection management code
339
+ enableRefresh: () => this.#enableRefresh(),
337
340
  onClientsDeleted: (deletedClients) => this.#deleteClientsManager.onClientsDeleted(deletedClients),
338
341
  zero: new ZeroRep(
339
342
  this.#zeroContext,
@@ -472,6 +475,9 @@ class Zero {
472
475
  void this.#runLoop();
473
476
  this.#expose();
474
477
  }
478
+ #enableRefresh() {
479
+ return this.#forceEnableRefresh || !this.#connectionManager.is(Connected) && !this.#connectionManager.is(Connecting);
480
+ }
475
481
  #expose() {
476
482
  const g = globalThis;
477
483
  if (g.__zero === void 0) {
@@ -1422,6 +1428,7 @@ ${error.errorBody.message}`, error);
1422
1428
  ex
1423
1429
  );
1424
1430
  const transition = getErrorConnectionTransition(ex);
1431
+ let sleepMs = void 0;
1425
1432
  switch (transition.status) {
1426
1433
  case NO_STATUS_TRANSITION: {
1427
1434
  const backoffParams = getBackoffParams(transition.reason);
@@ -1440,7 +1447,7 @@ ${error.errorBody.message}`, error);
1440
1447
  "ms before reconnecting due to error, state:",
1441
1448
  this.#connectionManager.state
1442
1449
  );
1443
- await sleep(backoffMs);
1450
+ sleepMs = backoffMs;
1444
1451
  break;
1445
1452
  }
1446
1453
  case NeedsAuth: {
@@ -1465,6 +1472,25 @@ ${error.errorBody.message}`, error);
1465
1472
  default:
1466
1473
  unreachable();
1467
1474
  }
1475
+ if (transition.status !== Closed) {
1476
+ try {
1477
+ this.#forceEnableRefresh = true;
1478
+ await this.#rep.runRefresh();
1479
+ } catch (ex2) {
1480
+ if (ex2 instanceof AbortError) {
1481
+ this.#lc.debug?.(
1482
+ `Refresh from storage did not complete before close.`
1483
+ );
1484
+ } else {
1485
+ this.#lc.error?.(`Error during refresh from storage`, ex2);
1486
+ }
1487
+ } finally {
1488
+ this.#forceEnableRefresh = false;
1489
+ }
1490
+ }
1491
+ if (sleepMs) {
1492
+ await sleep(sleepMs);
1493
+ }
1468
1494
  }
1469
1495
  }
1470
1496
  }