src/vs/platform/progress/common/progress.ts

229 LOC · 170 covered · 59 uncovered · 9 ranges · 353 concepts · 1 introducers · 179 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- progress.ts ×9
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { IAction } from '../../../base/common/actions.js';
7 > import { DeferredPromise } from '../../../base/common/async.js';
8 > import { CancellationToken, CancellationTokenSource } from '../../../base/common/cancellation.js';
9 > import { Disposable, DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';
10 > import { createDecorator } from '../../instantiation/common/instantiation.js';
11 > import { INotificationSource, NotificationPriority } from '../../notification/common/notification.js';
12 >
13 > export const IProgressService = createDecorator<IProgressService>('progressService');
14 >
15 > /**
16 > * A progress service that can be used to report progress to various locations of the UI.
17 > */
18 > export interface IProgressService {
19 >
20 > readonly _serviceBrand: undefined;
21 >
22 > withProgress<R>(
23 > options: IProgressOptions | IProgressDialogOptions | IProgressNotificationOptions | IProgressWindowOptions | IProgressCompositeOptions,
24 > task: (progress: IProgress<IProgressStep>) => Promise<R>,
25 > onDidCancel?: (choice?: number) => void
26 > ): Promise<R>;
27 > }
28 >
29 > export interface IProgressIndicator {
30 >
31 > /**
32 > * Show progress customized with the provided flags.
33 > */
34 > show(infinite: true, delay?: number): IProgressRunner;
35 > show(total: number, delay?: number): IProgressRunner;
36 >
37 > /**
38 > * Indicate progress for the duration of the provided promise. Progress will stop in
39 > * any case of promise completion, error or cancellation.
40 > */
41 > showWhile(promise: Promise<unknown>, delay?: number): Promise<void>;
42 > }
43 >
44 > export const enum ProgressLocation {
45 > Explorer = 1,
46 > Scm = 3,
47 > Extensions = 5,
48 > Window = 10,
49 > Notification = 15,
50 > Dialog = 20
51 > }
52 >
53 > export interface IProgressOptions {
54 > readonly location: ProgressLocation | string;
55 > readonly title?: string;
56 > readonly source?: string | INotificationSource;
57 > readonly total?: number;
58 > readonly cancellable?: boolean | string;
59 > readonly buttons?: string[];
60 > }
61 >
62 > export interface IProgressNotificationOptions extends IProgressOptions {
63 > readonly location: ProgressLocation.Notification;
64 > readonly primaryActions?: readonly IAction[];
65 > readonly secondaryActions?: readonly IAction[];
66 > readonly delay?: number;
67 > readonly priority?: NotificationPriority;
68 > readonly type?: 'loading' | 'syncing';
69 > }
70 >
71 > export interface IProgressDialogOptions extends IProgressOptions {
72 > readonly delay?: number;
73 > readonly detail?: string;
74 > readonly sticky?: boolean;
75 > }
76 >
77 > export interface IProgressWindowOptions extends IProgressOptions {
78 > readonly location: ProgressLocation.Window;
79 > readonly command?: string;
80 > readonly type?: 'loading' | 'syncing';
81 > }
82 >
83 > export interface IProgressCompositeOptions extends IProgressOptions {
84 > readonly location: ProgressLocation.Explorer | ProgressLocation.Extensions | ProgressLocation.Scm | string;
85 > readonly delay?: number;
86 > }
87 >
88 > export interface IProgressStep {
89 > message?: string;
90 > increment?: number;
91 > total?: number;
92 > }
93 >
94 > export interface IProgressRunner {
95 > total(value: number): void;
96 > worked(value: number): void;
97 > done(): void;
98 > }
99 >
100 > export const emptyProgressRunner = Object.freeze<IProgressRunner>({
101 > total() { },
102 > worked() { },
103 > done() { }
104 > });
105 >
106 > export interface IProgress<T> {
107 > report(item: T): void;
108 > }
109 >
110 > export class Progress<T> implements IProgress<T> {
111 >
112 > static readonly None = Object.freeze<IProgress<unknown>>({ report() { } });
113 >
114 > private _value?: T;
115 > get value(): T | undefined { return this._value; }
116 >
117 > constructor(private callback: (data: T) => unknown) {
118 }
120 > report(item: T) {
121 this._value = item;
122 this.callback(this._value);
123 }
124 > } progress.ts ×9
125 >
126 > /**
127 > * A helper to show progress during a long running operation. If the operation
128 > * is started multiple times, only the last invocation will drive the progress.
129 > */
130 > export interface IOperation {
131 > id: number;
132 > isCurrent: () => boolean;
133 > token: CancellationToken;
134 > stop(): void;
135 > }
136 >
137 > /**
138 > * RAII-style progress instance that allows imperative reporting and hides
139 > * once `dispose()` is called.
140 > */
141 > export class UnmanagedProgress extends Disposable {
142 > private readonly deferred = new DeferredPromise<void>();
143 > private reporter?: IProgress<IProgressStep>;
144 > private lastStep?: IProgressStep;
145 >
146 > constructor(
147 options: IProgressOptions | IProgressDialogOptions | IProgressNotificationOptions | IProgressWindowOptions | IProgressCompositeOptions,
148 @IProgressService progressService: IProgressService,
149 ) {
150 super();
151 progressService.withProgress(options, reporter => {
152 this.reporter = reporter;
153 if (this.lastStep) {
154 reporter.report(this.lastStep);
155 }
156
157 return this.deferred.p;
158 });
159
160 this._register(toDisposable(() => this.deferred.complete()));
161 }
163 > report(step: IProgressStep) {
164 if (this.reporter) {
165 this.reporter.report(step);
166 } else {
167 this.lastStep = step;
168 }
169 }
170 > } progress.ts ×9
171 >
172 > export class LongRunningOperation extends Disposable {
173 > private currentOperationId = 0;
174 > private readonly currentOperationDisposables = this._register(new DisposableStore());
175 > private currentProgressRunner: IProgressRunner | undefined;
176 > private currentProgressTimeout: Timeout | undefined = undefined;
177 >
178 > constructor(
179 private progressIndicator: IProgressIndicator
180 ) {
181 super();
182 }
184 > start(progressDelay: number): IOperation {
185
186 // Stop any previous operation
187 this.stop();
188
189 // Start new
190 const newOperationId = ++this.currentOperationId;
191 const newOperationToken = new CancellationTokenSource();
192 this.currentProgressTimeout = setTimeout(() => {
193 if (newOperationId === this.currentOperationId) {
194 this.currentProgressRunner = this.progressIndicator.show(true);
195 }
196 }, progressDelay);
197
198 this.currentOperationDisposables.add(toDisposable(() => clearTimeout(this.currentProgressTimeout)));
199 this.currentOperationDisposables.add(toDisposable(() => newOperationToken.cancel()));
200 this.currentOperationDisposables.add(toDisposable(() => this.currentProgressRunner ? this.currentProgressRunner.done() : undefined));
201
202 return {
203 id: newOperationId,
204 token: newOperationToken.token,
205 stop: () => this.doStop(newOperationId),
206 isCurrent: () => this.currentOperationId === newOperationId
207 };
208 }
210 > stop(): void {
211 this.doStop(this.currentOperationId);
212 }
214 > private doStop(operationId: number): void {
215 if (this.currentOperationId === operationId) {
216 this.currentOperationDisposables.clear();
217 }
218 }
219 > } progress.ts ×9
220 >
221 > export const IEditorProgressService = createDecorator<IEditorProgressService>('editorProgressService');
222 >
223 > /**
224 > * A progress service that will report progress local to the editor triggered from.
225 > */
226 > export interface IEditorProgressService extends IProgressIndicator {
227 >
228 > readonly _serviceBrand: undefined;
229 > }