UNPKG

7.63 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 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};
26var __param = (this && this.__param) || function (paramIndex, decorator) {
27 return function (target, key) { decorator(target, key, paramIndex); }
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30exports.MessageService = void 0;
31const inversify_1 = require("inversify");
32const message_service_protocol_1 = require("./message-service-protocol");
33const cancellation_1 = require("./cancellation");
34/**
35 * Service to log and categorize messages, show progress information and offer actions.
36 *
37 * The messages are processed by this service and forwarded to an injected {@link MessageClient}.
38 * For example "@theia/messages" provides such a client, rendering messages as notifications
39 * in the frontend.
40 *
41 * ### Example usage
42 *
43 * ```typescript
44 * @inject(MessageService)
45 * protected readonly messageService: MessageService;
46 *
47 * messageService.warn("Typings not available");
48 *
49 * messageService.error("Could not restore state", ["Rollback", "Ignore"])
50 * .then(action => action === "Rollback" && rollback());
51 * ```
52 */
53let MessageService = class MessageService {
54 constructor(client) {
55 this.client = client;
56 this.progressIdPrefix = Math.random().toString(36).substring(5);
57 this.counter = 0;
58 }
59 // eslint-disable-next-line @typescript-eslint/no-explicit-any
60 log(message, ...args) {
61 return this.processMessage(message_service_protocol_1.MessageType.Log, message, args);
62 }
63 // eslint-disable-next-line @typescript-eslint/no-explicit-any
64 info(message, ...args) {
65 return this.processMessage(message_service_protocol_1.MessageType.Info, message, args);
66 }
67 // eslint-disable-next-line @typescript-eslint/no-explicit-any
68 warn(message, ...args) {
69 return this.processMessage(message_service_protocol_1.MessageType.Warning, message, args);
70 }
71 // eslint-disable-next-line @typescript-eslint/no-explicit-any
72 error(message, ...args) {
73 return this.processMessage(message_service_protocol_1.MessageType.Error, message, args);
74 }
75 // eslint-disable-next-line @typescript-eslint/no-explicit-any
76 processMessage(type, text, args) {
77 if (!!args && args.length > 0) {
78 const first = args[0];
79 const actions = Array.from(new Set(args.filter(a => typeof a === 'string')));
80 const options = (typeof first === 'object' && !Array.isArray(first))
81 ? first
82 : undefined;
83 return this.client.showMessage({ type, options, text, actions });
84 }
85 return this.client.showMessage({ type, text });
86 }
87 /**
88 * Shows the given message as a progress.
89 *
90 * @param message the message to show for the progress.
91 * @param onDidCancel an optional callback which will be invoked if the progress indicator was canceled.
92 *
93 * @returns a promise resolving to a {@link Progress} object with which the progress can be updated.
94 *
95 * ### Example usage
96 *
97 * ```typescript
98 * @inject(MessageService)
99 * protected readonly messageService: MessageService;
100 *
101 * // this will show "Progress" as a cancelable message
102 * this.messageService.showProgress({text: 'Progress'});
103 *
104 * // this will show "Rolling back" with "Cancel" and an additional "Skip" action
105 * this.messageService.showProgress({
106 * text: `Rolling back`,
107 * actions: ["Skip"],
108 * },
109 * () => console.log("canceled"))
110 * .then((progress) => {
111 * // register if interested in the result (only necessary for custom actions)
112 * progress.result.then((result) => {
113 * // will be 'Cancel', 'Skip' or `undefined`
114 * console.log("result is", result);
115 * });
116 * progress.report({message: "Cleaning references", work: {done: 10, total: 100}});
117 * progress.report({message: "Restoring previous state", work: {done: 80, total: 100}});
118 * progress.report({message: "Complete", work: {done: 100, total: 100}});
119 * // we are done so we can cancel the progress message, note that this will also invoke `onDidCancel`
120 * progress.cancel();
121 * });
122 * ```
123 */
124 async showProgress(message, onDidCancel) {
125 var _a;
126 const id = this.newProgressId();
127 const cancellationSource = new cancellation_1.CancellationTokenSource();
128 const report = (update) => {
129 this.client.reportProgress(id, update, message, cancellationSource.token);
130 };
131 const type = (_a = message.type) !== null && _a !== void 0 ? _a : message_service_protocol_1.MessageType.Progress;
132 const actions = new Set(message.actions);
133 if (message_service_protocol_1.ProgressMessage.isCancelable(message)) {
134 actions.delete(message_service_protocol_1.ProgressMessage.Cancel);
135 actions.add(message_service_protocol_1.ProgressMessage.Cancel);
136 }
137 const clientMessage = Object.assign(Object.assign({}, message), { type, actions: Array.from(actions) });
138 const result = this.client.showProgress(id, clientMessage, cancellationSource.token);
139 if (message_service_protocol_1.ProgressMessage.isCancelable(message) && typeof onDidCancel === 'function') {
140 result.then(value => {
141 if (value === message_service_protocol_1.ProgressMessage.Cancel) {
142 onDidCancel();
143 }
144 });
145 }
146 return {
147 id,
148 cancel: () => cancellationSource.cancel(),
149 result,
150 report
151 };
152 }
153 newProgressId() {
154 return `${this.progressIdPrefix}-${++this.counter}`;
155 }
156};
157MessageService = __decorate([
158 (0, inversify_1.injectable)(),
159 __param(0, (0, inversify_1.inject)(message_service_protocol_1.MessageClient)),
160 __metadata("design:paramtypes", [message_service_protocol_1.MessageClient])
161], MessageService);
162exports.MessageService = MessageService;
163//# sourceMappingURL=message-service.js.map
\No newline at end of file