UNPKG

@codingame/monaco-vscode-extensions-service-override

Version:

VSCode public API plugged on the monaco editor - extensions service-override

134 lines (131 loc) 6.4 kB
import { __decorate, __param } from 'vscode/external/tslib/tslib.es6.js'; import { raceCancellation } from 'vscode/vscode/vs/base/common/async'; import { Event, Emitter } from 'vscode/vscode/vs/base/common/event'; import { DisposableStore } from 'vscode/vscode/vs/base/common/lifecycle'; import { ILogService } from 'vscode/vscode/vs/platform/log/common/log.service'; import { ExtHostContext, MainContext } from 'vscode/vscode/vs/workbench/api/common/extHost.protocol'; import { TextToSpeechStatus } from 'vscode/vscode/vs/workbench/contrib/speech/common/speechService'; import { ISpeechService } from 'vscode/vscode/vs/workbench/contrib/speech/common/speechService.service'; import { extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js'; let MainThreadSpeech = class MainThreadSpeech { constructor(extHostContext, speechService, logService) { this.speechService = speechService; this.logService = logService; this.providerRegistrations = ( new Map()); this.speechToTextSessions = ( new Map()); this.textToSpeechSessions = ( new Map()); this.keywordRecognitionSessions = ( new Map()); this.proxy = ( extHostContext.getProxy(ExtHostContext.ExtHostSpeech)); } $registerProvider(handle, identifier, metadata) { this.logService.trace('[Speech] extension registered provider', metadata.extension.value); const registration = this.speechService.registerSpeechProvider(identifier, { metadata, createSpeechToTextSession: (token, options) => { if (token.isCancellationRequested) { return { onDidChange: Event.None }; } const disposables = ( new DisposableStore()); const session = Math.random(); this.proxy.$createSpeechToTextSession(handle, session, options?.language); const onDidChange = disposables.add(( new Emitter())); this.speechToTextSessions.set(session, { onDidChange }); disposables.add(token.onCancellationRequested(() => { this.proxy.$cancelSpeechToTextSession(session); this.speechToTextSessions.delete(session); disposables.dispose(); })); return { onDidChange: onDidChange.event }; }, createTextToSpeechSession: (token, options) => { if (token.isCancellationRequested) { return { onDidChange: Event.None, synthesize: async () => { } }; } const disposables = ( new DisposableStore()); const session = Math.random(); this.proxy.$createTextToSpeechSession(handle, session, options?.language); const onDidChange = disposables.add(( new Emitter())); this.textToSpeechSessions.set(session, { onDidChange }); disposables.add(token.onCancellationRequested(() => { this.proxy.$cancelTextToSpeechSession(session); this.textToSpeechSessions.delete(session); disposables.dispose(); })); return { onDidChange: onDidChange.event, synthesize: async (text) => { await this.proxy.$synthesizeSpeech(session, text); await raceCancellation(Event.toPromise(Event.filter(onDidChange.event, e => e.status === TextToSpeechStatus.Stopped)), token); } }; }, createKeywordRecognitionSession: token => { if (token.isCancellationRequested) { return { onDidChange: Event.None }; } const disposables = ( new DisposableStore()); const session = Math.random(); this.proxy.$createKeywordRecognitionSession(handle, session); const onDidChange = disposables.add(( new Emitter())); this.keywordRecognitionSessions.set(session, { onDidChange }); disposables.add(token.onCancellationRequested(() => { this.proxy.$cancelKeywordRecognitionSession(session); this.keywordRecognitionSessions.delete(session); disposables.dispose(); })); return { onDidChange: onDidChange.event }; } }); this.providerRegistrations.set(handle, { dispose: () => { registration.dispose(); } }); } $unregisterProvider(handle) { const registration = this.providerRegistrations.get(handle); if (registration) { registration.dispose(); this.providerRegistrations.delete(handle); } } $emitSpeechToTextEvent(session, event) { const providerSession = this.speechToTextSessions.get(session); providerSession?.onDidChange.fire(event); } $emitTextToSpeechEvent(session, event) { const providerSession = this.textToSpeechSessions.get(session); providerSession?.onDidChange.fire(event); } $emitKeywordRecognitionEvent(session, event) { const providerSession = this.keywordRecognitionSessions.get(session); providerSession?.onDidChange.fire(event); } dispose() { this.providerRegistrations.forEach(disposable => disposable.dispose()); this.providerRegistrations.clear(); this.speechToTextSessions.forEach(session => session.onDidChange.dispose()); this.speechToTextSessions.clear(); this.textToSpeechSessions.forEach(session => session.onDidChange.dispose()); this.textToSpeechSessions.clear(); this.keywordRecognitionSessions.forEach(session => session.onDidChange.dispose()); this.keywordRecognitionSessions.clear(); } }; MainThreadSpeech = __decorate([ extHostNamedCustomer(MainContext.MainThreadSpeech), ( __param(1, ISpeechService)), ( __param(2, ILogService)) ], MainThreadSpeech); export { MainThreadSpeech };