Skip to content

Commit 2646716

Browse files
committed
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.
1 parent a7bb741 commit 2646716

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

extensions/ql-vscode/src/commandRunner.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,35 @@ export interface ProgressUpdate {
4242

4343
export type ProgressCallback = (p: ProgressUpdate) => void;
4444

45+
/**
46+
* A task that reports progress.
47+
*
48+
* @param progress a progress handler function. Call this
49+
* function with a `ProgressUpdate` instance in order to
50+
* denote some progress being achieved on this task.
51+
* @param token a cancellation token
52+
*/
53+
export type ProgressTask<R> = (
54+
progress: ProgressCallback,
55+
token: CancellationToken,
56+
) => Thenable<R>;
57+
4558
/**
4659
* A task that handles command invocations from `commandRunner`
4760
* and includes a progress monitor.
4861
*
4962
*
5063
* Arguments passed to the command handler are passed along,
51-
* untouched to this `ProgressTask` instance.
64+
* untouched to this `ProgressTaskWithArgs` instance.
5265
*
5366
* @param progress a progress handler function. Call this
5467
* function with a `ProgressUpdate` instance in order to
5568
* denote some progress being achieved on this task.
56-
* @param token a cencellation token
69+
* @param token a cancellation token
5770
* @param args arguments passed to this task passed on from
5871
* `commands.registerCommand`.
5972
*/
60-
export type ProgressTask<R> = (
73+
export type ProgressTaskWithArgs<R> = (
6174
progress: ProgressCallback,
6275
token: CancellationToken,
6376
...args: any[]
@@ -92,20 +105,15 @@ type NoProgressTask = (...args: any[]) => Promise<any>;
92105
export function withProgress<R>(
93106
options: ProgressOptions,
94107
task: ProgressTask<R>,
95-
...args: any[]
96108
): Thenable<R> {
97109
let progressAchieved = 0;
98110
return Window.withProgress(options, (progress, token) => {
99-
return task(
100-
(p) => {
101-
const { message, step, maxStep } = p;
102-
const increment = (100 * (step - progressAchieved)) / maxStep;
103-
progressAchieved = step;
104-
progress.report({ message, increment });
105-
},
106-
token,
107-
...args,
108-
);
111+
return task((p) => {
112+
const { message, step, maxStep } = p;
113+
const increment = (100 * (step - progressAchieved)) / maxStep;
114+
progressAchieved = step;
115+
progress.report({ message, increment });
116+
}, token);
109117
});
110118
}
111119

@@ -177,7 +185,7 @@ export function commandRunner(
177185
*/
178186
export function commandRunnerWithProgress<R>(
179187
commandId: string,
180-
task: ProgressTask<R>,
188+
task: ProgressTaskWithArgs<R>,
181189
progressOptions: Partial<ProgressOptions>,
182190
outputLogger = extLogger,
183191
): Disposable {
@@ -189,7 +197,9 @@ export function commandRunnerWithProgress<R>(
189197
...progressOptions,
190198
};
191199

192-
return withProgress(progressOptionsWithDefaults, task, ...args);
200+
return withProgress(progressOptionsWithDefaults, (progress, token) =>
201+
task(progress, token, ...args),
202+
);
193203
},
194204
outputLogger,
195205
);

0 commit comments

Comments
 (0)