'use strict'; /* eslint-disable @typescript-eslint/no-invalid-void-type */ class AbortError extends Error { constructor(message) { super(message); this.name = 'AbortError'; } } function promiseFromSignal(signal) { return new Promise((_, reject) => { signal === null || signal === void 0 || signal.addEventListener('abort', () => { reject(new AbortError(signal.reason)); }); }); } /** * Create an interval timer that can be aborted with an AbortSignal. * * ```typescript * const callback = () => { * console.log('Interval callback'); * }; * * const controller = new AbortController(); * interval(callback, 1000, { signal: controller.signal }); * * // To abort the interval * controller.abort(); * ``` * * @param callback The callback function to execute. * @param ms The interval time in milliseconds. * @param options An optional AbortSignal to abort the interval. */ function interval(callback, ms, options) { var _options$signal, _options$signal2; options === null || options === void 0 || (_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.throwIfAborted(); const timer = setInterval(callback, ms); options === null || options === void 0 || (_options$signal2 = options.signal) === null || _options$signal2 === void 0 || _options$signal2.addEventListener('abort', () => { clearInterval(timer); }); } /** * Set a timeout timer that can be aborted with an AbortSignal. * * ```typescript * const controller = new AbortController(); * timeout(() => { * console.log('Timeout callback'); * }, 5000, { signal: controller.signal }); * * // To abort the timeout * controller.abort(); * ``` * * @param callback The callback function to execute after the timeout. * @param ms The timeout duration in milliseconds. * @param options An optional AbortSignal to abort the timeout. */ function timeout(callback, ms, options) { var _options$signal3, _options$signal5; options === null || options === void 0 || (_options$signal3 = options.signal) === null || _options$signal3 === void 0 || _options$signal3.throwIfAborted(); function onAbort() { clearTimeout(timer); } const timer = setTimeout(function (...args) { var _options$signal4; options === null || options === void 0 || (_options$signal4 = options.signal) === null || _options$signal4 === void 0 || _options$signal4.removeEventListener('abort', onAbort); if (callback instanceof Function) { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call callback(...args); } else { // eslint-disable-next-line @typescript-eslint/no-implied-eval new Function(callback)(...args); } }, ms); options === null || options === void 0 || (_options$signal5 = options.signal) === null || _options$signal5 === void 0 || _options$signal5.addEventListener('abort', onAbort); } /** * Create a promise-based delay that can be aborted with an AbortSignal. * * ```typescript * await delay(5000, { signal: AbortSignal.timeout(1000) }) * * // This line of code will not execute; the preceding line will be rejected after 1000ms, throwing an AbortError. * ``` * * @param ms The delay time in milliseconds. * @param options An optional AbortSignal to abort the delay. * @returns a promise resolved after ms. */ function delay(ms, options) { return Promise.race([new Promise(function (resolve) { timeout(function () { resolve(); }, ms, options); }), promiseFromSignal(options === null || options === void 0 ? void 0 : options.signal)]); } /** * Set a animation frame callback that can be aborted with an AbortSignal. * * ```typescript * const controller = new AbortController(); * animationFrame(() => { * console.log('animation callback'); * }, { signal: controller.signal }); * * // To abort the timeout * controller.abort(); * ``` * * @param callback The callback function to execute in next animation frame. * @param options An optional AbortSignal to abort the animation frame callback. */ function animationFrame(callback, options) { var _options$signal6, _options$signal8; options === null || options === void 0 || (_options$signal6 = options.signal) === null || _options$signal6 === void 0 || _options$signal6.throwIfAborted(); function onAbort() { cancelAnimationFrame(timer); } const timer = requestAnimationFrame(function (...args) { var _options$signal7; options === null || options === void 0 || (_options$signal7 = options.signal) === null || _options$signal7 === void 0 || _options$signal7.removeEventListener('abort', onAbort); callback(...args); }); options === null || options === void 0 || (_options$signal8 = options.signal) === null || _options$signal8 === void 0 || _options$signal8.addEventListener('abort', onAbort); } exports.animationFrame = animationFrame; exports.delay = delay; exports.interval = interval; exports.timeout = timeout;