import { cancellation_exports } from "./cancellation.js";
function delayNextTick() {
	return new Promise((e) => {
		typeof setImmediate > "u" ? setTimeout(e, 0) : setImmediate(e);
	});
}
var lastTick = 0, globalInterruptionPeriod = 10;
function startCancelableOperation() {
	return lastTick = performance.now(), new cancellation_exports.CancellationTokenSource();
}
function setInterruptionPeriod(e) {
	globalInterruptionPeriod = e;
}
const OperationCancelled = Symbol("OperationCancelled");
function isOperationCancelled(e) {
	return e === OperationCancelled;
}
async function interruptAndCheck(c) {
	if (c === cancellation_exports.CancellationToken.None) return;
	let l = performance.now();
	if (l - lastTick >= globalInterruptionPeriod && (lastTick = l, await delayNextTick(), lastTick = performance.now()), c.isCancellationRequested) throw OperationCancelled;
}
var Deferred = class {
	constructor() {
		this.promise = new Promise((e, s) => {
			this.resolve = (s) => (e(s), this), this.reject = (e) => (s(e), this);
		});
	}
};
export { Deferred, OperationCancelled, delayNextTick, interruptAndCheck, isOperationCancelled, setInterruptionPeriod, startCancelableOperation };
