import { LanguageServiceDefaults } from './_.contribution' ; import { editor , IDisposable , Uri } from './fillers/monaco-editor-core' ; const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000 ; export class WorkerManager < T > { private _defaults : LanguageServiceDefaults ; private _idleCheckInterval : number ; private _lastUsedTime : number ; private _configChangeListener : IDisposable ; private _worker : editor . MonacoWebWorker < T > | null ; private _client : Promise < T > | null | undefined ; constructor ( defaults ) { this . _defaults = defaults ; this . _worker = null ; this . _idleCheckInterval = window . setInterval ( ( ) => this . _checkIfIdle ( ) , 30 * 1000 ) ; this . _lastUsedTime = 0 ; this . _configChangeListener = this . _defaults . onDidChange ( ( ) => this . _stopWorker ( ) ) ; } private _stopWorker ( ) { if ( this . _worker ) { this . _worker . dispose ( ) ; this . _worker = null ; } this . _client = null ; } dispose ( ) { clearInterval ( this . _idleCheckInterval ) ; this . _configChangeListener . dispose ( ) ; this . _stopWorker ( ) ; } private _checkIfIdle ( ) { if ( ! this . _worker ) { return ; } let timePassedSinceLastUsed = Date . now ( ) - this . _lastUsedTime ; if ( timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR ) { this . _stopWorker ( ) ; } } private _getClient ( ) { this . _lastUsedTime = Date . now ( ) ; if ( ! this . _client ) { this . _worker = editor . createWebWorker < T > ( { moduleId : this . _defaults . languageId , label : this . _defaults . languageId , createData : { languageSettings : this . _defaults . diagnosticsOptions , languageId : this . _defaults . languageId } } ) ; this . _client = < Promise < T > > ( < any > this . _worker . getProxy ( ) ) ; } return this . _client ; } getLanguageServiceWorker ( ... resources ) { let _client ; return this . _getClient ( ) . then ( ( client ) => { _client = client ; } ) . then ( ( _ ) => { return this . _worker ?. withSyncedResources ( resources ) ; } ) . then ( ( _ ) => _client ) ; } }