-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprogress.ts
More file actions
55 lines (51 loc) · 1.72 KB
/
progress.ts
File metadata and controls
55 lines (51 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as vscode from "vscode";
export type ProgressResult<T> =
| { ok: true; value: T }
| { ok: false; cancelled: true }
| { ok: false; cancelled: false; error: unknown };
export interface ProgressContext {
progress: vscode.Progress<{ message?: string; increment?: number }>;
signal: AbortSignal;
}
/**
* Run a task inside a VS Code progress notification with cancellation support.
* Errors thrown by the task are captured in the result rather than propagated
* through `withProgress`, and AbortErrors from cancellation are surfaced as
* `{ cancelled: true }`.
*/
export function withCancellableProgress<T>(
options: vscode.ProgressOptions & { cancellable: true },
fn: (ctx: ProgressContext) => Promise<T>,
): Thenable<ProgressResult<T>> {
return vscode.window.withProgress(
options,
async (progress, ct): Promise<ProgressResult<T>> => {
const ac = new AbortController();
const listener = ct.onCancellationRequested(() => ac.abort());
try {
const value = await fn({ progress, signal: ac.signal });
return { ok: true, value };
} catch (error) {
if ((error as Error).name === "AbortError") {
return { ok: false, cancelled: true };
}
return { ok: false, cancelled: false, error };
} finally {
listener.dispose();
}
},
);
}
/**
* Run a task inside a VS Code progress notification (no cancellation).
* A thin wrapper over `vscode.window.withProgress` that passes only the
* progress reporter, hiding the unused cancellation token.
*/
export function withProgress<T>(
options: vscode.ProgressOptions,
fn: (
progress: vscode.Progress<{ message?: string; increment?: number }>,
) => Promise<T>,
): Thenable<T> {
return vscode.window.withProgress(options, (progress) => fn(progress));
}