-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-util] Update "delay" method in core-util with abort options (#2…
…3019) ### Packages impacted by this PR core-util ### Issues associated with this PR #15930 ### Describe the problem that is addressed by this PR - Update "delay" in core-util to support abort message - Unduplicate "delay" function in core-http and core-amqp ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? - Choose the implementation with "options" parameters to keep the flexibility in case we want to add extra functionality in the future ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ #15832 ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [x ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
- Loading branch information
1 parent
8c9b021
commit a6e366d
Showing
17 changed files
with
117 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { AbortError, AbortSignalLike } from "@azure/abort-controller"; | ||
import { isDefined } from "./typeGuards"; | ||
|
||
const StandardAbortMessage = "The operation was aborted."; | ||
|
||
/** | ||
* Options for support abort functionality for the delay method | ||
*/ | ||
export interface DelayOptions { | ||
/** | ||
* The abortSignal associated with containing operation. | ||
*/ | ||
abortSignal?: AbortSignalLike; | ||
/** | ||
* The abort error message associated with containing operation. | ||
*/ | ||
abortErrorMsg?: string; | ||
} | ||
|
||
/** | ||
* 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: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(() => resolve(), timeInMs)); | ||
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); | ||
} | ||
removeListeners(); | ||
return rejectOnAbort(); | ||
}; | ||
|
||
if (options?.abortSignal && options.abortSignal.aborted) { | ||
return rejectOnAbort(); | ||
} | ||
|
||
timer = setTimeout(() => { | ||
removeListeners(); | ||
resolve(); | ||
}, timeInMs); | ||
|
||
if (options?.abortSignal) { | ||
options.abortSignal.addEventListener("abort", onAborted); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.