Skip to content

Commit 565f83e

Browse files
committed
Fix saving the code
1 parent bf1c29d commit 565f83e

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,32 @@ export const onOperationApplied: Action<{
365365
}
366366
};
367367

368+
export const setCode: Action<{
369+
moduleShortid: string;
370+
code: string;
371+
}> = ({ effects, state, actions }, { code, moduleShortid }) => {
372+
if (!state.editor.currentSandbox) {
373+
return;
374+
}
375+
376+
const module = state.editor.currentSandbox.modules.find(
377+
m => m.shortid === moduleShortid
378+
);
379+
380+
if (!module) {
381+
return;
382+
}
383+
384+
// If the code is opened in VSCode, change the code in VSCode and let
385+
// other clients know via the event triggered by VSCode. Otherwise
386+
// send a manual event and just change the state.
387+
if (effects.vscode.isModuleOpened(module)) {
388+
effects.vscode.setModuleCode({ ...module, code }, true);
389+
} else {
390+
actions.editor.codeChanged({ moduleShortid, code });
391+
}
392+
};
393+
368394
export const codeChanged: Action<{
369395
moduleShortid: string;
370396
code: string;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export const saveCode: AsyncAction<{
169169
if (savedCode === null) {
170170
// If the savedCode is also module.code
171171
effects.moduleRecover.remove(sandbox.id, module);
172+
effects.vscode.syncModule(module);
172173
}
173174

174175
if (
@@ -305,8 +306,7 @@ export const removeNpmDependencyFromPackageJson: AsyncAction<string> = async (
305306
return;
306307
}
307308

308-
effects.vscode.setModuleCode({ ...module, code }, true);
309-
module.code = code;
309+
actions.editor.setCode({ moduleShortid, code });
310310

311311
await actions.editor.codeSaved({
312312
code,
@@ -347,8 +347,7 @@ export const addNpmDependencyToPackageJson: AsyncAction<{
347347
return;
348348
}
349349

350-
effects.vscode.setModuleCode({ ...module, code }, true);
351-
module.code = code;
350+
actions.editor.setCode({ moduleShortid, code });
352351

353352
await actions.editor.codeSaved({
354353
code,
@@ -542,8 +541,7 @@ export const updateSandboxPackageJson: AsyncAction = async ({
542541
return;
543542
}
544543

545-
effects.vscode.setModuleCode({ ...module, code }, true);
546-
module.code = code;
544+
actions.editor.setCode({ moduleShortid, code });
547545

548546
await actions.editor.codeSaved({
549547
code,

packages/app/src/app/overmind/namespaces/live/liveMessageOperators.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ export const onSave: Operator<LiveMessage<{
3535
return;
3636
}
3737

38-
module.savedCode = data.saved_code;
38+
module.savedCode = module.code === data.saved_code ? null : data.saved_code;
3939
module.updatedAt = data.updated_at;
4040
module.insertedAt = data.inserted_at;
4141
sandbox.version = data.version;
4242
effects.vscode.sandboxFsSync.writeFile(state.editor.modulesByPath, module);
43+
44+
if (module.savedCode === null) {
45+
effects.vscode.syncModule(module);
46+
}
4347
});
4448

4549
export const onJoin: Operator<LiveMessage<{

0 commit comments

Comments
 (0)