Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Allow to init and start separately
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Oct 31, 2023
1 parent f2f9f95 commit 6229bcd
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/monaco-editor-wrapper/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@ export class MonacoEditorLanguageClientWrapper {
private serviceConfig: InitializeServiceConfig;
private logger: Logger;

private init(userConfig: UserConfig) {
async init(userConfig: UserConfig) {
if (userConfig.wrapperConfig.editorAppConfig.useDiffEditor && !userConfig.wrapperConfig.editorAppConfig.codeOriginal) {
throw new Error('Use diff editor was used without a valid config.');
}
// Always dispose old instances before start
this.editorApp?.disposeApp();

this.id = userConfig.id ?? Math.floor(Math.random() * 101).toString();
this.logger = new Logger(userConfig.loggerConfig);
this.serviceConfig = userConfig.wrapperConfig.serviceConfig ?? {};

if (userConfig.wrapperConfig.editorAppConfig.$type === 'classic') {
this.editorApp = new EditorAppClassic(this.id, userConfig, this.logger);
} else {
this.editorApp = new EditorAppExtended(this.id, userConfig, this.logger);
}
// editorApps init their own service thats why they have to be created first
await this.initServices();

this.languageClientWrapper = new LanguageClientWrapper(this.editorApp.getConfig().languageId,
userConfig.languageClientConfig, this.logger);
}

private async initServices() {
Expand Down Expand Up @@ -68,27 +81,18 @@ export class MonacoEditorLanguageClientWrapper {
}

async start(userConfig: UserConfig, htmlElement: HTMLElement | null) {
await this.init(userConfig);
await this.startNoInit(htmlElement);
}

async startNoInit(htmlElement: HTMLElement | null) {
if (!htmlElement) {
throw new Error('No HTMLElement provided for monaco-editor.');
}
// Always dispose old instances before start
this.editorApp?.disposeApp();

this.init(userConfig);

if (userConfig.wrapperConfig.editorAppConfig.$type === 'classic') {
this.editorApp = new EditorAppClassic(this.id, userConfig, this.logger);
} else {
this.editorApp = new EditorAppExtended(this.id, userConfig, this.logger);
}
await this.initServices();

this.languageClientWrapper = new LanguageClientWrapper(this.editorApp.getConfig().languageId,
userConfig.languageClientConfig, this.logger);

this.logger.info(`Starting monaco-editor (${this.id})`);
await this.editorApp?.init();
await this.editorApp.createEditors(htmlElement);
await this.editorApp?.createEditors(htmlElement);

if (this.languageClientWrapper.haveLanguageClientConfig()) {
await this.languageClientWrapper.start();
Expand Down

0 comments on commit 6229bcd

Please sign in to comment.