Skip to content

Commit 71e408a

Browse files
fix error and corrections handling
1 parent 6b67dee commit 71e408a

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

packages/app/src/app/overmind/effects/vscode/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const context: any = window;
8585
*/
8686
export class VSCodeEffect {
8787
public initialized: Promise<unknown>;
88+
public editorInitialized = blocker<void>();
8889
public sandboxFsSync: SandboxFsSync;
8990
private mountableFilesystem: any;
9091

@@ -125,7 +126,6 @@ export class VSCodeEffect {
125126
getSignal: options.getSignal,
126127
};
127128
this.onSelectionChangeDebounced = debounce(options.onSelectionChange, 500);
128-
129129
this.prepareElements();
130130

131131
// We instantly create a sandbox sync, as we want our
@@ -455,7 +455,9 @@ export class VSCodeEffect {
455455
});
456456
}
457457

458-
public setErrors = (errors: ModuleError[]) => {
458+
public setErrors = async (errors: ModuleError[]) => {
459+
await this.editorInitialized.promise;
460+
459461
const activeEditor = this.editorApi.getActiveCodeEditor();
460462

461463
if (activeEditor) {
@@ -496,7 +498,9 @@ export class VSCodeEffect {
496498
}
497499
};
498500

499-
public setCorrections = (corrections: ModuleCorrection[]) => {
501+
public setCorrections = async (corrections: ModuleCorrection[]) => {
502+
await this.editorInitialized.promise;
503+
500504
const activeEditor = this.editorApi.getActiveCodeEditor();
501505
if (activeEditor) {
502506
if (corrections.length > 0) {
@@ -675,6 +679,10 @@ export class VSCodeEffect {
675679
}
676680

677681
private async loadEditor(monaco: any, container: HTMLElement) {
682+
if (this.editorInitialized.isResolved()) {
683+
return Promise.resolve();
684+
}
685+
678686
this.monaco = monaco;
679687
this.workbench = new Workbench(monaco, this.controller, this.runCommand);
680688

@@ -809,6 +817,8 @@ export class VSCodeEffect {
809817
if (this.settings.lintEnabled) {
810818
this.createLinter();
811819
}
820+
821+
this.editorInitialized.resolve();
812822
resolve();
813823
});
814824
});

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -566,24 +566,10 @@ export const prettifyClicked: AsyncAction = async ({
566566

567567
// TODO(@CompuIves): Look into whether we even want to call this function.
568568
// We can probably call the dispatch from the bundler itself instead.
569-
export const errorsCleared: Action = ({ state, effects }) => {
569+
export const errorsCleared: Action = ({ state }) => {
570570
const sandbox = state.editor.sandbox;
571-
if (!sandbox) {
572-
return;
573-
}
574571

575-
if (sandbox.errors.length) {
576-
sandbox.errors.forEach(error => {
577-
try {
578-
const module = sandbox.getModuleByPath(error.path);
579-
module.errors = [];
580-
} catch (e) {
581-
// Module is probably somewhere in eg. /node_modules which is not
582-
// in the store
583-
}
584-
});
585-
sandbox.clearErrors();
586-
}
572+
sandbox.clearErrors('*');
587573
};
588574

589575
export const toggleStatusBar: Action = ({ state }) => {
@@ -786,7 +772,7 @@ export const previewActionReceived: Action<any> = (
786772
const currentErrors = sandbox.errors;
787773
const newErrors = clearCorrectionsFromAction(currentErrors, action);
788774

789-
state.editor.sandbox.clearErrors();
775+
state.editor.sandbox.clearErrors(action.path);
790776
newErrors.forEach(error => sandbox.addError(error));
791777
effects.vscode.setErrors(sandbox.errors);
792778
break;
@@ -800,7 +786,7 @@ export const previewActionReceived: Action<any> = (
800786
action
801787
);
802788

803-
sandbox.clearCorrections();
789+
sandbox.clearCorrections(action.path);
804790
newCorrections.forEach(correction => sandbox.addCorrection(correction));
805791
effects.vscode.setCorrections(sandbox.corrections);
806792
break;

packages/app/src/app/overmind/namespaces/editor/models/EditorSandbox.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,38 @@ export class EditorSandbox {
464464
);
465465
}
466466

467-
clearErrors() {
468-
this.errors = [];
467+
clearErrors(path: string) {
468+
if (path === '*') {
469+
this.errors = [];
470+
this.currentSandbox.modules.forEach(module => {
471+
if (module.errors.length) {
472+
module.errors = [];
473+
}
474+
});
475+
} else {
476+
this.errors = this.errors.filter(error => error.path !== path);
477+
const module = this.getModuleByPath(path);
478+
if (module) {
479+
module.errors = [];
480+
}
481+
}
469482
}
470483

471-
clearCorrections() {
472-
this.corrections = [];
484+
clearCorrections(path: string) {
485+
if (path === '*') {
486+
this.corrections = [];
487+
this.currentSandbox.modules.forEach(module => {
488+
if (module.corrections.length) {
489+
module.corrections = [];
490+
}
491+
});
492+
} else {
493+
this.corrections = this.corrections.filter(error => error.path !== path);
494+
const module = this.getModuleByPath(path);
495+
if (module) {
496+
module.corrections = [];
497+
}
498+
}
473499
}
474500

475501
setFrozen(isFrozen: boolean) {

0 commit comments

Comments
 (0)