1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
18 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
19 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
20 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
21 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
22 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.ProgressService = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const message_service_protocol_1 = require("./message-service-protocol");
|
30 | const cancellation_1 = require("./cancellation");
|
31 | const progress_service_protocol_1 = require("./progress-service-protocol");
|
32 | const message_service_1 = require("./message-service");
|
33 | let ProgressService = class ProgressService {
|
34 | constructor() {
|
35 | this.progressIdPrefix = Math.random().toString(36).substring(5);
|
36 | this.counter = 0;
|
37 | }
|
38 | async showProgress(message, onDidCancel) {
|
39 | if (this.shouldDelegate(message)) {
|
40 | return this.messageService.showProgress(message, onDidCancel);
|
41 | }
|
42 | const id = this.newProgressId();
|
43 | const cancellationSource = new cancellation_1.CancellationTokenSource();
|
44 | const report = (update) => {
|
45 | this.client.reportProgress(id, update, message, cancellationSource.token);
|
46 | };
|
47 | const actions = new Set(message.actions);
|
48 | if (message_service_protocol_1.ProgressMessage.isCancelable(message)) {
|
49 | actions.delete(message_service_protocol_1.ProgressMessage.Cancel);
|
50 | actions.add(message_service_protocol_1.ProgressMessage.Cancel);
|
51 | }
|
52 | const clientMessage = Object.assign(Object.assign({}, message), { actions: Array.from(actions) });
|
53 | const result = this.client.showProgress(id, clientMessage, cancellationSource.token);
|
54 | if (message_service_protocol_1.ProgressMessage.isCancelable(message) && typeof onDidCancel === 'function') {
|
55 | result.then(value => {
|
56 | if (value === message_service_protocol_1.ProgressMessage.Cancel) {
|
57 | onDidCancel();
|
58 | }
|
59 | });
|
60 | }
|
61 | return {
|
62 | id,
|
63 | cancel: () => cancellationSource.cancel(),
|
64 | result,
|
65 | report
|
66 | };
|
67 | }
|
68 | shouldDelegate(message) {
|
69 | const location = message.options && message.options.location;
|
70 | return location === 'notification';
|
71 | }
|
72 | newProgressId() {
|
73 | return `${this.progressIdPrefix}-${++this.counter}`;
|
74 | }
|
75 | async withProgress(text, locationId, task, onDidCancel) {
|
76 | const progress = await this.showProgress({ text, options: { cancelable: true, location: locationId } }, onDidCancel);
|
77 | try {
|
78 | return await task();
|
79 | }
|
80 | finally {
|
81 | progress.cancel();
|
82 | }
|
83 | }
|
84 | };
|
85 | __decorate([
|
86 | (0, inversify_1.inject)(progress_service_protocol_1.ProgressClient),
|
87 | __metadata("design:type", Object)
|
88 | ], ProgressService.prototype, "client", void 0);
|
89 | __decorate([
|
90 | (0, inversify_1.inject)(message_service_1.MessageService),
|
91 | __metadata("design:type", message_service_1.MessageService)
|
92 | ], ProgressService.prototype, "messageService", void 0);
|
93 | ProgressService = __decorate([
|
94 | (0, inversify_1.injectable)()
|
95 | ], ProgressService);
|
96 | exports.ProgressService = ProgressService;
|
97 |
|
\ | No newline at end of file |