Skip to content

Commit

Permalink
[drop-me]: Added an example.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta committed Apr 15, 2020
1 parent dd3590c commit fec6c49
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion examples/api-samples/src/browser/api-samples-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Disposable>();

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}'.`);
}
}

}

0 comments on commit fec6c49

Please sign in to comment.