Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getWorker to MonacoEnvironment #46032

Merged
merged 1 commit into from Mar 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/vs/base/worker/defaultWorkerFactory.ts
Expand Up @@ -7,17 +7,22 @@
import { globals } from 'vs/base/common/platform';
import { logOnceWebWorkerWarning, IWorker, IWorkerCallback, IWorkerFactory } from 'vs/base/common/worker/simpleWorker';

function getWorkerUrl(workerId: string, label: string): string {
// Option for hosts to overwrite the worker script url (used in the standalone editor)
if (globals.MonacoEnvironment && typeof globals.MonacoEnvironment.getWorkerUrl === 'function') {
return globals.MonacoEnvironment.getWorkerUrl(workerId, label);
function getWorker(workerId: string, label: string): Worker {
// Option for hosts to overwrite the worker script (used in the standalone editor)
if (globals.MonacoEnvironment) {
if (typeof globals.MonacoEnvironment.getWorker === 'function') {
return globals.MonacoEnvironment.getWorker(workerId, label);
}
if (typeof globals.MonacoEnvironment.getWorkerUrl === 'function') {
return new Worker(globals.MonacoEnvironment.getWorkerUrl(workerId, label));
}
}
// ESM-comment-begin
if (typeof require === 'function') {
return require.toUrl('./' + workerId) + '#' + label;
return new Worker(require.toUrl('./' + workerId) + '#' + label);
}
// ESM-comment-end
throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl`);
throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker`);
}

/**
Expand All @@ -31,7 +36,7 @@ class WebWorker implements IWorker {

constructor(moduleId: string, id: number, label: string, onMessageCallback: IWorkerCallback, onErrorCallback: (err: any) => void) {
this.id = id;
this.worker = new Worker(getWorkerUrl('workerMain.js', label));
this.worker = getWorker('workerMain.js', label);
this.postMessage(moduleId);
this.worker.onmessage = function (ev: any) {
onMessageCallback(ev.data);
Expand Down