mirror of
https://github.com/microsoft/vscode.git
synced 2026-09-13 05:01:08 +01:00
voice - better ports lifecycle
This commit is contained in:
@@ -7,12 +7,15 @@ import { Event } from 'vs/base/common/event';
|
||||
import { MessagePortMain, MessageEvent } from 'vs/base/parts/sandbox/node/electronTypes';
|
||||
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
|
||||
import { IVoiceRecognitionService } from 'vs/platform/voiceRecognition/common/voiceRecognitionService';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { CancellationTokenSource } from 'vs/base/common/cancellation';
|
||||
|
||||
export class VoiceTranscriber extends Disposable {
|
||||
|
||||
constructor(
|
||||
private readonly onDidWindowConnectRaw: Event<MessagePortMain>,
|
||||
@IVoiceRecognitionService private readonly voiceRecognitionService: IVoiceRecognitionService,
|
||||
@ILogService private readonly logService: ILogService
|
||||
) {
|
||||
super();
|
||||
|
||||
@@ -21,12 +24,17 @@ export class VoiceTranscriber extends Disposable {
|
||||
|
||||
private registerListeners(): void {
|
||||
this._register(this.onDidWindowConnectRaw(port => {
|
||||
this.logService.info(`[voice] transcriber: new connection`);
|
||||
|
||||
const cts = new CancellationTokenSource();
|
||||
this._register(toDisposable(() => cts.dispose(true)));
|
||||
|
||||
const portHandler = async (e: MessageEvent) => {
|
||||
if (!(e.data instanceof Float32Array)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await this.voiceRecognitionService.transcribe(e.data);
|
||||
const result = await this.voiceRecognitionService.transcribe(e.data, cts.token);
|
||||
|
||||
port.postMessage(result);
|
||||
};
|
||||
@@ -36,6 +44,12 @@ export class VoiceTranscriber extends Disposable {
|
||||
|
||||
port.start();
|
||||
this._register(toDisposable(() => port.close()));
|
||||
|
||||
port.on('close', () => {
|
||||
this.logService.info(`[voice] transcriber: closed connection`);
|
||||
|
||||
cts.dispose(true);
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user