浏览代码

Tightens the public API

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 {
+        /** A full HTTP path to a JavaScript file which adds a function `customTSWorkerFactory` to the self inside a web-worker */
         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));
 	}
 
-	_getModel(fileName: string): monaco.worker.IMirrorModel | null {
+	private _getModel(fileName: string): monaco.worker.IMirrorModel | null {
 		let models = this._ctx.getMirrorModels();
 		for (let i = 0; i < models.length; i++) {
 			if (models[i].uri.toString() === fileName) {
@@ -257,22 +257,28 @@ export interface ICreateData {
 	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 {
 	let TSWorkerClass = TypeScriptWorker
 	if (createData.customWorkerPath) {
-
 		// @ts-ignore - This is available in a webworker
 		if (typeof importScripts === "undefined") {
 			console.warn("Monaco is not using webworkers for background tasks, and that is needed to support the customWorkerPath flag")
 		} else {
 			// @ts-ignore - This is available in a webworker
 			importScripts(createData.customWorkerPath)
+
 			// @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`)
 			}
-			// @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
 // 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")
 
-/**
- *
- * @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 {
 
     // Adds a custom function to the webworker
@@ -59,7 +54,6 @@ const worker = (TypeScriptWorker, ts, libFileMap) => {
       recurse(mainSrcFile, 0)
       return miniAST
     }
-
   }
 }