/******************************************************************************** * Copyright (C) 2017 TypeFox and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ import { inject, injectable } from 'inversify'; import { MessageClient, MessageOptions, MessageType, Progress, ProgressMessage, ProgressUpdate } from './message-service-protocol'; import { CancellationTokenSource } from '@gedit/utils'; @injectable() export class MessageService { private progressIdPrefix = Math.random().toString(36).substring(5); private counter = 0; constructor( @inject(MessageClient) protected readonly client: MessageClient ) { } log(message: string, ...actions: T[]): Promise; log(message: string, options?: MessageOptions, ...actions: T[]): Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any log(message: string, ...args: any[]): Promise { return this.processMessage(MessageType.Log, message, args); } info(message: string, ...actions: T[]): Promise; info(message: string, options?: MessageOptions, ...actions: T[]): Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any info(message: string, ...args: any[]): Promise { return this.processMessage(MessageType.Info, message, args); } warn(message: string, ...actions: T[]): Promise; warn(message: string, options?: MessageOptions, ...actions: T[]): Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any warn(message: string, ...args: any[]): Promise { return this.processMessage(MessageType.Warning, message, args); } error(message: string, ...actions: T[]): Promise; error(message: string, options?: MessageOptions, ...actions: T[]): Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any error(message: string, ...args: any[]): Promise { return this.processMessage(MessageType.Error, message, args); } async showProgress(message: ProgressMessage, onDidCancel?: () => void): Promise { const id = this.newProgressId(); const cancellationSource = new CancellationTokenSource(); const report = (update: ProgressUpdate) => { this.client.reportProgress(id, update, message, cancellationSource.token); }; const actions = new Set(message.actions); if (ProgressMessage.isCancelable(message)) { actions.delete(ProgressMessage.Cancel); actions.add(ProgressMessage.Cancel); } const clientMessage = {...message, actions: Array.from(actions)}; const result = this.client.showProgress(id, clientMessage, cancellationSource.token); if (ProgressMessage.isCancelable(message) && typeof onDidCancel === 'function') { result.then(value => { if (value === ProgressMessage.Cancel) { onDidCancel(); } }); } return { id, cancel: () => cancellationSource.cancel(), result, report }; } // eslint-disable-next-line @typescript-eslint/no-explicit-any protected processMessage(type: MessageType, text: string, args?: any[]): Promise { if (!!args && args.length > 0) { const first = args[0]; const actions = Array.from(new Set(args.filter(a => typeof a === 'string'))); const options = (typeof first === 'object' && !Array.isArray(first)) ? first : undefined; return this.client.showMessage({type, options, text, actions}); } return this.client.showMessage({type, text}); } protected newProgressId(): string { return `${this.progressIdPrefix}-${++this.counter}`; } }