|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { ConsumedRecord, ConsumerChangedStatusEvent, ConsumerCollection, ConsumerCollectionChangedEvent, RecordReceivedEvent } from "../client"; |
| 3 | +import { OutputChannelProvider } from "../providers/outputChannelProvider"; |
| 4 | + |
| 5 | +class ConsumerOutput { |
| 6 | + private channel: vscode.OutputChannel; |
| 7 | + constructor(uri: vscode.Uri, registry: ConsumerOutputRegistry) { |
| 8 | + |
| 9 | + this.channel = registry.getChannel(`Kafka Consumer [${uri.path}]`); |
| 10 | + |
| 11 | + } |
| 12 | + |
| 13 | + public show(): void { |
| 14 | + this.channel.show(); |
| 15 | + } |
| 16 | + |
| 17 | + public sendText(data: string) { |
| 18 | + this.channel.appendLine(data); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export class ConsumerOutputRegistry implements vscode.Disposable { |
| 23 | + getChannel(name: string): vscode.OutputChannel { |
| 24 | + return this.channelProvider.getChannel(name); |
| 25 | + } |
| 26 | + |
| 27 | + private kafkaConsumers: { [id: string /* vscode URI */]: ConsumerOutput } = {}; |
| 28 | + private disposables: vscode.Disposable[] = []; |
| 29 | + |
| 30 | + constructor(private consumerCollection: ConsumerCollection, private channelProvider: OutputChannelProvider) { |
| 31 | + |
| 32 | + this.disposables.push(this.consumerCollection.onDidChangeCollection((event: ConsumerCollectionChangedEvent) => { |
| 33 | + for (const startedUri of event.created) { |
| 34 | + this.showConsumer(startedUri); |
| 35 | + this.onDidChangeStatus(startedUri, 'started'); |
| 36 | + this.attachToConsumer(startedUri); |
| 37 | + } |
| 38 | + |
| 39 | + for (const closedUri of event.closed) { |
| 40 | + this.onDidCloseConsumer(closedUri); |
| 41 | + } |
| 42 | + })); |
| 43 | + } |
| 44 | + |
| 45 | + private showConsumer(uri: vscode.Uri): void { |
| 46 | + let consumer = this.kafkaConsumers[uri.toString()]; |
| 47 | + if (!consumer) { |
| 48 | + consumer = new ConsumerOutput(uri, this); |
| 49 | + this.kafkaConsumers[uri.toString()] = consumer; |
| 50 | + consumer.show(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public dispose(): void { |
| 55 | + this.consumerCollection.dispose(); |
| 56 | + this.disposables.forEach(d => d.dispose()); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + private attachToConsumer(uri: vscode.Uri): void { |
| 61 | + const consumer = this.consumerCollection.get(uri); |
| 62 | + |
| 63 | + if (consumer === null) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + this.disposables.push(consumer.onDidReceiveRecord((arg: RecordReceivedEvent) => { |
| 68 | + this.onDidReceiveRecord(arg.uri, arg.record); |
| 69 | + })); |
| 70 | + |
| 71 | + this.disposables.push(consumer.onDidChangeStatus((arg: ConsumerChangedStatusEvent) => { |
| 72 | + this.onDidChangeStatus(arg.uri, arg.status); |
| 73 | + })); |
| 74 | + } |
| 75 | + |
| 76 | + private onDidChangeStatus(uri: vscode.Uri, status: string): void { |
| 77 | + const consumer = this.kafkaConsumers[uri.toString()]; |
| 78 | + if (consumer) { |
| 79 | + consumer.sendText(`Consumer: ${status}`); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private onDidReceiveRecord(uri: vscode.Uri, message: ConsumedRecord): void { |
| 84 | + const consumer = this.kafkaConsumers[uri.toString()]; |
| 85 | + if (consumer) { |
| 86 | + consumer.sendText(``); |
| 87 | + consumer.sendText(`Key: ${message.key}`); |
| 88 | + consumer.sendText(`Partition: ${message.partition}`); |
| 89 | + consumer.sendText(`Offset: ${message.offset}`); |
| 90 | + consumer.sendText(`Value:`); |
| 91 | + consumer.sendText(`${message.value}`); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private onDidCloseConsumer(uri: vscode.Uri): void { |
| 96 | + this.onDidChangeStatus(uri, 'closed'); |
| 97 | + } |
| 98 | + |
| 99 | + disposeConsumer(uri: vscode.Uri): void { |
| 100 | + delete this.kafkaConsumers[uri.toString()]; |
| 101 | + if (this.consumerCollection.has(uri)) { |
| 102 | + this.consumerCollection.close(uri); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments