Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/progress.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { Progress } from 'vscode';
7
8
9
/**
10
* Sends the progress to report to the progress option if promise takes longer than time to wait
11
* @param progress The progress object which receives the progressToReport item
12
* @param progressToReport The progress being reported to the progressToReport item
13
* @param promise The promise which is being executed
14
* @param timeToWait The time to allow that promise to execute before firing off the progress
15
* @returns The promise so that it may be externally awaited
16
*/
17
export async function reportProgressOnSlowPromise<T, P extends Promise<any>>(progress: Progress<T>, progressToReport: T, promise: P, timeToWait: number): Promise<Awaited<P>> {
18
let timeoutId: any | null = null;
19
let promiseResolved = false;
20
21
// Start a timer
22
timeoutId = setTimeout(() => {
23
if (!promiseResolved) {
24
// If the promise is not yet resolved or rejected, report the progress
25
progress.report(progressToReport);
26
}
27
}, timeToWait);
28
29
try {
30
// Wait for the promise to resolve or reject
31
const result = await promise;
32
promiseResolved = true;
33
return result;
34
} finally {
35
// If the timer is still running, clear it
36
if (timeoutId) {
37
clearTimeout(timeoutId);
38
}
39
}
40
}
41
42