(
action: Promise<T>,
cancellationTokenSource: vs.CancellationTokenSource,
progressText: string,
{ showAfterMs = oneSecondInMs }: { showAfterMs?: number } = {},
)
| 6 | * takes longer than a specified time. |
| 7 | */ |
| 8 | export function withProgressIfSlow<T>( |
| 9 | action: Promise<T>, |
| 10 | cancellationTokenSource: vs.CancellationTokenSource, |
| 11 | progressText: string, |
| 12 | { showAfterMs = oneSecondInMs }: { showAfterMs?: number } = {}, |
| 13 | ): Promise<T> { |
| 14 | // Show progress until the action completes, but only start after the specified time. |
| 15 | const progressTimer = setTimeout(() => { |
| 16 | vs.window.withProgress( |
| 17 | { |
| 18 | title: progressText, |
| 19 | location: vs.ProgressLocation.Notification, |
| 20 | cancellable: true, |
| 21 | }, (_progress, token) => { |
| 22 | token.onCancellationRequested(() => cancellationTokenSource.cancel()); |
| 23 | return action; |
| 24 | }, |
| 25 | ); |
| 26 | }, showAfterMs); |
| 27 | |
| 28 | // Don't keep anything alive. |
| 29 | progressTimer.unref(); |
| 30 | |
| 31 | // If the action completes before the timer fires, cancel it. |
| 32 | return action.finally(() => clearTimeout(progressTimer)); |
| 33 | } |
no test coverage detected