export function createTimelineZoomRenderer({
  requestFrame = globalThis.requestAnimationFrame,
  cancelFrame = globalThis.cancelAnimationFrame,
  onLive,
  onCommit,
} = {}) {
  let frameId = null;
  let destroyed = false;

  function live() {
    if (destroyed || frameId !== null) return;
    frameId = requestFrame(() => {
      frameId = null;
      if (!destroyed) onLive?.();
    });
  }

  function commit() {
    if (destroyed) return;
    if (frameId !== null) {
      cancelFrame(frameId);
      frameId = null;
    }
    onCommit?.();
  }

  function destroy() {
    destroyed = true;
    if (frameId !== null) {
      cancelFrame(frameId);
      frameId = null;
    }
  }

  return {
    live,
    commit,
    destroy,
    isPending: () => frameId !== null,
  };
}
