@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.
@@ -39,7 +39,7 @@ var FluxstackLive = (() => {
39
39
  });
40
40
 
41
41
  // src/connection.ts
42
- var LiveConnection = class {
42
+ var _LiveConnection = class _LiveConnection {
43
43
  constructor(options = {}) {
44
44
  __publicField(this, "ws", null);
45
45
  __publicField(this, "options");
@@ -59,6 +59,7 @@ var FluxstackLive = (() => {
59
59
  authenticated: false,
60
60
  auth: { authenticated: false, session: null }
61
61
  });
62
+ __publicField(this, "consecutiveHeartbeatFailures", 0);
62
63
  this.options = {
63
64
  url: options.url,
64
65
  auth: options.auth,
@@ -203,16 +204,30 @@ var FluxstackLive = (() => {
203
204
  }
204
205
  startHeartbeat() {
205
206
  this.stopHeartbeat();
207
+ this.consecutiveHeartbeatFailures = 0;
206
208
  this.heartbeatInterval = setInterval(() => {
207
209
  if (this.ws?.readyState === WebSocket.OPEN) {
210
+ let failed = false;
208
211
  for (const componentId of this.componentCallbacks.keys()) {
209
212
  this.sendMessage({
210
213
  type: "COMPONENT_PING",
211
214
  componentId,
212
215
  timestamp: Date.now()
213
216
  }).catch(() => {
217
+ failed = true;
214
218
  });
215
219
  }
220
+ if (failed) {
221
+ this.consecutiveHeartbeatFailures++;
222
+ this.log(`Heartbeat failed (${this.consecutiveHeartbeatFailures}/${_LiveConnection.MAX_HEARTBEAT_FAILURES})`);
223
+ if (this.consecutiveHeartbeatFailures >= _LiveConnection.MAX_HEARTBEAT_FAILURES) {
224
+ this.log("Too many heartbeat failures, reconnecting...");
225
+ this.setState({ error: "Heartbeat failed" });
226
+ this.reconnect();
227
+ }
228
+ } else {
229
+ this.consecutiveHeartbeatFailures = 0;
230
+ }
216
231
  }
217
232
  }, this.options.heartbeatInterval);
218
233
  }
@@ -396,8 +411,15 @@ var FluxstackLive = (() => {
396
411
  { type: "AUTH", payload: credentials },
397
412
  5e3
398
413
  );
399
- const success = response.authenticated || false;
400
- this.setState({ authenticated: success });
414
+ const payload = response.payload;
415
+ const success = payload?.authenticated || false;
416
+ this.setState({
417
+ authenticated: success,
418
+ auth: {
419
+ authenticated: success,
420
+ session: success ? payload?.session || null : null
421
+ }
422
+ });
401
423
  return success;
402
424
  } catch {
403
425
  return false;
@@ -421,6 +443,8 @@ var FluxstackLive = (() => {
421
443
  this.stateListeners.clear();
422
444
  }
423
445
  };
446
+ __publicField(_LiveConnection, "MAX_HEARTBEAT_FAILURES", 3);
447
+ var LiveConnection = _LiveConnection;
424
448
 
425
449
  // src/component.ts
426
450
  function isPlainObject(v) {
@@ -1419,7 +1443,10 @@ var FluxstackLive = (() => {
1419
1443
  userId,
1420
1444
  lastUpdate: Date.now()
1421
1445
  }));
1422
- } catch {
1446
+ } catch (e) {
1447
+ if (typeof console !== "undefined") {
1448
+ console.warn(`[fluxstack] Failed to persist state for '${name}':`, e instanceof Error ? e.message : e);
1449
+ }
1423
1450
  }
1424
1451
  }
1425
1452
  function getPersistedState(enabled, name) {