UNPKG

4.29 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};
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.ProgressLocationService = void 0;
25const inversify_1 = require("inversify");
26const message_service_protocol_1 = require("../common/message-service-protocol");
27const promise_util_1 = require("../common/promise-util");
28const event_1 = require("../common/event");
29let ProgressLocationService = class ProgressLocationService {
30 constructor() {
31 this.emitters = new Map();
32 this.lastEvents = new Map();
33 this.progressByLocation = new Map();
34 }
35 getProgress(locationId) {
36 return this.lastEvents.get(locationId);
37 }
38 onProgress(locationId) {
39 const emitter = this.addEmitter(locationId);
40 return emitter.event;
41 }
42 addEmitter(locationId) {
43 const emitter = new event_1.Emitter();
44 const list = this.emitters.get(locationId) || [];
45 list.push(emitter);
46 this.emitters.set(locationId, list);
47 return emitter;
48 }
49 async showProgress(progressId, message, cancellationToken) {
50 const locationId = this.getLocationId(message);
51 const result = new promise_util_1.Deferred();
52 cancellationToken.onCancellationRequested(() => {
53 this.processEvent(progressId, locationId, 'done');
54 result.resolve(message_service_protocol_1.ProgressMessage.Cancel);
55 });
56 this.processEvent(progressId, locationId, 'start');
57 return result.promise;
58 }
59 processEvent(progressId, locationId, event) {
60 const progressSet = this.progressByLocation.get(locationId) || new Set();
61 if (event === 'start') {
62 progressSet.add(progressId);
63 }
64 else {
65 progressSet.delete(progressId);
66 }
67 this.progressByLocation.set(locationId, progressSet);
68 const show = !!progressSet.size;
69 this.fireEvent(locationId, show);
70 }
71 fireEvent(locationId, show) {
72 const lastEvent = this.lastEvents.get(locationId);
73 const shouldFire = !lastEvent || lastEvent.show !== show;
74 if (shouldFire) {
75 this.lastEvents.set(locationId, { show });
76 this.getOrCreateEmitters(locationId).forEach(e => e.fire({ show }));
77 }
78 }
79 getOrCreateEmitters(locationId) {
80 let emitters = this.emitters.get(locationId);
81 if (!emitters) {
82 emitters = [this.addEmitter(locationId)];
83 }
84 return emitters;
85 }
86 getLocationId(message) {
87 return message.options && message.options.location || 'unknownLocation';
88 }
89 async reportProgress(progressId, update, message, cancellationToken) {
90 /* NOOP */
91 }
92};
93ProgressLocationService = __decorate([
94 (0, inversify_1.injectable)()
95], ProgressLocationService);
96exports.ProgressLocationService = ProgressLocationService;
97//# sourceMappingURL=progress-location-service.js.map
\No newline at end of file