[]previous and next page

Polling and cleanup

A resource aborts its in-flight request and drops late results when its owner is disposed.

For agentsUpdated

A resource aborts its in-flight request and drops late results when its owner is disposed. Timers you add are yours to clean up: return the cleanup from the effect, or call onCleanup.

src/poll.tsxtsx
import { useEffect, useResource } from "@shadow-js/core";

export function Clock(props: { load: () => Promise<number>; everyMs: number }) {
  const tick = useResource(() => props.load());
  useEffect(() => {
    const id = setInterval(() => void tick.refetch(), props.everyMs);
    return () => clearInterval(id);
  });
  return <output>{tick.latest}</output>;
}

The effect has no tracked reads other than props.everyMs, so it creates the interval once and again only if the prop changes. refetch() on a poll keeps the previous value visible, so the screen does not flicker to a loading state.