@azure/core-util
Advanced tools
Comparing version 1.1.2-alpha.20230209.1 to 1.1.2-alpha.20230215.3
@@ -6,34 +6,57 @@ // Copyright (c) Microsoft Corporation. | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
* @param timeInMs - The number of milliseconds to be delayed. | ||
* @param options - The options for delay - currently abort options | ||
* @returns Promise that is resolved after timeInMs | ||
* Creates an abortable promise. | ||
* @param buildPromise - A function that takes the resolve and reject functions as parameters. | ||
* @param options - The options for the abortable promise. | ||
* @returns A promise that can be aborted. | ||
* @internal | ||
*/ | ||
export function delay(timeInMs, options) { | ||
export function createAbortablePromise(buildPromise, options) { | ||
const { cleanupBeforeAbort, abortSignal, abortErrorMsg } = options !== null && options !== void 0 ? options : {}; | ||
return new Promise((resolve, reject) => { | ||
var _a, _b; | ||
function rejectOnAbort() { | ||
var _a; | ||
reject(new AbortError((_a = options === null || options === void 0 ? void 0 : options.abortErrorMsg) !== null && _a !== void 0 ? _a : StandardAbortMessage)); | ||
reject(new AbortError(abortErrorMsg !== null && abortErrorMsg !== void 0 ? abortErrorMsg : "The operation was aborted.")); | ||
} | ||
function removeListeners() { | ||
var _a; | ||
(_a = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _a === void 0 ? void 0 : _a.removeEventListener("abort", onAbort); | ||
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.removeEventListener("abort", onAbort); | ||
} | ||
function onAbort() { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
clearTimeout(token); | ||
cleanupBeforeAbort === null || cleanupBeforeAbort === void 0 ? void 0 : cleanupBeforeAbort(); | ||
removeListeners(); | ||
rejectOnAbort(); | ||
} | ||
if ((_a = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) { | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
return rejectOnAbort(); | ||
} | ||
const token = setTimeout(() => { | ||
removeListeners(); | ||
resolve(); | ||
}, timeInMs); | ||
(_b = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _b === void 0 ? void 0 : _b.addEventListener("abort", onAbort); | ||
try { | ||
buildPromise((x) => { | ||
removeListeners(); | ||
resolve(x); | ||
}, (x) => { | ||
removeListeners(); | ||
reject(x); | ||
}); | ||
} | ||
catch (err) { | ||
reject(err); | ||
} | ||
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.addEventListener("abort", onAbort); | ||
}); | ||
} | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
* @param timeInMs - The number of milliseconds to be delayed. | ||
* @param options - The options for delay - currently abort options | ||
* @returns Promise that is resolved after timeInMs | ||
*/ | ||
export function delay(timeInMs, options) { | ||
let token; | ||
const { abortSignal, abortErrorMsg } = options !== null && options !== void 0 ? options : {}; | ||
return createAbortablePromise((resolve) => { | ||
token = setTimeout(resolve, timeInMs); | ||
}, { | ||
cleanupBeforeAbort: () => clearTimeout(token), | ||
abortSignal, | ||
abortErrorMsg: abortErrorMsg !== null && abortErrorMsg !== void 0 ? abortErrorMsg : StandardAbortMessage, | ||
}); | ||
} | ||
//# sourceMappingURL=delay.js.map |
@@ -19,34 +19,57 @@ 'use strict'; | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
* @param timeInMs - The number of milliseconds to be delayed. | ||
* @param options - The options for delay - currently abort options | ||
* @returns Promise that is resolved after timeInMs | ||
* Creates an abortable promise. | ||
* @param buildPromise - A function that takes the resolve and reject functions as parameters. | ||
* @param options - The options for the abortable promise. | ||
* @returns A promise that can be aborted. | ||
* @internal | ||
*/ | ||
function delay(timeInMs, options) { | ||
function createAbortablePromise(buildPromise, options) { | ||
const { cleanupBeforeAbort, abortSignal, abortErrorMsg } = options !== null && options !== void 0 ? options : {}; | ||
return new Promise((resolve, reject) => { | ||
var _a, _b; | ||
function rejectOnAbort() { | ||
var _a; | ||
reject(new abortController.AbortError((_a = options === null || options === void 0 ? void 0 : options.abortErrorMsg) !== null && _a !== void 0 ? _a : StandardAbortMessage)); | ||
reject(new abortController.AbortError(abortErrorMsg !== null && abortErrorMsg !== void 0 ? abortErrorMsg : "The operation was aborted.")); | ||
} | ||
function removeListeners() { | ||
var _a; | ||
(_a = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _a === void 0 ? void 0 : _a.removeEventListener("abort", onAbort); | ||
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.removeEventListener("abort", onAbort); | ||
} | ||
function onAbort() { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
clearTimeout(token); | ||
cleanupBeforeAbort === null || cleanupBeforeAbort === void 0 ? void 0 : cleanupBeforeAbort(); | ||
removeListeners(); | ||
rejectOnAbort(); | ||
} | ||
if ((_a = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) { | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
return rejectOnAbort(); | ||
} | ||
const token = setTimeout(() => { | ||
removeListeners(); | ||
resolve(); | ||
}, timeInMs); | ||
(_b = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _b === void 0 ? void 0 : _b.addEventListener("abort", onAbort); | ||
try { | ||
buildPromise((x) => { | ||
removeListeners(); | ||
resolve(x); | ||
}, (x) => { | ||
removeListeners(); | ||
reject(x); | ||
}); | ||
} | ||
catch (err) { | ||
reject(err); | ||
} | ||
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.addEventListener("abort", onAbort); | ||
}); | ||
} | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
* @param timeInMs - The number of milliseconds to be delayed. | ||
* @param options - The options for delay - currently abort options | ||
* @returns Promise that is resolved after timeInMs | ||
*/ | ||
function delay(timeInMs, options) { | ||
let token; | ||
const { abortSignal, abortErrorMsg } = options !== null && options !== void 0 ? options : {}; | ||
return createAbortablePromise((resolve) => { | ||
token = setTimeout(resolve, timeInMs); | ||
}, { | ||
cleanupBeforeAbort: () => clearTimeout(token), | ||
abortSignal, | ||
abortErrorMsg: abortErrorMsg !== null && abortErrorMsg !== void 0 ? abortErrorMsg : StandardAbortMessage, | ||
}); | ||
} | ||
@@ -53,0 +76,0 @@ // Copyright (c) Microsoft Corporation. |
{ | ||
"name": "@azure/core-util", | ||
"version": "1.1.2-alpha.20230209.1", | ||
"version": "1.1.2-alpha.20230215.3", | ||
"description": "Core library for shared utility methods", | ||
@@ -79,2 +79,3 @@ "sdk-type": "client", | ||
"@types/chai": "^4.1.6", | ||
"@types/chai-as-promised": "^7.1.4", | ||
"@types/mocha": "^7.0.2", | ||
@@ -85,2 +86,3 @@ "@types/node": "^14.0.0", | ||
"chai": "^4.2.0", | ||
"chai-as-promised": "^7.1.1", | ||
"downlevel-dts": "^0.10.0", | ||
@@ -87,0 +89,0 @@ "cross-env": "^7.0.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72236
730
32