Merge pull request #3227 from microsoft/hediet/workerManager

Refactors workerManager
pull/3228/head
Henning Dieterichs 3 years ago committed by GitHub
commit 9b2e90e2d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,8 +8,6 @@ import type { TypeScriptWorker } from './tsWorker';
import { editor, Uri, IDisposable } from '../../fillers/monaco-editor-core'; import { editor, Uri, IDisposable } from '../../fillers/monaco-editor-core';
export class WorkerManager { export class WorkerManager {
private _modeId: string;
private _defaults: LanguageServiceDefaults;
private _configChangeListener: IDisposable; private _configChangeListener: IDisposable;
private _updateExtraLibsToken: number; private _updateExtraLibsToken: number;
private _extraLibsChangeListener: IDisposable; private _extraLibsChangeListener: IDisposable;
@ -17,9 +15,10 @@ export class WorkerManager {
private _worker: editor.MonacoWebWorker<TypeScriptWorker> | null; private _worker: editor.MonacoWebWorker<TypeScriptWorker> | null;
private _client: Promise<TypeScriptWorker> | null; private _client: Promise<TypeScriptWorker> | null;
constructor(modeId: string, defaults: LanguageServiceDefaults) { constructor(
this._modeId = modeId; private readonly _modeId: string,
this._defaults = defaults; private readonly _defaults: LanguageServiceDefaults
) {
this._worker = null; this._worker = null;
this._client = null; this._client = null;
this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker()); this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker());
@ -29,6 +28,12 @@ export class WorkerManager {
); );
} }
dispose(): void {
this._configChangeListener.dispose();
this._extraLibsChangeListener.dispose();
this._stopWorker();
}
private _stopWorker(): void { private _stopWorker(): void {
if (this._worker) { if (this._worker) {
this._worker.dispose(); this._worker.dispose();
@ -37,12 +42,6 @@ export class WorkerManager {
this._client = null; this._client = null;
} }
dispose(): void {
this._configChangeListener.dispose();
this._extraLibsChangeListener.dispose();
this._stopWorker();
}
private async _updateExtraLibs(): Promise<void> { private async _updateExtraLibs(): Promise<void> {
if (!this._worker) { if (!this._worker) {
return; return;
@ -58,6 +57,7 @@ export class WorkerManager {
private _getClient(): Promise<TypeScriptWorker> { private _getClient(): Promise<TypeScriptWorker> {
if (!this._client) { if (!this._client) {
this._client = (async () => {
this._worker = editor.createWebWorker<TypeScriptWorker>({ this._worker = editor.createWebWorker<TypeScriptWorker>({
// module that exports the create() method and returns a `TypeScriptWorker` instance // module that exports the create() method and returns a `TypeScriptWorker` instance
moduleId: 'vs/language/typescript/tsWorker', moduleId: 'vs/language/typescript/tsWorker',
@ -75,39 +75,27 @@ export class WorkerManager {
} }
}); });
let p = <Promise<TypeScriptWorker>>this._worker.getProxy();
if (this._defaults.getEagerModelSync()) { if (this._defaults.getEagerModelSync()) {
p = p.then((worker) => { return await this._worker.withSyncedResources(
if (this._worker) {
return this._worker.withSyncedResources(
editor editor
.getModels() .getModels()
.filter((model) => model.getLanguageId() === this._modeId) .filter((model) => model.getLanguageId() === this._modeId)
.map((model) => model.uri) .map((model) => model.uri)
); );
} }
return worker;
});
}
this._client = p; return await this._worker.getProxy();
})();
} }
return this._client; return this._client;
} }
getLanguageServiceWorker(...resources: Uri[]): Promise<TypeScriptWorker> { async getLanguageServiceWorker(...resources: Uri[]): Promise<TypeScriptWorker> {
let _client: TypeScriptWorker; const client = await this._getClient();
return this._getClient()
.then((client) => {
_client = client;
})
.then((_) => {
if (this._worker) { if (this._worker) {
return this._worker.withSyncedResources(resources); await this._worker.withSyncedResources(resources);
} }
}) return client;
.then((_) => _client);
} }
} }

Loading…
Cancel
Save