fsanyoto commited on
Commit
8caabf7
·
verified ·
1 Parent(s): ef82438

Cost hotfix: remove recurring grid database polling

Browse files

Token baselines now refresh only at mount and user return; no background interval remains.

Files changed (1) hide show
  1. web/src/customer-grid/apiBridge.ts +21 -37
web/src/customer-grid/apiBridge.ts CHANGED
@@ -610,37 +610,33 @@ export async function fetchChangeTokens(scope: string): Promise<ChangeTokens | n
610
  return out;
611
  }
612
 
613
- /** ~10 s. The owner's ask is "a new record shows up in about ten seconds", and the request it
614
- * costs is a dict read server-side — see `routes_changes.py` for why that is the whole design. */
615
- export const CHANGE_POLL_MS = 10_000;
616
-
617
  interface ChangeWatch {
618
- timer: ReturnType<typeof setInterval> | null;
619
  tokens: ChangeTokens | null;
620
  listeners: Set<(changed: string[]) => void>;
621
  inFlight: boolean;
622
- /** ⭐ ONE poll implementation per watch, held here so the interval and the visibility handler
623
- * call the SAME function. The visibility handler used to re-inline the body, which quietly
624
- * skipped `inFlight` — so returning to a tab whose poll was still outstanding stacked a second
625
- * request on top of it, exactly what that guard exists to prevent. */
626
  poll: () => Promise<void>;
627
  }
628
 
629
  const changeWatches = new Map<string, ChangeWatch>();
630
  let visibilityBound = false;
631
 
 
 
 
 
 
 
632
  /**
633
  * Watch one scope for server-side changes. Returns its unsubscribe.
634
  *
635
- * ⭐ ONE TIMER PER SCOPE, REFCOUNTED, and that is a budget rather than tidiness. A linked-record
636
- * grid mounts a second `useCustomerData` on the same surface, and a per-hook interval would double
637
- * the poll rate for a table the user opened once. The done-when is "roughly 6 tiny calls per minute
638
- * per tab" — per TAB, not per component.
639
- *
640
- * ⛔ IT DOES NOT POLL A HIDDEN TAB, and it polls IMMEDIATELY on becoming visible again. A background
641
- * tab left open overnight would otherwise spend the night asking a question nobody can see the
642
- * answer to, and the one moment its answer certainly matters is the moment you look at it.
643
- * `visibilitychange` had zero listeners in this client before this.
644
  */
645
  export function subscribeChanges(scope: string,
646
  onChange: (changed: string[]) => void): () => void {
@@ -648,10 +644,9 @@ export function subscribeChanges(scope: string,
648
  let watch = changeWatches.get(scope);
649
  if (!watch) {
650
  const w0: ChangeWatch = {
651
- timer: null, tokens: null, listeners: new Set(), inFlight: false,
652
  poll: async () => {
653
- // A slow answer must not queue a second request behind itself: on a cold Space the first
654
- // poll can outlive the interval, and without this the backlog grows while it stays slow.
655
  if (w0.inFlight || document.visibilityState !== "visible") return;
656
  w0.inFlight = true;
657
  try {
@@ -670,28 +665,17 @@ export function subscribeChanges(scope: string,
670
  const w = watch;
671
  w.listeners.add(onChange);
672
 
673
- if (!w.timer) {
674
- void w.poll(); // take the baseline now, not in ten seconds
675
- w.timer = setInterval(() => { void w.poll(); }, CHANGE_POLL_MS);
676
- }
677
  if (!visibilityBound) {
678
  visibilityBound = true;
679
- window.addEventListener("visibilitychange", () => {
680
- if (document.visibilityState !== "visible") return;
681
- for (const watched of changeWatches.values())
682
- if (watched.timer) void watched.poll();
683
- });
684
  }
685
 
686
  return () => {
687
  w.listeners.delete(onChange);
688
- if (w.listeners.size === 0 && w.timer) {
689
- clearInterval(w.timer);
690
- w.timer = null;
691
- // ⚠ The tokens are DROPPED with the last listener. A remount must re-baseline rather than
692
- // compare against a token from before it was unmounted: the payload it is about to fetch is
693
- // fresh by definition, so treating the interval it missed as "a change" would cost a second
694
- // full read of what it just read.
695
  w.tokens = null;
696
  }
697
  };
 
610
  return out;
611
  }
612
 
613
+ /**
614
+ * Token checks are event driven: once at scope mount and when a person returns to the visible tab
615
+ * or window. There is intentionally no recurring timer.
616
+ */
617
  interface ChangeWatch {
 
618
  tokens: ChangeTokens | null;
619
  listeners: Set<(changed: string[]) => void>;
620
  inFlight: boolean;
621
+ /** One implementation per scope so mount, focus, and visibility refreshes share the same guard. */
 
 
 
622
  poll: () => Promise<void>;
623
  }
624
 
625
  const changeWatches = new Map<string, ChangeWatch>();
626
  let visibilityBound = false;
627
 
628
+ function refreshVisibleChanges(): void {
629
+ if (document.visibilityState !== "visible") return;
630
+ for (const watched of changeWatches.values())
631
+ if (watched.listeners.size > 0) void watched.poll();
632
+ }
633
+
634
  /**
635
  * Watch one scope for server-side changes. Returns its unsubscribe.
636
  *
637
+ * One watch per scope is reference counted. Its initial token baseline is shared by every grid on
638
+ * that scope, and a change made elsewhere is checked only when the user returns to this browser
639
+ * tab or focuses its window. A forgotten tab creates no database traffic.
 
 
 
 
 
 
640
  */
641
  export function subscribeChanges(scope: string,
642
  onChange: (changed: string[]) => void): () => void {
 
644
  let watch = changeWatches.get(scope);
645
  if (!watch) {
646
  const w0: ChangeWatch = {
647
+ tokens: null, listeners: new Set(), inFlight: false,
648
  poll: async () => {
649
+ // A slow answer must not queue another mount/focus request behind itself.
 
650
  if (w0.inFlight || document.visibilityState !== "visible") return;
651
  w0.inFlight = true;
652
  try {
 
665
  const w = watch;
666
  w.listeners.add(onChange);
667
 
668
+ if (w.tokens === null) void w.poll();
 
 
 
669
  if (!visibilityBound) {
670
  visibilityBound = true;
671
+ window.addEventListener("visibilitychange", refreshVisibleChanges);
672
+ window.addEventListener("focus", refreshVisibleChanges);
 
 
 
673
  }
674
 
675
  return () => {
676
  w.listeners.delete(onChange);
677
+ if (w.listeners.size === 0) {
678
+ // A remount takes a new baseline rather than comparing against a stale, unobserved token.
 
 
 
 
 
679
  w.tokens = null;
680
  }
681
  };