"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const cancellation = require("../../_virtual/cancellation.cjs");
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 cancellation.cancellationExports.CancellationTokenSource();
}
const OperationCancelled = /* @__PURE__ */ Symbol("OperationCancelled");
function isOperationCancelled(err) {
  return err === OperationCancelled;
}
async function interruptAndCheck(token) {
  if (token === cancellation.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;
      };
    });
  }
}
exports.Deferred = Deferred;
exports.OperationCancelled = OperationCancelled;
exports.delayNextTick = delayNextTick;
exports.interruptAndCheck = interruptAndCheck;
exports.isOperationCancelled = isOperationCancelled;
exports.startCancelableOperation = startCancelableOperation;
//# sourceMappingURL=lib-utils-promise-utils.cjs.map
