Skip to content

Commit d3d20bf

Browse files
author
Akos Kitta
committed
[drop-me]: Added an example.
Signed-off-by: Akos Kitta <[email protected]>
1 parent 085067a commit d3d20bf

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

examples/api-samples/src/browser/api-samples-frontend-module.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,66 @@
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
1616

17-
import { ContainerModule } from 'inversify';
17+
import { ContainerModule, inject, injectable } from 'inversify';
1818
import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution';
1919
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';
2022

2123
export default new ContainerModule(bind => {
2224
bindDynamicLabelProvider(bind);
2325
bindSampleUnclosableView(bind);
26+
bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope();
2427
});
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

Comments
 (0)