From 9a9ef69e1f407743da8caf0c8bc6e4ec535c6f23 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 15 Sep 2023 14:05:57 +0200 Subject: [PATCH] voice - cleanup disposables (#193199) --- .../sharedProcess/contrib/voiceTranscriber.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts b/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts index 23a790e6109..6f29b8d3a23 100644 --- a/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts +++ b/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts @@ -67,13 +67,20 @@ abstract class VoiceTranscriber extends Disposable { this._register(toDisposable(() => this.port.off('message', requestHandler))); this.port.start(); - this._register(toDisposable(() => this.port.close())); + let closed = false; this.port.on('close', () => { this.logService.info(`[voice] transcriber: closed connection`); - cts.dispose(true); + closed = true; + this.dispose(); }); + + this._register(toDisposable(() => { + if (!closed) { + this.port.close(); + } + })); } protected abstract handleRequest(data: Float32Array, cancellation: CancellationToken): Promise; @@ -93,7 +100,7 @@ abstract class VoiceTranscriber extends Disposable { class SlidingWindowVoiceTranscriber extends VoiceTranscriber { - private readonly transcriptionQueue = new Queue(); + private readonly transcriptionQueue = this._register(new Queue()); private transcribedResults: string[] = []; private data: Float32Array = new Float32Array(0); @@ -103,7 +110,7 @@ class SlidingWindowVoiceTranscriber extends VoiceTranscriber { this.logService.info(`[voice] transcriber: voice detected, storing in buffer`); this.data = this.data ? this.joinFloat32Arrays([this.data, data]) : data; - } else if (this.data) { + } else { this.logService.info(`[voice] transcriber: silence detected, transcribing window...`); const data = this.data.slice(0); @@ -136,6 +143,12 @@ class SlidingWindowVoiceTranscriber extends VoiceTranscriber { this.port.postMessage(this.transcribedResults.join(' ')); } + + override dispose(): void { + super.dispose(); + + this.data = new Float32Array(0); + } } class FullWindowVoiceTranscriber extends VoiceTranscriber { @@ -190,4 +203,10 @@ class FullWindowVoiceTranscriber extends VoiceTranscriber { this.port.postMessage(result); } + + override dispose(): void { + super.dispose(); + + this.data = undefined; + } }