mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 13:55:41 +01:00
Refactors workerManager
This commit is contained in:
parent
913ae66ad8
commit
4c5552d6a5
1 changed files with 39 additions and 51 deletions
|
|
@ -8,8 +8,6 @@ import type { TypeScriptWorker } from './tsWorker';
|
|||
import { editor, Uri, IDisposable } from '../../fillers/monaco-editor-core';
|
||||
|
||||
export class WorkerManager {
|
||||
private _modeId: string;
|
||||
private _defaults: LanguageServiceDefaults;
|
||||
private _configChangeListener: IDisposable;
|
||||
private _updateExtraLibsToken: number;
|
||||
private _extraLibsChangeListener: IDisposable;
|
||||
|
|
@ -17,9 +15,10 @@ export class WorkerManager {
|
|||
private _worker: editor.MonacoWebWorker<TypeScriptWorker> | null;
|
||||
private _client: Promise<TypeScriptWorker> | null;
|
||||
|
||||
constructor(modeId: string, defaults: LanguageServiceDefaults) {
|
||||
this._modeId = modeId;
|
||||
this._defaults = defaults;
|
||||
constructor(
|
||||
private readonly _modeId: string,
|
||||
private readonly _defaults: LanguageServiceDefaults
|
||||
) {
|
||||
this._worker = null;
|
||||
this._client = null;
|
||||
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 {
|
||||
if (this._worker) {
|
||||
this._worker.dispose();
|
||||
|
|
@ -37,12 +42,6 @@ export class WorkerManager {
|
|||
this._client = null;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this._configChangeListener.dispose();
|
||||
this._extraLibsChangeListener.dispose();
|
||||
this._stopWorker();
|
||||
}
|
||||
|
||||
private async _updateExtraLibs(): Promise<void> {
|
||||
if (!this._worker) {
|
||||
return;
|
||||
|
|
@ -58,56 +57,45 @@ export class WorkerManager {
|
|||
|
||||
private _getClient(): Promise<TypeScriptWorker> {
|
||||
if (!this._client) {
|
||||
this._worker = editor.createWebWorker<TypeScriptWorker>({
|
||||
// module that exports the create() method and returns a `TypeScriptWorker` instance
|
||||
moduleId: 'vs/language/typescript/tsWorker',
|
||||
this._client = (async () => {
|
||||
this._worker = editor.createWebWorker<TypeScriptWorker>({
|
||||
// module that exports the create() method and returns a `TypeScriptWorker` instance
|
||||
moduleId: 'vs/language/typescript/tsWorker',
|
||||
|
||||
label: this._modeId,
|
||||
label: this._modeId,
|
||||
|
||||
keepIdleModels: true,
|
||||
keepIdleModels: true,
|
||||
|
||||
// passed in to the create() method
|
||||
createData: {
|
||||
compilerOptions: this._defaults.getCompilerOptions(),
|
||||
extraLibs: this._defaults.getExtraLibs(),
|
||||
customWorkerPath: this._defaults.workerOptions.customWorkerPath,
|
||||
inlayHintsOptions: this._defaults.inlayHintsOptions
|
||||
}
|
||||
});
|
||||
|
||||
let p = <Promise<TypeScriptWorker>>this._worker.getProxy();
|
||||
|
||||
if (this._defaults.getEagerModelSync()) {
|
||||
p = p.then((worker) => {
|
||||
if (this._worker) {
|
||||
return this._worker.withSyncedResources(
|
||||
editor
|
||||
.getModels()
|
||||
.filter((model) => model.getLanguageId() === this._modeId)
|
||||
.map((model) => model.uri)
|
||||
);
|
||||
// passed in to the create() method
|
||||
createData: {
|
||||
compilerOptions: this._defaults.getCompilerOptions(),
|
||||
extraLibs: this._defaults.getExtraLibs(),
|
||||
customWorkerPath: this._defaults.workerOptions.customWorkerPath,
|
||||
inlayHintsOptions: this._defaults.inlayHintsOptions
|
||||
}
|
||||
return worker;
|
||||
});
|
||||
}
|
||||
|
||||
this._client = p;
|
||||
if (this._defaults.getEagerModelSync()) {
|
||||
return await this._worker.withSyncedResources(
|
||||
editor
|
||||
.getModels()
|
||||
.filter((model) => model.getLanguageId() === this._modeId)
|
||||
.map((model) => model.uri)
|
||||
);
|
||||
}
|
||||
|
||||
return await this._worker.getProxy();
|
||||
})();
|
||||
}
|
||||
|
||||
return this._client;
|
||||
}
|
||||
|
||||
getLanguageServiceWorker(...resources: Uri[]): Promise<TypeScriptWorker> {
|
||||
let _client: TypeScriptWorker;
|
||||
return this._getClient()
|
||||
.then((client) => {
|
||||
_client = client;
|
||||
})
|
||||
.then((_) => {
|
||||
if (this._worker) {
|
||||
return this._worker.withSyncedResources(resources);
|
||||
}
|
||||
})
|
||||
.then((_) => _client);
|
||||
async getLanguageServiceWorker(...resources: Uri[]): Promise<TypeScriptWorker> {
|
||||
const client = await this._getClient();
|
||||
if (this._worker) {
|
||||
await this._worker.withSyncedResources(resources);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue