@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.
@@ -2453,11 +2453,14 @@ var AwarenessError = class extends Error {
2453
2453
  this.code = 1001;
2454
2454
  }
2455
2455
  };
2456
- var AbracadabraBaseProvider = class extends EventEmitter {
2456
+ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter {
2457
2457
  /** Current WebSocket connection status. */
2458
2458
  get connectionStatus() {
2459
2459
  return this.configuration.websocketProvider.status;
2460
2460
  }
2461
+ static {
2462
+ this.MAX_RESUBSCRIBE_ATTEMPTS = 6;
2463
+ }
2461
2464
  get rpc() {
2462
2465
  if (!this._rpc) this._rpc = new RpcClient(this);
2463
2466
  return this._rpc;
@@ -2509,6 +2512,8 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2509
2512
  this.authorizedScope = void 0;
2510
2513
  this.manageSocket = false;
2511
2514
  this._isAttached = false;
2515
+ this._resubscribeAttempts = 0;
2516
+ this._resubscribeTimer = null;
2512
2517
  this.intervals = { forceSync: null };
2513
2518
  this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
2514
2519
  this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
@@ -2753,6 +2758,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2753
2758
  this.isAuthenticated = false;
2754
2759
  this.synced = false;
2755
2760
  this._hasEverAuthenticated = false;
2761
+ this.clearResubscribe();
2756
2762
  if (this.awareness) removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
2757
2763
  }
2758
2764
  destroy() {
@@ -2787,6 +2793,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2787
2793
  this.configuration.websocketProvider.off("rateLimited", this.forwardRateLimited);
2788
2794
  this.configuration.websocketProvider.detach(this);
2789
2795
  this._isAttached = false;
2796
+ this.clearResubscribe();
2790
2797
  }
2791
2798
  attach() {
2792
2799
  if (this._isAttached) return;
@@ -2812,11 +2819,47 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2812
2819
  if (this.manageSocket && !this._hasEverAuthenticated) try {
2813
2820
  this.configuration.websocketProvider.disconnect();
2814
2821
  } catch {}
2822
+ if (!this.manageSocket && !this.isSynced && this._isAttached && this._resubscribeAttempts < AbracadabraBaseProvider.MAX_RESUBSCRIBE_ATTEMPTS && AbracadabraBaseProvider.isTransientSubscribeDenial(reason)) this.scheduleResubscribe();
2823
+ }
2824
+ /**
2825
+ * Whether a permission-denied reason reflects a *transient* server condition
2826
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
2827
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
2828
+ */
2829
+ static isTransientSubscribeDenial(reason) {
2830
+ const r = reason.toLowerCase();
2831
+ return r.includes("not found") || r.includes("not subscribed");
2832
+ }
2833
+ /**
2834
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
2835
+ * timer is in flight at a time; each denial that slips past the guard above
2836
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
2837
+ */
2838
+ scheduleResubscribe() {
2839
+ if (this._resubscribeTimer) return;
2840
+ const attempt = this._resubscribeAttempts++;
2841
+ const delay = Math.min(4e3, 250 * 2 ** attempt);
2842
+ this._resubscribeTimer = setTimeout(() => {
2843
+ this._resubscribeTimer = null;
2844
+ if (!this._isAttached || this.isSynced || this.connectionStatus !== WebSocketStatus.Connected) return;
2845
+ this.sendToken().then(() => {
2846
+ if (this._isAttached && !this.isAuthenticated) this.startSync();
2847
+ }).catch(() => {});
2848
+ }, delay);
2849
+ }
2850
+ /** Cancel any pending resubscribe and reset the attempt budget. */
2851
+ clearResubscribe() {
2852
+ if (this._resubscribeTimer) {
2853
+ clearTimeout(this._resubscribeTimer);
2854
+ this._resubscribeTimer = null;
2855
+ }
2856
+ this._resubscribeAttempts = 0;
2815
2857
  }
2816
2858
  authenticatedHandler(scope) {
2817
2859
  this.isAuthenticated = true;
2818
2860
  this._hasEverAuthenticated = true;
2819
2861
  this.authorizedScope = scope;
2862
+ this.clearResubscribe();
2820
2863
  this.emit("authenticated", { scope });
2821
2864
  }
2822
2865
  setAwarenessField(key, value) {