Tightens the public API

pull/2748/head
Orta 5 years ago
parent 166e63b991
commit 5ee395c5ee

1
src/monaco.d.ts vendored

@ -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;
} }

@ -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)
} }
} }

@ -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")
/** /** @type { import("@typescript/vfs") } */
* const tsvfs = globalThis.tsvfs
* @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("../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
} }
} }
} }

Loading…
Cancel
Save