Repository navigation
Expand file tree
/
Copy pathprogressReporter.ts
More file actions
106 lines (93 loc) · 4.06 KB
/
Copy pathprogressReporter.ts
File metadata and controls
106 lines (93 loc) · 4.06 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { CancellationToken, CancellationTokenSource, Progress as VSCProgress, ProgressLocation } from 'vscode';
import { IApplicationShell } from '../../common/application/types';
import { IDisposable } from '../../common/types';
import { createDeferred } from '../../common/utils/async';
import { noop } from '../../common/utils/misc';
import { registerReporter } from './decorator';
import { getUserMessageForAction } from './messages';
import { IProgressReporter, Progress, ReportableAction } from './types';
@injectable()
export class ProgressReporter implements IProgressReporter {
private progressReporters: VSCProgress<{ message?: string | undefined; increment?: number | undefined }>[] = [];
private actionPhases = new Map<ReportableAction, 'started' | 'completed'>();
private currentActions: ReportableAction[] = [];
private get currentAction(): ReportableAction | undefined {
return this.currentActions.length === 0 ? undefined : this.currentActions[this.currentActions.length - 1];
}
constructor(@inject(IApplicationShell) private readonly appShell: IApplicationShell) {
registerReporter(this);
}
/**
* Create and display a progress indicator for starting of Jupyter Notebooks.
*
* @param {string} message
* @returns {(IDisposable & { token: CancellationToken })}
* @memberof ProgressReporter
*/
public createProgressIndicator(message: string): IDisposable & { token: CancellationToken } {
const cancellation = new CancellationTokenSource();
const deferred = createDeferred();
const options = { location: ProgressLocation.Notification, cancellable: false, title: message };
this.appShell
.withProgress(options, async (progress, cancelToken) => {
cancelToken.onCancellationRequested(() => {
if (!cancelToken.isCancellationRequested) {
cancellation.cancel();
}
deferred.resolve();
});
cancellation.token.onCancellationRequested(() => {
deferred.resolve();
});
this.progressReporters.push(progress);
await deferred.promise;
})
.then(noop, noop);
return {
token: cancellation.token,
dispose: () => deferred.resolve()
};
}
/**
* Reports progress to the user.
* Keep messages in a stack. As we have new actions taking place place them in a stack and notify progress.
* As they finish pop them from the stack (if currently displayed).
* We need a stack, as we have multiple async actions taking place, and each stat & can complete at different times.
*
* @param {Progress} progress
* @returns {void}
* @memberof JupyterStartupProgressReporter
*/
public report(progress: Progress): void {
if (this.progressReporters.length === 0) {
return;
}
this.actionPhases.set(progress.action, progress.phase);
if (progress.phase === 'started') {
this.currentActions.push(progress.action);
}
if (!this.currentAction) {
return;
}
// If current action has been completed, then pop that item.
// Until we have an action that is still in progress
while (this.actionPhases.get(this.currentAction) && this.actionPhases.get(this.currentAction) !== 'started') {
this.actionPhases.delete(this.currentAction);
this.currentActions.pop();
}
this.updateProgressMessage();
}
private updateProgressMessage() {
if (!this.currentAction || this.progressReporters.length === 0) {
return;
}
const message = getUserMessageForAction(this.currentAction);
if (message) {
this.progressReporters.forEach((item) => item.report({ message }));
}
}
}