Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.0 KB
Raw Blame
// Instant feedback for taps that wait on the server. A request started from a
// button's click (or a form's submit) marks that button `data-pending` until
// the request settles, so every action button shows it is working the moment
// it is tapped, with no per-component state. `request()` reports each call
// here; nothing else needs to opt in.

// Element whose click/submit is being dispatched right now. Cleared on the
// next macrotask, which runs after the handler's synchronous body and its
// microtasks, so only requests that handler started are attributed to it.
let currentTrigger = null;
let clearTimer = null;
const pendingByElement = new WeakMap();
let pendingCount = 0;
const listeners = new Set();

const notify = () => {
  for (const listener of listeners) listener();
};

const setTrigger = (element) => {
  if (!element) return;
  currentTrigger = element;
  clearTimeout(clearTimer);
  clearTimer = setTimeout(() => { currentTrigger = null; }, 0);
};

const triggerFromEvent = (event) => {
  if (event.type === 'submit') {
    // Some engines report the form itself as the submitter of requestSubmit().
    const { submitter } = event;
    if (submitter && submitter !== event.target) return submitter;
    return event.target?.querySelector?.('button[type="submit"], button:not([type])') ?? null;
  }
  return event.target?.closest?.('button, [role="button"]') ?? null;
};

let installed = false;
export function installActionFeedback(doc = globalThis.document) {
  if (installed || !doc) return;
  installed = true;
  const capture = (event) => setTrigger(triggerFromEvent(event));
  doc.addEventListener('click', capture, true);
  doc.addEventListener('submit', capture, true);
}

// Only `data-pending` is set here. `aria-busy` stays with components that
// manage it: a button that sets its busy state in the same click hasn't
// committed it yet when the request starts, so this module could not tell its
// attribute from one it added, and removing it on settle would strip a busy
// state the component still holds. ActionActivityBar announces long waits.
const markPending = (element, delta) => {
  const count = (pendingByElement.get(element) ?? 0) + delta;
  if (count > 0) {
    pendingByElement.set(element, count);
    element.setAttribute('data-pending', 'true');
  } else {
    pendingByElement.delete(element);
    element.removeAttribute('data-pending');
  }
};

// Wraps one in-flight request. Returns the same promise.
export function trackRequest(promise) {
  const element = currentTrigger;
  if (!element) return promise;
  markPending(element, 1);
  pendingCount += 1;
  notify();
  const settle = () => {
    markPending(element, -1);
    pendingCount -= 1;
    notify();
  };
  promise.then(settle, settle);
  return promise;
}

// Count of tap-started requests still in flight, for the global activity bar.
export const getPendingActionCount = () => pendingCount;
export function subscribePendingActions(listener) {
  listeners.add(listener);
  return () => listeners.delete(listener);
}