@usions/sdk 2.27.0 → 2.27.2

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.
@@ -68,7 +68,7 @@ export function isTrustedMessageSource(event) {
68
68
  * Core Usion object with init, _post, _request
69
69
  */
70
70
  export const core = {
71
- version: '2.27.0', // synced from package.json by scripts/sync-version.mjs
71
+ version: '2.27.2', // synced from package.json by scripts/sync-version.mjs
72
72
  config: {},
73
73
  _initialized: false,
74
74
  _initCallback: null,
@@ -15,6 +15,8 @@ export function applyGameResilience(game, Usion) {
15
15
  backpressureBytes: 8192,
16
16
  lastInboundAt: 0,
17
17
  lastProbeAt: 0,
18
+ lastWatchdogAt: 0,
19
+ livenessGraceUntil: 0,
18
20
  lastCloseCode: /** @type {number|null} */ (null),
19
21
  declaredDead: false,
20
22
  watchdogTimer: /** @type {*} */ (null),
@@ -102,8 +104,25 @@ export function applyGameResilience(game, Usion) {
102
104
  var s = game.directSocket;
103
105
  if (!game.directMode || !s || s.readyState !== 1) { stopWatchdog(); return; }
104
106
  if (res.liveness) {
105
- var age = Date.now() - res.lastInboundAt;
106
- if (age >= res.liveness.deadAfterMs) {
107
+ var now = Date.now();
108
+ var stalled = res.lastWatchdogAt && now - res.lastWatchdogAt > 2000;
109
+ res.lastWatchdogAt = now;
110
+ // A backgrounded WebView and a blocked main thread cannot process socket
111
+ // messages or this interval. On resume, an overdue watchdog used to run
112
+ // before the queued pong/snapshot and close a healthy socket. Pause while
113
+ // hidden, and give any delayed event loop one fresh liveness window.
114
+ if (typeof document !== 'undefined' && document.visibilityState === 'hidden') {
115
+ res.livenessGraceUntil = now + res.liveness.deadAfterMs;
116
+ return;
117
+ }
118
+ if (stalled) {
119
+ res.livenessGraceUntil = now + res.liveness.deadAfterMs;
120
+ sendProbe();
121
+ evaluate();
122
+ return;
123
+ }
124
+ var age = now - res.lastInboundAt;
125
+ if (age >= res.liveness.deadAfterMs && now >= res.livenessGraceUntil) {
107
126
  // Silent-dead: declare + close; the onclose path then reconnects.
108
127
  res.declaredDead = true;
109
128
  stopWatchdog();
@@ -114,7 +133,7 @@ export function applyGameResilience(game, Usion) {
114
133
  return;
115
134
  }
116
135
  if (age >= res.liveness.probeAfterMs
117
- && Date.now() - res.lastProbeAt >= res.liveness.probeAfterMs) sendProbe();
136
+ && now - res.lastProbeAt >= res.liveness.probeAfterMs) sendProbe();
118
137
  }
119
138
  evaluate();
120
139
  }
@@ -139,6 +158,8 @@ export function applyGameResilience(game, Usion) {
139
158
  res.lastCloseCode = null;
140
159
  res.lastInboundAt = Date.now();
141
160
  res.lastProbeAt = 0;
161
+ res.lastWatchdogAt = Date.now();
162
+ res.livenessGraceUntil = 0;
142
163
  stopWatchdog();
143
164
  res.watchdogTimer = setInterval(watchdogTick, 1000);
144
165
  addVisibility();
@@ -222,8 +243,13 @@ export function applyGameResilience(game, Usion) {
222
243
  if (typeof document === 'undefined' || !document.addEventListener || res.visHandler) return;
223
244
  res.visHandler = function () {
224
245
  if (document.visibilityState !== 'visible' || !game.directMode || !game._joined) return;
225
- sendProbe();
226
246
  var now = Date.now();
247
+ // Start a new health window before any overdue interval can judge the
248
+ // socket using wall time accumulated while the app was backgrounded.
249
+ if (res.liveness) res.livenessGraceUntil = now + res.liveness.deadAfterMs;
250
+ res.lastWatchdogAt = now;
251
+ res.lastProbeAt = 0;
252
+ sendProbe();
227
253
  if (now - res.lastVisResyncAt >= 2000) {
228
254
  res.lastVisResyncAt = now;
229
255
  try { game.requestSync(game._lastSequence || 0); } catch (e) { /* non-fatal */ }