|
| 1 | +import * as PQueue from 'p-queue'; |
| 2 | +import { injectable } from 'inversify'; |
| 3 | +import { Deferred } from '@theia/core/lib/common/promise-util'; |
| 4 | +import { OutputUri } from '@theia/output/lib/common/output-uri'; |
| 5 | +import { IReference } from '@theia/monaco/lib/browser/monaco-text-model-service'; |
| 6 | +import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model'; |
| 7 | +import { OutputChannelManager as TheiaOutputChannelManager, OutputChannel as TheiaOutputChannel } from '@theia/output/lib/common/output-channel'; |
| 8 | + |
| 9 | +@injectable() |
| 10 | +export class OutputChannelManager extends TheiaOutputChannelManager { |
| 11 | + |
| 12 | + getChannel(name: string): TheiaOutputChannel { |
| 13 | + const existing = this.channels.get(name); |
| 14 | + if (existing) { |
| 15 | + return existing; |
| 16 | + } |
| 17 | + |
| 18 | + // We have to register the resource first, because `textModelService#createModelReference` will require it |
| 19 | + // right after creating the monaco.editor.ITextModel. |
| 20 | + // All `append` and `appendLine` will be deferred until the underlying text-model instantiation. |
| 21 | + let resource = this.resources.get(name); |
| 22 | + if (!resource) { |
| 23 | + const uri = OutputUri.create(name); |
| 24 | + const editorModelRef = new Deferred<IReference<MonacoEditorModel>>(); |
| 25 | + resource = this.createResource({ uri, editorModelRef }); |
| 26 | + this.resources.set(name, resource); |
| 27 | + this.textModelService.createModelReference(uri).then(ref => editorModelRef.resolve(ref)); |
| 28 | + } |
| 29 | + |
| 30 | + const channel = new OutputChannel(resource, this.preferences); |
| 31 | + this.channels.set(name, channel); |
| 32 | + this.toDisposeOnChannelDeletion.set(name, this.registerListeners(channel)); |
| 33 | + this.channelAddedEmitter.fire(channel); |
| 34 | + if (!this.selectedChannel) { |
| 35 | + this.selectedChannel = channel; |
| 36 | + } |
| 37 | + return channel; |
| 38 | + } |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +export class OutputChannel extends TheiaOutputChannel { |
| 43 | + |
| 44 | + dispose(): void { |
| 45 | + super.dispose(); |
| 46 | + if ((this as any).disposed) { |
| 47 | + const textModifyQueue: PQueue = (this as any).textModifyQueue; |
| 48 | + textModifyQueue.pause(); |
| 49 | + textModifyQueue.clear(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments