Remove args from ProgressTask

This removes the `args` from the `ProgressTask` passed to
`withProgress`. The `args` is only used by the
`commandRunnerWithProgress` and can easily be replaced by an anonymous
function that passes the `args` instead. This will simplify the
`ProgressTask` interface and make it easier to use.
This commit is contained in:
Koen Vlaswinkel
2023-03-13 14:51:32 +01:00
parent a7bb74190f
commit 2646716261

View File

@@ -42,22 +42,35 @@ export interface ProgressUpdate {
export type ProgressCallback = (p: ProgressUpdate) => void; export type ProgressCallback = (p: ProgressUpdate) => void;
/**
* A task that reports progress.
*
* @param progress a progress handler function. Call this
* function with a `ProgressUpdate` instance in order to
* denote some progress being achieved on this task.
* @param token a cancellation token
*/
export type ProgressTask<R> = (
progress: ProgressCallback,
token: CancellationToken,
) => Thenable<R>;
/** /**
* A task that handles command invocations from `commandRunner` * A task that handles command invocations from `commandRunner`
* and includes a progress monitor. * and includes a progress monitor.
* *
* *
* Arguments passed to the command handler are passed along, * Arguments passed to the command handler are passed along,
* untouched to this `ProgressTask` instance. * untouched to this `ProgressTaskWithArgs` instance.
* *
* @param progress a progress handler function. Call this * @param progress a progress handler function. Call this
* function with a `ProgressUpdate` instance in order to * function with a `ProgressUpdate` instance in order to
* denote some progress being achieved on this task. * denote some progress being achieved on this task.
* @param token a cencellation token * @param token a cancellation token
* @param args arguments passed to this task passed on from * @param args arguments passed to this task passed on from
* `commands.registerCommand`. * `commands.registerCommand`.
*/ */
export type ProgressTask<R> = ( export type ProgressTaskWithArgs<R> = (
progress: ProgressCallback, progress: ProgressCallback,
token: CancellationToken, token: CancellationToken,
...args: any[] ...args: any[]
@@ -92,20 +105,15 @@ type NoProgressTask = (...args: any[]) => Promise<any>;
export function withProgress<R>( export function withProgress<R>(
options: ProgressOptions, options: ProgressOptions,
task: ProgressTask<R>, task: ProgressTask<R>,
...args: any[]
): Thenable<R> { ): Thenable<R> {
let progressAchieved = 0; let progressAchieved = 0;
return Window.withProgress(options, (progress, token) => { return Window.withProgress(options, (progress, token) => {
return task( return task((p) => {
(p) => { const { message, step, maxStep } = p;
const { message, step, maxStep } = p; const increment = (100 * (step - progressAchieved)) / maxStep;
const increment = (100 * (step - progressAchieved)) / maxStep; progressAchieved = step;
progressAchieved = step; progress.report({ message, increment });
progress.report({ message, increment }); }, token);
},
token,
...args,
);
}); });
} }
@@ -177,7 +185,7 @@ export function commandRunner(
*/ */
export function commandRunnerWithProgress<R>( export function commandRunnerWithProgress<R>(
commandId: string, commandId: string,
task: ProgressTask<R>, task: ProgressTaskWithArgs<R>,
progressOptions: Partial<ProgressOptions>, progressOptions: Partial<ProgressOptions>,
outputLogger = extLogger, outputLogger = extLogger,
): Disposable { ): Disposable {
@@ -189,7 +197,9 @@ export function commandRunnerWithProgress<R>(
...progressOptions, ...progressOptions,
}; };
return withProgress(progressOptionsWithDefaults, task, ...args); return withProgress(progressOptionsWithDefaults, (progress, token) =>
task(progress, token, ...args),
);
}, },
outputLogger, outputLogger,
); );