From 93e5264bd6f8e01223b440e6513ea9175e2a5a29 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 14 Apr 2020 14:13:27 +0200 Subject: [PATCH] [drop-me]: Added an example. Signed-off-by: Akos Kitta --- .../browser/api-samples-frontend-module.ts | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/examples/api-samples/src/browser/api-samples-frontend-module.ts b/examples/api-samples/src/browser/api-samples-frontend-module.ts index afacf29ff68b5..52b546420e69c 100644 --- a/examples/api-samples/src/browser/api-samples-frontend-module.ts +++ b/examples/api-samples/src/browser/api-samples-frontend-module.ts @@ -14,11 +14,66 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { ContainerModule } from 'inversify'; +import { ContainerModule, inject, injectable } from 'inversify'; import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution'; import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution'; +import { MessageService, CommandRegistry, CommandContribution, Disposable, DisposableCollection } from '@theia/core'; +import { OutputChannelManager, OutputChannel } from '@theia/output/lib/common/output-channel'; export default new ContainerModule(bind => { bindDynamicLabelProvider(bind); bindSampleUnclosableView(bind); + bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope(); }); + +@injectable() +class SampleOutputChannelsCommandContribution implements CommandContribution { + + @inject(OutputChannelManager) + private readonly ocm: OutputChannelManager; + + @inject(MessageService) + private readonly messageService: MessageService; + + private toDispose = new Map(); + + registerCommands(commands: CommandRegistry): void { + for (const channelName of ['one', 'two', 'three']) { + const command = { id: `post-date-now-${channelName}`, label: `API Sample: Post Date.now() to the '${channelName}' channel.` }; + commands.registerCommand(command, { + execute: () => { + const toDisposePerChannel = this.toDispose.get(channelName); + if (toDisposePerChannel) { + toDisposePerChannel.dispose(); + } else { + const channel = this.getChannel(channelName); + channel.setVisibility(true); + const timer = window.setInterval(() => this.appendLineTo(channelName, Date.now()), 200); + this.toDispose.set(channelName, new DisposableCollection( + // eslint-disable-next-line max-len + channel.onLockChange(({ locked }) => this.messageService.info(`API Sample: ${locked ? 'Locked' : 'Unlocked'} output channel: '${channelName}'.`), { timeout: 1000 }), + Disposable.create(() => this.appendLineTo(channelName, 'User abort.')), + Disposable.create(() => this.toDispose.delete(channelName)), + Disposable.create(() => window.clearInterval(timer)) + )); + } + } + }); + } + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private appendLineTo(channelName: string, what: any): void { + this.getChannel(channelName).appendLine(`[${channelName}]: ${what}`); + } + + private getChannel(channelName: string): OutputChannel { + const channel = this.ocm.getChannel(channelName); + if (channel) { + return channel; + } else { + throw new Error(`Ouch. No channel was found with name: '${channelName}'.`); + } + } + +}