@actuallab/rpc 16.0.50 → 16.1.1

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/index.cjs CHANGED
@@ -798,9 +798,9 @@ var RpcOutboundCall = class {
798
798
  /** Per-call connect/run timeouts enforced by the peer's maintenance loop
799
799
  * (R12). `undefined` means unbounded (the query default). */
800
800
  timeouts;
801
- /** `Date.now()` when this call was created (used for the connect timeout). */
802
- startedAt = Date.now();
803
- /** `Date.now()` when this call was last put on the wire, or 0 if it hasn't
801
+ /** `monotonicClock.nowMs()` when this call was created (used for the connect timeout). */
802
+ startedAt = import_core4.monotonicClock.nowMs();
803
+ /** `monotonicClock.nowMs()` when this call was last put on the wire, or 0 if it hasn't
804
804
  * been sent yet (used for the run timeout). */
805
805
  sentAt = 0;
806
806
  /**
@@ -1621,7 +1621,7 @@ var RpcStreamSender = class _RpcStreamSender {
1621
1621
  // Windowed-MIN ack round trip (ms, -1 until sampled): converges on propagation
1622
1622
  // RTT — unlike a mean/EMA it is not inflated by self-induced queuing.
1623
1623
  get minRttMs() {
1624
- const cutoffMs = Date.now() - RTT_WINDOW_MS;
1624
+ const cutoffMs = import_core5.awakeClock.nowMs() - RTT_WINDOW_MS;
1625
1625
  while (this._rttSamples.length > 0 && this._rttSamples[0].atMs < cutoffMs)
1626
1626
  this._rttSamples.shift();
1627
1627
  if (this._rttSamples.length === 0)
@@ -1742,7 +1742,7 @@ var RpcStreamSender = class _RpcStreamSender {
1742
1742
  this._nextIndex,
1743
1743
  item
1744
1744
  );
1745
- this._pendingSendTimes.push({ index: this._nextIndex, sentAtMs: Date.now() });
1745
+ this._pendingSendTimes.push({ index: this._nextIndex, sentAtMs: import_core5.awakeClock.nowMs() });
1746
1746
  if (this._pendingSendTimes.length > MAX_PENDING_SEND_TIMES)
1747
1747
  this._pendingSendTimes.splice(0, MAX_PENDING_SEND_TIMES >> 1);
1748
1748
  }
@@ -2021,7 +2021,7 @@ var RpcStreamSender = class _RpcStreamSender {
2021
2021
  }
2022
2022
  if (sentAtMs < 0)
2023
2023
  return;
2024
- const nowMs = Date.now();
2024
+ const nowMs = import_core5.awakeClock.nowMs();
2025
2025
  const cutoffMs = nowMs - RTT_WINDOW_MS;
2026
2026
  while (this._rttSamples.length > 0 && this._rttSamples[0].atMs < cutoffMs)
2027
2027
  this._rttSamples.shift();
@@ -2843,10 +2843,10 @@ var RpcPeer = class {
2843
2843
  _isConnected = false;
2844
2844
  _connectionState = 0 /* Disconnected */;
2845
2845
  _keepAliveTimer;
2846
- _keepAliveWatchdog;
2846
+ _cancelKeepAliveWatchdog;
2847
2847
  _maintainTimer;
2848
- /** Wall-clock time of the last inbound `$sys.KeepAlive`, or 0 if none yet
2849
- * for the current connection. Useful for diagnostics. */
2848
+ /** `monotonicClock` time of the last inbound `$sys.KeepAlive` or the last connect,
2849
+ * whichever is later; 0 if the peer has never been connected. */
2850
2850
  _lastKeepAliveAt = 0;
2851
2851
  /** When the current outage began; 0 until the peer has been connected at least once. */
2852
2852
  _disconnectedAt = 0;
@@ -2875,7 +2875,7 @@ var RpcPeer = class {
2875
2875
  * per outage) plus `Maintain` (RunTimeout, per connection). Calls without timeouts
2876
2876
  * (queries) never time out here. */
2877
2877
  _maintainOutboundCalls() {
2878
- const now = Date.now();
2878
+ const now = import_core7.monotonicClock.nowMs();
2879
2879
  const canRunTimeout = this._isConnected && (this._lastKeepAliveAt === 0 || now - this._lastKeepAliveAt <= this.keepAliveTimeoutMs);
2880
2880
  for (const call of [...this.outboundCalls.values()]) {
2881
2881
  const timeouts = call.timeouts;
@@ -2942,11 +2942,12 @@ var RpcPeer = class {
2942
2942
  if (value === 3 /* Connected */) {
2943
2943
  this._isConnected = true;
2944
2944
  this._disconnectedAt = 0;
2945
+ this._lastKeepAliveAt = import_core7.monotonicClock.nowMs();
2945
2946
  this._armKeepAliveWatchdog();
2946
2947
  } else {
2947
2948
  if (previousState === 3 /* Connected */) {
2948
2949
  this._isConnected = false;
2949
- this._disconnectedAt = Date.now();
2950
+ this._disconnectedAt = import_core7.monotonicClock.nowMs();
2950
2951
  }
2951
2952
  this._disarmKeepAliveWatchdog();
2952
2953
  }
@@ -2973,7 +2974,7 @@ var RpcPeer = class {
2973
2974
  if (this._connection !== conn) return;
2974
2975
  this._connection = void 0;
2975
2976
  if (this._isConnected)
2976
- this._disconnectedAt = Date.now();
2977
+ this._disconnectedAt = import_core7.monotonicClock.nowMs();
2977
2978
  this._isConnected = false;
2978
2979
  if (ev.reason)
2979
2980
  infoLog?.log(`'${this.ref}': Disconnected: ${ev.reason} (code=${ev.code})`);
@@ -3014,14 +3015,14 @@ var RpcPeer = class {
3014
3015
  outboundCall.timeouts = options?.timeouts;
3015
3016
  this.outboundCalls.register(outboundCall);
3016
3017
  if (this._isConnected) {
3017
- outboundCall.sentAt = Date.now();
3018
+ outboundCall.sentAt = import_core7.monotonicClock.nowMs();
3018
3019
  this._sendWireData(outboundCall.serializedWireData);
3019
3020
  } else {
3020
3021
  const stateHandler = (state) => {
3021
3022
  if (state !== 3 /* Connected */) return;
3022
3023
  this.connectionStateChanged.remove(stateHandler);
3023
3024
  if (outboundCall.result.isCompleted) return;
3024
- outboundCall.sentAt = Date.now();
3025
+ outboundCall.sentAt = import_core7.monotonicClock.nowMs();
3025
3026
  this._sendWireData(outboundCall.serializedWireData);
3026
3027
  };
3027
3028
  this.connectionStateChanged.add(stateHandler);
@@ -3247,10 +3248,9 @@ var RpcPeer = class {
3247
3248
  }
3248
3249
  }
3249
3250
  }, this.keepAlivePeriodMs);
3250
- if (this._keepAliveWatchdog !== void 0)
3251
- clearTimeout(this._keepAliveWatchdog);
3252
- this._keepAliveWatchdog = setTimeout(() => {
3253
- this._keepAliveWatchdog = void 0;
3251
+ this._cancelKeepAliveWatchdog?.();
3252
+ this._cancelKeepAliveWatchdog = (0, import_core7.startTimer)(() => {
3253
+ this._cancelKeepAliveWatchdog = void 0;
3254
3254
  if (this._connection === void 0) return;
3255
3255
  warnLog8?.log(
3256
3256
  `'${this.ref}': no keep-alive for ${this.keepAliveTimeoutMs}ms \u2014 force-closing connection`
@@ -3265,16 +3265,14 @@ var RpcPeer = class {
3265
3265
  clearInterval(this._keepAliveTimer);
3266
3266
  this._keepAliveTimer = void 0;
3267
3267
  }
3268
- if (this._keepAliveWatchdog !== void 0) {
3269
- clearTimeout(this._keepAliveWatchdog);
3270
- this._keepAliveWatchdog = void 0;
3271
- }
3268
+ this._cancelKeepAliveWatchdog?.();
3269
+ this._cancelKeepAliveWatchdog = void 0;
3272
3270
  }
3273
3271
  /** Called by `RpcSystemCallHandler` when a `$sys.KeepAlive` message
3274
3272
  * arrives — records the timestamp and re-arms the silence watchdog. */
3275
3273
  notifyKeepAliveReceived() {
3276
- this._lastKeepAliveAt = Date.now();
3277
- if (this._keepAliveWatchdog !== void 0)
3274
+ this._lastKeepAliveAt = import_core7.monotonicClock.nowMs();
3275
+ if (this._cancelKeepAliveWatchdog !== void 0)
3278
3276
  this._armKeepAliveWatchdog();
3279
3277
  }
3280
3278
  };
@@ -3546,7 +3544,7 @@ var RpcClientPeer = class extends RpcPeer {
3546
3544
  );
3547
3545
  const sentCalls = this.outboundCalls.getSentCalls();
3548
3546
  this._setConnectionState(3 /* Connected */);
3549
- connectedAt = Date.now();
3547
+ connectedAt = import_core7.monotonicClock.nowMs();
3550
3548
  if (peerChangeKind !== 1 /* ChangedToVeryFirst */)
3551
3549
  await this._reconnect(
3552
3550
  sentCalls,
@@ -3565,7 +3563,7 @@ var RpcClientPeer = class extends RpcPeer {
3565
3563
  this._disposed = true;
3566
3564
  break;
3567
3565
  }
3568
- if (connectedAt !== 0 && Date.now() - connectedAt >= this.prematureDisconnectTimeoutMs)
3566
+ if (connectedAt !== 0 && import_core7.monotonicClock.nowMs() - connectedAt >= this.prematureDisconnectTimeoutMs)
3569
3567
  this._tryIndex = 0;
3570
3568
  this._tryIndex++;
3571
3569
  }
@@ -3625,7 +3623,7 @@ var RpcClientPeer = class extends RpcPeer {
3625
3623
  continue;
3626
3624
  }
3627
3625
  eligible.push(call);
3628
- call.sentAt = Date.now();
3626
+ call.sentAt = import_core7.monotonicClock.nowMs();
3629
3627
  }
3630
3628
  let unknownIds = null;
3631
3629
  let isReconciled = false;
@@ -3655,7 +3653,7 @@ var RpcClientPeer = class extends RpcPeer {
3655
3653
  this.outboundCalls.remove(call.callId);
3656
3654
  continue;
3657
3655
  }
3658
- call.sentAt = Date.now();
3656
+ call.sentAt = import_core7.monotonicClock.nowMs();
3659
3657
  this._sendWireData(call.serializedWireData);
3660
3658
  resentSinceYield++;
3661
3659
  if (resentSinceYield >= YIELD_BATCH) {
@@ -4879,14 +4877,14 @@ var RpcPeerStateMonitor = class {
4879
4877
  _onConnectionStateChanged = (state) => {
4880
4878
  if (state === 3 /* Connected */) {
4881
4879
  this._rawConnected = true;
4882
- this._connectedAt = Date.now();
4880
+ this._connectedAt = import_core10.monotonicClock.nowMs();
4883
4881
  this._lastError = void 0;
4884
4882
  } else {
4885
4883
  const wasConnected = this._rawConnected;
4886
4884
  this._rawConnected = false;
4887
4885
  this._lastError = new Error("Connection closed");
4888
4886
  if (wasConnected) {
4889
- this._disconnectedAt = Date.now();
4887
+ this._disconnectedAt = import_core10.monotonicClock.nowMs();
4890
4888
  }
4891
4889
  }
4892
4890
  this._recompute();
@@ -4901,13 +4899,13 @@ var RpcPeerStateMonitor = class {
4901
4899
  this.peer = peer;
4902
4900
  this._rawConnected = peer.isConnected;
4903
4901
  if (this._rawConnected) {
4904
- this._connectedAt = Date.now();
4902
+ this._connectedAt = import_core10.monotonicClock.nowMs();
4905
4903
  this._state = {
4906
4904
  kind: 3 /* JustConnected */,
4907
4905
  reconnectsIn: 0
4908
4906
  };
4909
4907
  } else {
4910
- this._disconnectedAt = Date.now();
4908
+ this._disconnectedAt = import_core10.monotonicClock.nowMs();
4911
4909
  this._state = {
4912
4910
  kind: 1 /* JustDisconnected */,
4913
4911
  reconnectsIn: 0
@@ -4943,7 +4941,7 @@ var RpcPeerStateMonitor = class {
4943
4941
  clearTimeout(this._timer);
4944
4942
  this._timer = void 0;
4945
4943
  }
4946
- const now = Date.now();
4944
+ const now = import_core10.monotonicClock.nowMs();
4947
4945
  let newState;
4948
4946
  if (this._rawConnected) {
4949
4947
  const connectedFor = now - this._connectedAt;