Path: blob/main/extensions/copilot/src/util/common/progress.ts
13397 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { Progress } from 'vscode';678/**9* Sends the progress to report to the progress option if promise takes longer than time to wait10* @param progress The progress object which receives the progressToReport item11* @param progressToReport The progress being reported to the progressToReport item12* @param promise The promise which is being executed13* @param timeToWait The time to allow that promise to execute before firing off the progress14* @returns The promise so that it may be externally awaited15*/16export async function reportProgressOnSlowPromise<T, P extends Promise<any>>(progress: Progress<T>, progressToReport: T, promise: P, timeToWait: number): Promise<Awaited<P>> {17let timeoutId: any | null = null;18let promiseResolved = false;1920// Start a timer21timeoutId = setTimeout(() => {22if (!promiseResolved) {23// If the promise is not yet resolved or rejected, report the progress24progress.report(progressToReport);25}26}, timeToWait);2728try {29// Wait for the promise to resolve or reject30const result = await promise;31promiseResolved = true;32return result;33} finally {34// If the timer is still running, clear it35if (timeoutId) {36clearTimeout(timeoutId);37}38}39}404142