|
14 | 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
15 | 15 | ********************************************************************************/ |
16 | 16 |
|
17 | | -import { ContainerModule } from 'inversify'; |
| 17 | +import { ContainerModule, inject, injectable } from 'inversify'; |
18 | 18 | import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution'; |
19 | 19 | import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution'; |
| 20 | +import { MessageService, CommandRegistry, CommandContribution, Disposable, DisposableCollection } from '@theia/core'; |
| 21 | +import { OutputChannelManager, OutputChannel } from '@theia/output/lib/common/output-channel'; |
20 | 22 |
|
21 | 23 | export default new ContainerModule(bind => { |
22 | 24 | bindDynamicLabelProvider(bind); |
23 | 25 | bindSampleUnclosableView(bind); |
| 26 | + bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope(); |
24 | 27 | }); |
| 28 | + |
| 29 | +@injectable() |
| 30 | +class SampleOutputChannelsCommandContribution implements CommandContribution { |
| 31 | + |
| 32 | + @inject(OutputChannelManager) |
| 33 | + private readonly ocm: OutputChannelManager; |
| 34 | + |
| 35 | + @inject(MessageService) |
| 36 | + private readonly messageService: MessageService; |
| 37 | + |
| 38 | + private toDispose = new Map<string, Disposable>(); |
| 39 | + |
| 40 | + registerCommands(commands: CommandRegistry): void { |
| 41 | + for (const channelName of ['one', 'two', 'three']) { |
| 42 | + const command = { id: `post-date-now-${channelName}`, label: `API Sample: Post Date.now() to the '${channelName}' channel.` }; |
| 43 | + commands.registerCommand(command, { |
| 44 | + execute: () => { |
| 45 | + const toDisposePerChannel = this.toDispose.get(channelName); |
| 46 | + if (toDisposePerChannel) { |
| 47 | + toDisposePerChannel.dispose(); |
| 48 | + } else { |
| 49 | + const channel = this.getChannel(channelName); |
| 50 | + channel.setVisibility(true); |
| 51 | + const timer = window.setInterval(() => this.appendLineTo(channelName, Date.now()), 200); |
| 52 | + this.toDispose.set(channelName, new DisposableCollection( |
| 53 | + // eslint-disable-next-line max-len |
| 54 | + channel.onLockChange(({ locked }) => this.messageService.info(`API Sample: ${locked ? 'Locked' : 'Unlocked'} output channel: '${channelName}'.`), { timeout: 1000 }), |
| 55 | + Disposable.create(() => this.appendLineTo(channelName, 'User abort.')), |
| 56 | + Disposable.create(() => this.toDispose.delete(channelName)), |
| 57 | + Disposable.create(() => window.clearInterval(timer)) |
| 58 | + )); |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 66 | + private appendLineTo(channelName: string, what: any): void { |
| 67 | + this.getChannel(channelName).appendLine(`[${channelName}]: ${what}`); |
| 68 | + } |
| 69 | + |
| 70 | + private getChannel(channelName: string): OutputChannel { |
| 71 | + const channel = this.ocm.getChannel(channelName); |
| 72 | + if (channel) { |
| 73 | + return channel; |
| 74 | + } else { |
| 75 | + throw new Error(`Ouch. No channel was found with name: '${channelName}'.`); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments