Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions sdk/core/core-util/src/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Licensed under the MIT license.

import { AbortError, AbortSignalLike } from "@azure/abort-controller";
import { isDefined } from "./typeGuards";

const StandardAbortMessage = "The operation was aborted.";
const StandardAbortMessage = "The delay was aborted.";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some Storage tests at least might be sniffing the message, so you could see some failures with brittle tests. I tried to clean most of that up recently, but fair warning. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The packages in the CI pipeline seem to be happy but if you come across any such failure, please pass it my way and I will address it.


/**
* Options for support abort functionality for the delay method
Expand All @@ -28,38 +27,25 @@ export interface DelayOptions {
*/
export function delay(timeInMs: number, options?: DelayOptions): Promise<void> {
return new Promise((resolve, reject) => {
let timer: ReturnType<typeof setTimeout> | undefined = undefined;
let onAborted: (() => void) | undefined = undefined;

const rejectOnAbort = (): void => {
return reject(new AbortError(options?.abortErrorMsg ?? StandardAbortMessage));
};

const removeListeners = (): void => {
if (options?.abortSignal && onAborted) {
options.abortSignal.removeEventListener("abort", onAborted);
}
};

onAborted = (): void => {
if (isDefined(timer)) {
clearTimeout(timer);
}
Comment thread
deyaaeldeen marked this conversation as resolved.
function rejectOnAbort(): void {
reject(new AbortError(options?.abortErrorMsg ?? StandardAbortMessage));
}
function removeListeners(): void {
options?.abortSignal?.removeEventListener("abort", onAbort);
}
function onAbort(): void {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
clearTimeout(token);
removeListeners();
return rejectOnAbort();
};

if (options?.abortSignal && options.abortSignal.aborted) {
rejectOnAbort();
}
if (options?.abortSignal?.aborted) {
return rejectOnAbort();
}

timer = setTimeout(() => {
const token = setTimeout(() => {
removeListeners();
resolve();
}, timeInMs);

if (options?.abortSignal) {
options.abortSignal.addEventListener("abort", onAborted);
}
options?.abortSignal?.addEventListener("abort", onAbort);
});
}