Fix Monaco initialisation race condition

This commit is contained in:
Sipke Schoorstra 2023-06-07 22:53:13 +02:00
parent afac92fd43
commit b43540d80c
2 changed files with 14 additions and 13 deletions

View file

@ -26,12 +26,13 @@ export class ElsaMonaco {
editor: any;
@Watch('language')
languageChangeHandler(newValue: string) {
async languageChangeHandler(newValue: string) {
if (!this.editor)
return;
const model = this.editor.getModel();
this.monaco.editor.setModelLanguage(model, this.language);
const monaco = await this.getMonaco();
monaco.editor.setModelLanguage(model, this.language);
}
@Method()
@ -45,7 +46,7 @@ export class ElsaMonaco {
@Method()
async addJavaScriptLib(libSource: string, libUri: string) {
const monaco = this.monaco;
const monaco = await this.getMonaco();
monaco.languages.typescript.javascriptDefaults.setExtraLibs([{
filePath: "lib.es5.d.ts"
@ -69,13 +70,8 @@ export class ElsaMonaco {
}
}
async componentWillLoad() {
const monacoLibPath = monacoStore.monacoLibPath;
this.monaco = await initializeMonacoWorker(monacoLibPath);
}
componentDidLoad() {
const monaco = this.monaco;
async componentDidLoad() {
const monaco = await this.getMonaco();
const language = this.language;
// Validation settings.
@ -162,6 +158,14 @@ export class ElsaMonaco {
}
}
private async getMonaco() {
if(!!this.monaco)
return this.monaco;
const monacoLibPath = monacoStore.monacoLibPath;
return this.monaco = await initializeMonacoWorker(monacoLibPath);
}
disconnectedCallback() {
const editor = this.editor;

View file

@ -26,7 +26,6 @@ export async function initializeMonacoWorker(libPath?: string): Promise<Monaco>
return await mutex.runExclusive(async () => {
if (isInitialized) {
return win.monaco;
}
@ -41,8 +40,6 @@ export async function initializeMonacoWorker(libPath?: string): Promise<Monaco>
const startTime = Date.now();
const timeout = 3000;
debugger;
const checkForMonaco = () => {
if (win.monaco) {
resolve(win.monaco);