UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __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};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.ProgressService = void 0;
28const inversify_1 = require("inversify");
29const message_service_protocol_1 = require("./message-service-protocol");
30const cancellation_1 = require("./cancellation");
31const progress_service_protocol_1 = require("./progress-service-protocol");
32const message_service_1 = require("./message-service");
33let 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);
93ProgressService = __decorate([
94 (0, inversify_1.injectable)()
95], ProgressService);
96exports.ProgressService = ProgressService;
97//# sourceMappingURL=progress-service.js.map
\No newline at end of file