@abraca/dabra 2.35.1 → 2.37.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.
@@ -2423,11 +2423,14 @@ var AwarenessError = class extends Error {
2423
2423
  this.code = 1001;
2424
2424
  }
2425
2425
  };
2426
- var AbracadabraBaseProvider = class extends EventEmitter {
2426
+ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter {
2427
2427
  /** Current WebSocket connection status. */
2428
2428
  get connectionStatus() {
2429
2429
  return this.configuration.websocketProvider.status;
2430
2430
  }
2431
+ static {
2432
+ this.MAX_RESUBSCRIBE_ATTEMPTS = 6;
2433
+ }
2431
2434
  get rpc() {
2432
2435
  if (!this._rpc) this._rpc = new RpcClient(this);
2433
2436
  return this._rpc;
@@ -2479,6 +2482,8 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2479
2482
  this.authorizedScope = void 0;
2480
2483
  this.manageSocket = false;
2481
2484
  this._isAttached = false;
2485
+ this._resubscribeAttempts = 0;
2486
+ this._resubscribeTimer = null;
2482
2487
  this.intervals = { forceSync: null };
2483
2488
  this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
2484
2489
  this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
@@ -2723,6 +2728,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2723
2728
  this.isAuthenticated = false;
2724
2729
  this.synced = false;
2725
2730
  this._hasEverAuthenticated = false;
2731
+ this.clearResubscribe();
2726
2732
  if (this.awareness) removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
2727
2733
  }
2728
2734
  destroy() {
@@ -2757,6 +2763,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2757
2763
  this.configuration.websocketProvider.off("rateLimited", this.forwardRateLimited);
2758
2764
  this.configuration.websocketProvider.detach(this);
2759
2765
  this._isAttached = false;
2766
+ this.clearResubscribe();
2760
2767
  }
2761
2768
  attach() {
2762
2769
  if (this._isAttached) return;
@@ -2782,11 +2789,47 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2782
2789
  if (this.manageSocket && !this._hasEverAuthenticated) try {
2783
2790
  this.configuration.websocketProvider.disconnect();
2784
2791
  } catch {}
2792
+ if (!this.manageSocket && !this.isSynced && this._isAttached && this._resubscribeAttempts < AbracadabraBaseProvider.MAX_RESUBSCRIBE_ATTEMPTS && AbracadabraBaseProvider.isTransientSubscribeDenial(reason)) this.scheduleResubscribe();
2793
+ }
2794
+ /**
2795
+ * Whether a permission-denied reason reflects a *transient* server condition
2796
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
2797
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
2798
+ */
2799
+ static isTransientSubscribeDenial(reason) {
2800
+ const r = reason.toLowerCase();
2801
+ return r.includes("not found") || r.includes("not subscribed");
2802
+ }
2803
+ /**
2804
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
2805
+ * timer is in flight at a time; each denial that slips past the guard above
2806
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
2807
+ */
2808
+ scheduleResubscribe() {
2809
+ if (this._resubscribeTimer) return;
2810
+ const attempt = this._resubscribeAttempts++;
2811
+ const delay = Math.min(4e3, 250 * 2 ** attempt);
2812
+ this._resubscribeTimer = setTimeout(() => {
2813
+ this._resubscribeTimer = null;
2814
+ if (!this._isAttached || this.isSynced || this.connectionStatus !== WebSocketStatus.Connected) return;
2815
+ this.sendToken().then(() => {
2816
+ if (this._isAttached && !this.isAuthenticated) this.startSync();
2817
+ }).catch(() => {});
2818
+ }, delay);
2819
+ }
2820
+ /** Cancel any pending resubscribe and reset the attempt budget. */
2821
+ clearResubscribe() {
2822
+ if (this._resubscribeTimer) {
2823
+ clearTimeout(this._resubscribeTimer);
2824
+ this._resubscribeTimer = null;
2825
+ }
2826
+ this._resubscribeAttempts = 0;
2785
2827
  }
2786
2828
  authenticatedHandler(scope) {
2787
2829
  this.isAuthenticated = true;
2788
2830
  this._hasEverAuthenticated = true;
2789
2831
  this.authorizedScope = scope;
2832
+ this.clearResubscribe();
2790
2833
  this.emit("authenticated", { scope });
2791
2834
  }
2792
2835
  setAwarenessField(key, value) {