|
1 | 1 | import { |
2 | 2 | CancellationToken, |
3 | | - ProgressOptions, |
| 3 | + ProgressOptions as VSCodeProgressOptions, |
4 | 4 | window as Window, |
5 | 5 | commands, |
6 | 6 | Disposable, |
@@ -42,22 +42,40 @@ export interface ProgressUpdate { |
42 | 42 |
|
43 | 43 | export type ProgressCallback = (p: ProgressUpdate) => void; |
44 | 44 |
|
| 45 | +// Make certain properties within a type optional |
| 46 | +type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; |
| 47 | + |
| 48 | +export type ProgressOptions = Optional<VSCodeProgressOptions, "location">; |
| 49 | + |
| 50 | +/** |
| 51 | + * A task that reports progress. |
| 52 | + * |
| 53 | + * @param progress a progress handler function. Call this |
| 54 | + * function with a `ProgressUpdate` instance in order to |
| 55 | + * denote some progress being achieved on this task. |
| 56 | + * @param token a cancellation token |
| 57 | + */ |
| 58 | +export type ProgressTask<R> = ( |
| 59 | + progress: ProgressCallback, |
| 60 | + token: CancellationToken, |
| 61 | +) => Thenable<R>; |
| 62 | + |
45 | 63 | /** |
46 | 64 | * A task that handles command invocations from `commandRunner` |
47 | 65 | * and includes a progress monitor. |
48 | 66 | * |
49 | 67 | * |
50 | 68 | * Arguments passed to the command handler are passed along, |
51 | | - * untouched to this `ProgressTask` instance. |
| 69 | + * untouched to this `ProgressTaskWithArgs` instance. |
52 | 70 | * |
53 | 71 | * @param progress a progress handler function. Call this |
54 | 72 | * function with a `ProgressUpdate` instance in order to |
55 | 73 | * denote some progress being achieved on this task. |
56 | | - * @param token a cencellation token |
| 74 | + * @param token a cancellation token |
57 | 75 | * @param args arguments passed to this task passed on from |
58 | 76 | * `commands.registerCommand`. |
59 | 77 | */ |
60 | | -export type ProgressTask<R> = ( |
| 78 | +export type ProgressTaskWithArgs<R> = ( |
61 | 79 | progress: ProgressCallback, |
62 | 80 | token: CancellationToken, |
63 | 81 | ...args: any[] |
@@ -90,23 +108,29 @@ type NoProgressTask = (...args: any[]) => Promise<any>; |
90 | 108 | * request). |
91 | 109 | */ |
92 | 110 | export function withProgress<R>( |
93 | | - options: ProgressOptions, |
94 | 111 | task: ProgressTask<R>, |
95 | | - ...args: any[] |
| 112 | + { |
| 113 | + location = ProgressLocation.Notification, |
| 114 | + title, |
| 115 | + cancellable, |
| 116 | + }: ProgressOptions = {}, |
96 | 117 | ): Thenable<R> { |
97 | 118 | let progressAchieved = 0; |
98 | | - return Window.withProgress(options, (progress, token) => { |
99 | | - return task( |
100 | | - (p) => { |
| 119 | + return Window.withProgress( |
| 120 | + { |
| 121 | + location, |
| 122 | + title, |
| 123 | + cancellable, |
| 124 | + }, |
| 125 | + (progress, token) => { |
| 126 | + return task((p) => { |
101 | 127 | const { message, step, maxStep } = p; |
102 | 128 | const increment = (100 * (step - progressAchieved)) / maxStep; |
103 | 129 | progressAchieved = step; |
104 | 130 | progress.report({ message, increment }); |
105 | | - }, |
106 | | - token, |
107 | | - ...args, |
108 | | - ); |
109 | | - }); |
| 131 | + }, token); |
| 132 | + }, |
| 133 | + ); |
110 | 134 | } |
111 | 135 |
|
112 | 136 | /** |
@@ -177,19 +201,17 @@ export function commandRunner( |
177 | 201 | */ |
178 | 202 | export function commandRunnerWithProgress<R>( |
179 | 203 | commandId: string, |
180 | | - task: ProgressTask<R>, |
181 | | - progressOptions: Partial<ProgressOptions>, |
| 204 | + task: ProgressTaskWithArgs<R>, |
| 205 | + progressOptions: ProgressOptions, |
182 | 206 | outputLogger = extLogger, |
183 | 207 | ): Disposable { |
184 | 208 | return commandRunner( |
185 | 209 | commandId, |
186 | 210 | async (...args: any[]) => { |
187 | | - const progressOptionsWithDefaults = { |
188 | | - location: ProgressLocation.Notification, |
189 | | - ...progressOptions, |
190 | | - }; |
191 | | - |
192 | | - return withProgress(progressOptionsWithDefaults, task, ...args); |
| 211 | + return withProgress( |
| 212 | + (progress, token) => task(progress, token, ...args), |
| 213 | + progressOptions, |
| 214 | + ); |
193 | 215 | }, |
194 | 216 | outputLogger, |
195 | 217 | ); |
|
0 commit comments