forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.ts
More file actions
48 lines (41 loc) · 1.47 KB
/
decorator.ts
File metadata and controls
48 lines (41 loc) · 1.47 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { traceError } from '../../common/logger';
import { PromiseFunction } from '../types';
import { IProgressReporter, Progress, ReportableAction } from './types';
const _reporters = new Set<IProgressReporter>();
export function registerReporter(reporter: IProgressReporter) {
_reporters.add(reporter);
}
export function disposeRegisteredReporters() {
_reporters.clear();
}
function report(progress: Progress) {
try {
_reporters.forEach((item) => item.report(progress));
} catch (ex) {
traceError('Failed to report progress', ex);
}
}
/**
* Reports a user reportable action.
* Action may be logged or displayed to the user depending on the registered listeners.
*
* @export
* @param {ReportableAction} action
* @returns
*/
export function reportAction(action: ReportableAction) {
return function (_target: Object, _propertyName: string, descriptor: TypedPropertyDescriptor<PromiseFunction>) {
const originalMethod = descriptor.value!;
// tslint:disable-next-line:no-any no-function-expression
descriptor.value = async function (...args: any[]) {
report({ action, phase: 'started' });
// tslint:disable-next-line:no-invalid-this
return originalMethod.apply(this, args).finally(() => {
report({ action, phase: 'completed' });
});
};
};
}