UNPKG

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