Orta 4 роки тому
батько
коміт
5ee395c5ee
3 змінених файлів з 17 додано та 16 видалено
  1. 1 0
      src/monaco.d.ts
  2. 11 5
      src/tsWorker.ts
  3. 5 11
      test/custom-worker.js

+ 1 - 0
src/monaco.d.ts

@@ -138,6 +138,7 @@ declare module monaco.languages.typescript {
     }
     }
 
 
     export interface WorkerOptions {
     export interface WorkerOptions {
+        /** A full HTTP path to a JavaScript file which adds a function `customTSWorkerFactory` to the self inside a web-worker */
         customWorkerPath?: string;
         customWorkerPath?: string;
     }
     }
 
 

+ 11 - 5
src/tsWorker.ts

@@ -37,7 +37,7 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, monaco.language
 		return models.concat(Object.keys(this._extraLibs));
 		return models.concat(Object.keys(this._extraLibs));
 	}
 	}
 
 
-	_getModel(fileName: string): monaco.worker.IMirrorModel | null {
+	private _getModel(fileName: string): monaco.worker.IMirrorModel | null {
 		let models = this._ctx.getMirrorModels();
 		let models = this._ctx.getMirrorModels();
 		for (let i = 0; i < models.length; i++) {
 		for (let i = 0; i < models.length; i++) {
 			if (models[i].uri.toString() === fileName) {
 			if (models[i].uri.toString() === fileName) {
@@ -257,22 +257,28 @@ export interface ICreateData {
 	customWorkerPath?: string
 	customWorkerPath?: string
 }
 }
 
 
+/** The shape of the factory */
+export interface CustomTSWebWorkerFactory {
+	(TSWorkerClass: typeof TypeScriptWorker, ts: typeof import("typescript"), libs: Record<string, string>): typeof TypeScriptWorker
+}
+
 export function create(ctx: IWorkerContext, createData: ICreateData): TypeScriptWorker {
 export function create(ctx: IWorkerContext, createData: ICreateData): TypeScriptWorker {
 	let TSWorkerClass = TypeScriptWorker
 	let TSWorkerClass = TypeScriptWorker
 	if (createData.customWorkerPath) {
 	if (createData.customWorkerPath) {
-
 		// @ts-ignore - This is available in a webworker
 		// @ts-ignore - This is available in a webworker
 		if (typeof importScripts === "undefined") {
 		if (typeof importScripts === "undefined") {
 			console.warn("Monaco is not using webworkers for background tasks, and that is needed to support the customWorkerPath flag")
 			console.warn("Monaco is not using webworkers for background tasks, and that is needed to support the customWorkerPath flag")
 		} else {
 		} else {
 			// @ts-ignore - This is available in a webworker
 			// @ts-ignore - This is available in a webworker
 			importScripts(createData.customWorkerPath)
 			importScripts(createData.customWorkerPath)
+
 			// @ts-ignore - This should come from the above eval
 			// @ts-ignore - This should come from the above eval
-			if (!self.customTSWorkerFactory) {
+			const workerFactoryFunc: CustomTSWebWorkerFactory | undefined = self.customTSWorkerFactory
+			if (!workerFactoryFunc) {
 				throw new Error(`The script at ${createData.customWorkerPath} does not add customTSWorkerFactory to self`)
 				throw new Error(`The script at ${createData.customWorkerPath} does not add customTSWorkerFactory to self`)
 			}
 			}
-			// @ts-ignore - The throw validates this
-			TSWorkerClass = self.customTSWorkerFactory(TypeScriptWorker, ts, libFileMap)
+
+			TSWorkerClass = workerFactoryFunc(TypeScriptWorker, ts, libFileMap)
 		}
 		}
 	}
 	}
 
 

+ 5 - 11
test/custom-worker.js

@@ -1,19 +1,14 @@
 // This example uses @typescript/vfs to create a virtual TS program
 // This example uses @typescript/vfs to create a virtual TS program
 // which can do work on a bg thread.
 // which can do work on a bg thread.
 
 
+// This version of the vfs edits the global scope (in the case of a webworker, this is 'self')
 importScripts("https://unpkg.com/@typescript/vfs@1.3.0/dist/vfs.globals.js")
 importScripts("https://unpkg.com/@typescript/vfs@1.3.0/dist/vfs.globals.js")
 
 
-/**
- *
- * @param {import("../src/tsWorker").TypeScriptWorker} TypeScriptWorker
- * @param {import("typescript")} ts
- * @param {Record<string, string>} libFileMap
- *
- */
-const worker = (TypeScriptWorker, ts, libFileMap) => {
-  /** @type { import("@typescript/vfs") } */
-  const tsvfs = globalThis.tsvfs
+/** @type { import("@typescript/vfs") } */
+const tsvfs = globalThis.tsvfs
 
 
+/** @type {import("../src/tsWorker").CustomTSWebWorkerFactory }*/
+const worker = (TypeScriptWorker, ts, libFileMap) => {
   return class MonacoTSWorker extends TypeScriptWorker {
   return class MonacoTSWorker extends TypeScriptWorker {
 
 
     // Adds a custom function to the webworker
     // Adds a custom function to the webworker
@@ -59,7 +54,6 @@ const worker = (TypeScriptWorker, ts, libFileMap) => {
       recurse(mainSrcFile, 0)
       recurse(mainSrcFile, 0)
       return miniAST
       return miniAST
     }
     }
-
   }
   }
 }
 }