tw-framework 1.0.7 → 1.0.8

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.
@@ -495,28 +495,44 @@
495
495
  }
496
496
 
497
497
  var es = null;
498
+ // v2: exponential backoff reconnect -- 1s, 2s, 4s, 8s, cap 10s.
499
+ // Every successfully applied frame resets the delay, so a healthy
500
+ // connection never grows its retry window; a dead server stops the
501
+ // client from hammering it every 1.5s forever.
502
+ var backoffMs = 1000, reconnectTimer = null;
498
503
  function open() {
504
+ if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }
499
505
  es = new EventSource(
500
506
  "/_tw/stream?s=" + encodeURIComponent(names.join(",")) + "&since=" + lastSeq
501
507
  );
502
508
  es.onmessage = function (ev) {
503
- try { apply(JSON.parse(ev.data)); } catch (e) { /* malformed frame */ }
509
+ try {
510
+ apply(JSON.parse(ev.data));
511
+ backoffMs = 1000; // healthy frame -> reset the backoff
512
+ } catch (e) { /* malformed frame */ }
504
513
  };
505
514
  es.onerror = function () {
506
515
  // EventSource retries on its own, but with the original URL; close
507
516
  // and reopen so the connection resumes from lastSeq.
508
517
  try { es.close(); } catch (e) { /* already closed */ }
509
- setTimeout(open, 1500);
518
+ es = null;
519
+ reconnectTimer = setTimeout(open, backoffMs);
520
+ backoffMs = Math.min(backoffMs * 2, 10000);
510
521
  };
511
522
  }
512
523
  open();
513
524
  activeStream = {
514
525
  close: function () {
526
+ if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }
515
527
  try { es.close(); } catch (e) { /* already closed */ }
516
528
  activeStream = null;
517
529
  },
518
530
  };
519
- window.__tw.signalStream = { reconnect: open, lastSeq: function () { return lastSeq; } };
531
+ window.__tw.signalStream = {
532
+ reconnect: open,
533
+ lastSeq: function () { return lastSeq; },
534
+ backoffMs: function () { return backoffMs; },
535
+ };
520
536
  }
521
537
 
522
538
  // expose for debugging / userland