import * as tsplus_module_1 from "@tsplus/stdlib/collections/mutable/DoublyLinkedList";
const scheduled = {
  running: false,
  tasks: /*#__PURE__*/new tsplus_module_1.DoublyLinkedList()
};

function starveInternal(depth) {
  const toRun = scheduled.tasks;
  scheduled.tasks = new tsplus_module_1.DoublyLinkedList();
  toRun.forEach(task => {
    task();
  });

  if (scheduled.tasks.isEmpty) {
    scheduled.running = false;
  } else {
    starve(depth);
  }
}

function starve(depth = 0) {
  if (depth >= 2048) {
    setTimeout(() => starveInternal(0), 0);
  } else {
    queueMicrotask(() => starveInternal(depth + 1));
  }
}

export function scheduleTask(task) {
  scheduled.tasks.add(task);

  if (!scheduled.running) {
    scheduled.running = true;
    starve();
  }
}
//# sourceMappingURL=Scheduler.mjs.map