import { c as cancellationExports } from "../../_virtual/cancellation.js";
function delayNextTick() {
  return new Promise((resolve) => {
    if (typeof setImmediate === "undefined") {
      setTimeout(resolve, 0);
    } else {
      setImmediate(resolve);
    }
  });
}
let lastTick = 0;
let globalInterruptionPeriod = 10;
function startCancelableOperation() {
  lastTick = performance.now();
  return new cancellationExports.CancellationTokenSource();
}
const OperationCancelled = Symbol("OperationCancelled");
function isOperationCancelled(err) {
  return err === OperationCancelled;
}
async function interruptAndCheck(token) {
  if (token === cancellationExports.CancellationToken.None) {
    return;
  }
  const current = performance.now();
  if (current - lastTick >= globalInterruptionPeriod) {
    lastTick = current;
    await delayNextTick();
    lastTick = performance.now();
  }
  if (token.isCancellationRequested) {
    throw OperationCancelled;
  }
}
class Deferred {
  constructor() {
    this.promise = new Promise((resolve, reject) => {
      this.resolve = (arg) => {
        resolve(arg);
        return this;
      };
      this.reject = (err) => {
        reject(err);
        return this;
      };
    });
  }
}
export {
  Deferred,
  OperationCancelled,
  delayNextTick,
  interruptAndCheck,
  isOperationCancelled,
  startCancelableOperation
};
//# sourceMappingURL=lib-utils-promise-utils.js.map
