Skip to content

Commit 33f1242

Browse files
make applyCode flag synchronous
1 parent d30319a commit 33f1242

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type OnFileChangeCallback = (data: OnFileChangeData) => void;
4545
export type OnOperationAppliedCallback = (data: OnOperationAppliedData) => void;
4646

4747
export class ModelsHandler {
48+
public isApplyingOperation: boolean = false;
4849
private modelAddedListener: { dispose: Function };
4950
private modelRemovedListener: { dispose: Function };
5051
private onChangeCallback: OnFileChangeCallback;
@@ -122,8 +123,9 @@ export class ModelsHandler {
122123
);
123124
}
124125

126+
this.isApplyingOperation = true;
125127
this.applyOperationToModel(operation, false, model.textEditorModel);
126-
128+
this.isApplyingOperation = false;
127129
this.onOperationAppliedCallback({
128130
code: model.textEditorModel.getValue(),
129131
moduleShortid: module.shortid,
@@ -433,6 +435,10 @@ export class ModelsHandler {
433435

434436
private getModelContentChangeListener(sandbox: Sandbox, model) {
435437
return model.onDidChangeContent(e => {
438+
if (this.isApplyingOperation) {
439+
return;
440+
}
441+
436442
const { path } = model.uri;
437443
try {
438444
const module = resolveModule(

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class VSCodeEffect {
8787
private settings: Settings;
8888
private linter: Linter;
8989
private modelsHandler: ModelsHandler;
90-
private isApplyingCode: boolean = false;
90+
private isApplyingOperation: boolean = false;
9191
private modelSelectionListener: { dispose: Function };
9292
private readOnly: boolean;
9393
private elements = {
@@ -215,22 +215,22 @@ export class VSCodeEffect {
215215
moduleShortid: string,
216216
operation: (string | number)[]
217217
) {
218-
this.isApplyingCode = true;
219-
220218
try {
221219
await this.modelsHandler.applyOperation(moduleShortid, operation);
222220
} catch (error) {
223-
this.isApplyingCode = false;
224221
throw error;
225222
}
226-
this.isApplyingCode = false;
227223
}
228224

229225
public updateOptions(options: { readOnly: boolean }) {
230226
this.editorApi.getActiveCodeEditor().updateOptions(options);
231227
}
232228

233229
public updateUserSelections(userSelections: EditorSelection[]) {
230+
if (!this.modelsHandler) {
231+
return;
232+
}
233+
234234
this.modelsHandler.updateUserSelections(
235235
this.options.getCurrentModule(),
236236
userSelections
@@ -676,22 +676,17 @@ export class VSCodeEffect {
676676
currentModule.code !== undefined &&
677677
activeEditor.getValue() !== currentModule.code
678678
) {
679-
// Don't send these changes over live, since these changes can also be made by someone else and
680-
// we don't want to keep singing these changes
681-
// TODO: a better long term solution would be to store the changes of someone else in a model, even if the
682-
// model is not opened in an editor.
683-
684-
this.isApplyingCode = true;
685679
// This means that the file in Cerebral is dirty and has changed,
686680
// VSCode only gets saved contents. In this case we manually set the value correctly.
681+
this.modelsHandler.isApplyingOperation = true;
687682
const model = activeEditor.getModel();
688683
model.applyEdits([
689684
{
690685
text: currentModule.code,
691686
range: model.getFullModelRange(),
692687
},
693688
]);
694-
this.isApplyingCode = false;
689+
this.modelsHandler.isApplyingOperation = false;
695690
}
696691

697692
this.modelSelectionListener = activeEditor.onDidChangeCursorSelection(

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Live/index.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import React, { FunctionComponent } from 'react';
21
import { useOvermind } from 'app/overmind';
3-
4-
import LiveInfo from './LiveInfo';
5-
import LiveButton from './LiveButton';
2+
import React, { FunctionComponent } from 'react';
63

74
import {
85
Description,
6+
ErrorDescription,
97
WorkspaceInputContainer,
108
WorkspaceSubtitle,
11-
ErrorDescription,
129
} from '../../elements';
1310
import { More } from '../More';
11+
import LiveButton from './LiveButton';
12+
import LiveInfo from './LiveInfo';
1413

1514
export const Live: FunctionComponent = () => {
1615
const {
@@ -29,8 +28,7 @@ export const Live: FunctionComponent = () => {
2928
},
3029
editor: {
3130
isAllModulesSynced,
32-
currentSandbox: { owned },
33-
currentId,
31+
currentSandbox: { owned, id },
3432
},
3533
},
3634
actions: {
@@ -112,7 +110,7 @@ export const Live: FunctionComponent = () => {
112110
<LiveButton
113111
onClick={() => {
114112
createLiveClicked({
115-
sandboxId: currentId,
113+
sandboxId: id,
116114
});
117115
}}
118116
isLoading={isLoading}

0 commit comments

Comments
 (0)