Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

832 B
Raw Blame
/** Coalesce a burst, then repeat if another invalidation arrives during the read. */
export function createTrailingRefetch(
  enabled: () => boolean,
  refetch: () => Promise<unknown>
) {
  let pending: Promise<void> | undefined;
  let requested = false;
  return () => {
    if (!enabled()) return Promise.resolve();
    requested = true;
    pending ??= Promise.resolve().then(async () => {
      try {
        let failed = false;
        let error: unknown;
        while (requested && enabled()) {
          requested = false;
          try {
            await refetch();
            failed = false;
          } catch (cause) {
            failed = true;
            error = cause;
          }
        }
        if (failed) throw error;
      } finally {
        pending = undefined;
      }
    });
    return pending;
  };
}