@fluxstack/live-client 0.5.0 → 0.6.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.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/connection.ts
2
- var LiveConnection = class {
2
+ var LiveConnection = class _LiveConnection {
3
3
  ws = null;
4
4
  options;
5
5
  reconnectAttempts = 0;
@@ -161,18 +161,34 @@ var LiveConnection = class {
161
161
  this.setState({ error: "Max reconnection attempts reached" });
162
162
  }
163
163
  }
164
+ consecutiveHeartbeatFailures = 0;
165
+ static MAX_HEARTBEAT_FAILURES = 3;
164
166
  startHeartbeat() {
165
167
  this.stopHeartbeat();
168
+ this.consecutiveHeartbeatFailures = 0;
166
169
  this.heartbeatInterval = setInterval(() => {
167
170
  if (this.ws?.readyState === WebSocket.OPEN) {
171
+ let failed = false;
168
172
  for (const componentId of this.componentCallbacks.keys()) {
169
173
  this.sendMessage({
170
174
  type: "COMPONENT_PING",
171
175
  componentId,
172
176
  timestamp: Date.now()
173
177
  }).catch(() => {
178
+ failed = true;
174
179
  });
175
180
  }
181
+ if (failed) {
182
+ this.consecutiveHeartbeatFailures++;
183
+ this.log(`Heartbeat failed (${this.consecutiveHeartbeatFailures}/${_LiveConnection.MAX_HEARTBEAT_FAILURES})`);
184
+ if (this.consecutiveHeartbeatFailures >= _LiveConnection.MAX_HEARTBEAT_FAILURES) {
185
+ this.log("Too many heartbeat failures, reconnecting...");
186
+ this.setState({ error: "Heartbeat failed" });
187
+ this.reconnect();
188
+ }
189
+ } else {
190
+ this.consecutiveHeartbeatFailures = 0;
191
+ }
176
192
  }
177
193
  }, this.options.heartbeatInterval);
178
194
  }
@@ -356,8 +372,15 @@ var LiveConnection = class {
356
372
  { type: "AUTH", payload: credentials },
357
373
  5e3
358
374
  );
359
- const success = response.authenticated || false;
360
- this.setState({ authenticated: success });
375
+ const payload = response.payload;
376
+ const success = payload?.authenticated || false;
377
+ this.setState({
378
+ authenticated: success,
379
+ auth: {
380
+ authenticated: success,
381
+ session: success ? payload?.session || null : null
382
+ }
383
+ });
361
384
  return success;
362
385
  } catch {
363
386
  return false;
@@ -1379,7 +1402,10 @@ function persistState(enabled, name, signedState, room, userId) {
1379
1402
  userId,
1380
1403
  lastUpdate: Date.now()
1381
1404
  }));
1382
- } catch {
1405
+ } catch (e) {
1406
+ if (typeof console !== "undefined") {
1407
+ console.warn(`[fluxstack] Failed to persist state for '${name}':`, e instanceof Error ? e.message : e);
1408
+ }
1383
1409
  }
1384
1410
  }
1385
1411
  function getPersistedState(enabled, name) {