"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHeaderValueAsNumber = exports.delay = void 0; const abort_controller_1 = require("@azure/abort-controller"); const StandardAbortMessage = "The operation was aborted."; /** * A wrapper for setTimeout that resolves a promise after delayInMs milliseconds. * @param delayInMs - The number of milliseconds to be delayed. * @param value - The value to be resolved with after a timeout of t milliseconds. * @param options - The options for delay - currently abort options * - abortSignal - The abortSignal associated with containing operation. * - abortErrorMsg - The abort error message associated with containing operation. * @returns Resolved promise */ function delay(delayInMs, value, options) { return new Promise((resolve, reject) => { let timer = undefined; let onAborted = undefined; const rejectOnAbort = () => { return reject(new abort_controller_1.AbortError((options === null || options === void 0 ? void 0 : options.abortErrorMsg) ? options === null || options === void 0 ? void 0 : options.abortErrorMsg : StandardAbortMessage)); }; const removeListeners = () => { if ((options === null || options === void 0 ? void 0 : options.abortSignal) && onAborted) { options.abortSignal.removeEventListener("abort", onAborted); } }; onAborted = () => { if (timer) { clearTimeout(timer); } removeListeners(); return rejectOnAbort(); }; if ((options === null || options === void 0 ? void 0 : options.abortSignal) && options.abortSignal.aborted) { return rejectOnAbort(); } timer = setTimeout(() => { removeListeners(); resolve(value); }, delayInMs); if (options === null || options === void 0 ? void 0 : options.abortSignal) { options.abortSignal.addEventListener("abort", onAborted); } }); } exports.delay = delay; /** * @internal * @returns the parsed value or undefined if the parsed value is invalid. */ function parseHeaderValueAsNumber(response, headerName) { const value = response.headers.get(headerName); if (!value) return; const valueAsNum = Number(value); if (Number.isNaN(valueAsNum)) return; return valueAsNum; } exports.parseHeaderValueAsNumber = parseHeaderValueAsNumber; //# sourceMappingURL=helpers.js.map