| 11 | const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min |
| 12 | |
| 13 | export class WorkerManager { |
| 14 | private _defaults: LanguageServiceDefaults; |
| 15 | private _idleCheckInterval: number; |
| 16 | private _lastUsedTime: number; |
| 17 | private _configChangeListener: IDisposable; |
| 18 | |
| 19 | private _worker: editor.MonacoWebWorker<HTMLWorker> | null; |
| 20 | private _client: Promise<HTMLWorker> | null; |
| 21 | |
| 22 | constructor(defaults: LanguageServiceDefaults) { |
| 23 | this._defaults = defaults; |
| 24 | this._worker = null; |
| 25 | this._client = null; |
| 26 | this._idleCheckInterval = window.setInterval(() => this._checkIfIdle(), 30 * 1000); |
| 27 | this._lastUsedTime = 0; |
| 28 | this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker()); |
| 29 | } |
| 30 | |
| 31 | private _stopWorker(): void { |
| 32 | if (this._worker) { |
| 33 | this._worker.dispose(); |
| 34 | this._worker = null; |
| 35 | } |
| 36 | this._client = null; |
| 37 | } |
| 38 | |
| 39 | dispose(): void { |
| 40 | clearInterval(this._idleCheckInterval); |
| 41 | this._configChangeListener.dispose(); |
| 42 | this._stopWorker(); |
| 43 | } |
| 44 | |
| 45 | private _checkIfIdle(): void { |
| 46 | if (!this._worker) { |
| 47 | return; |
| 48 | } |
| 49 | let timePassedSinceLastUsed = Date.now() - this._lastUsedTime; |
| 50 | if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) { |
| 51 | this._stopWorker(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private _getClient(): Promise<HTMLWorker> { |
| 56 | this._lastUsedTime = Date.now(); |
| 57 | |
| 58 | if (!this._client) { |
| 59 | this._worker = createWebWorker<HTMLWorker>({ |
| 60 | // module that exports the create() method and returns a `HTMLWorker` instance |
| 61 | moduleId: 'vs/language/html/htmlWorker', |
| 62 | createWorker: () => new Worker(new URL('./html.worker', import.meta.url), { type: 'module' }), |
| 63 | |
| 64 | // passed in to the create() method |
| 65 | createData: { |
| 66 | languageSettings: this._defaults.options, |
| 67 | languageId: this._defaults.languageId |
| 68 | }, |
| 69 | |
| 70 | label: this._defaults.languageId |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…