Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

1023 B
Raw Blame
/** Interval polling that pauses while the document is hidden. */

export interface PollOptions {
  intervalMs: number;
  /** Defaults to `document`; injectable for tests. */
  doc?: Pick<Document, "visibilityState" | "addEventListener" | "removeEventListener">;
}

/** Runs `tick` now and every `intervalMs` while visible; returns a stopper. */
export function startPolling(tick: () => void, { intervalMs, doc = document }: PollOptions): () => void {
  let timer: ReturnType<typeof setInterval> | undefined;
  const run = () => {
    if (timer === undefined) {
      tick();
      timer = setInterval(tick, intervalMs);
    }
  };
  const pause = () => {
    if (timer !== undefined) clearInterval(timer);
    timer = undefined;
  };
  const onVisibility = () => (doc.visibilityState === "hidden" ? pause() : run());
  doc.addEventListener("visibilitychange", onVisibility);
  if (doc.visibilityState !== "hidden") run();
  return () => {
    pause();
    doc.removeEventListener("visibilitychange", onVisibility);
  };
}