Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Tightens the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 31, 2020
1 parent 166e63b commit 18b0f5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 11 additions & 5 deletions src/tsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down
16 changes: 5 additions & 11 deletions test/custom-worker.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,7 +54,6 @@ const worker = (TypeScriptWorker, ts, libFileMap) => {
recurse(mainSrcFile, 0)
return miniAST
}

}
}

Expand Down

0 comments on commit 18b0f5d

Please sign in to comment.