Explorar o código

Adopt `createData` option when creatin a web worker

Alex Dima %!s(int64=9) %!d(string=hai) anos
pai
achega
7ba10ddbfb
Modificáronse 2 ficheiros con 26 adicións e 25 borrados
  1. 10 9
      src/worker.ts
  2. 16 16
      src/workerManager.ts

+ 10 - 9
src/worker.ts

@@ -24,17 +24,13 @@ export class TypeScriptWorker implements ts.LanguageServiceHost {
 
 	// --- model sync -----------------------
 
-	// private _models: { [uri: string]: MirrorModel2 } = Object.create(null);
 	private _extraLibs: { [fileName: string]: string } = Object.create(null);
 	private _languageService = ts.createLanguageService(this);
 	private _compilerOptions: ts.CompilerOptions;
 
-	// --- default ---------
-
-	acceptDefaults(options:ts.CompilerOptions, extraLibs:{ [path: string]: string }): Promise<void> {
-		this._compilerOptions = options;
-		this._extraLibs = extraLibs;
-		return;
+	constructor(createData:ICreateData) {
+		this._compilerOptions = createData.compilerOptions;
+		this._extraLibs = createData.extraLibs;
 	}
 
 	// --- language service host ---------------
@@ -176,6 +172,11 @@ export class TypeScriptWorker implements ts.LanguageServiceHost {
 	}
 }
 
-export function create(): TypeScriptWorker {
-	return new TypeScriptWorker();
+export interface ICreateData {
+	compilerOptions:ts.CompilerOptions;
+	extraLibs:{ [path: string]: string };
+}
+
+export function create(createData:ICreateData): TypeScriptWorker {
+	return new TypeScriptWorker(createData);
 }

+ 16 - 16
src/workerManager.ts

@@ -60,20 +60,18 @@ export class WorkerManager {
 
 		if (!this._client) {
 			this._worker = monaco.editor.createWebWorker<TypeScriptWorker>({
+
+				// module that exports the create() method and returns a `TypeScriptWorker` instance
 				moduleId: 'vs/language/typescript/src/worker',
+
+				// passed in to the create() method
+				createData: {
+					compilerOptions: this._defaults.compilerOptions,
+					extraLibs: this._defaults.extraLibs
+				}
 			});
 
-			let _client:TypeScriptWorker = null;
-
-			// avoid cancellation
-			this._client = toShallowCancelPromise(
-				this._worker.getProxy().then((client) => {
-					_client = client;
-				}).then(_ => {
-					const {compilerOptions, extraLibs} = this._defaults;
-					return _client.acceptDefaults(compilerOptions, extraLibs);
-				}).then(_ => _client)
-			);
+			this._client = this._worker.getProxy();
 		}
 
 		return this._client;
@@ -81,11 +79,13 @@ export class WorkerManager {
 
 	getLanguageServiceWorker(...resources: Uri[]): Promise<TypeScriptWorker> {
 		let _client:TypeScriptWorker;
-		return this._getClient().then((client) => {
-			_client = client
-		}).then(_ => {
-			return this._worker.withSyncedResources(resources)
-		}).then(_ => _client);
+		return toShallowCancelPromise(
+			this._getClient().then((client) => {
+				_client = client
+			}).then(_ => {
+				return this._worker.withSyncedResources(resources)
+			}).then(_ => _client)
+		);
 	}
 }