Skip to content

Commit 56f8d5d

Browse files
committed
Fix saving code and missing operations
1 parent 6c14467 commit 56f8d5d

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ export default {
112112
`/sandboxes/${sandboxId}/modules/${moduleShortid}`
113113
);
114114
},
115-
saveModuleCode(sandboxId: string, module: Module): Promise<Module> {
115+
saveModuleCode(
116+
sandboxId: string,
117+
moduleShortid: string,
118+
code: string
119+
): Promise<Module> {
116120
return api
117121
.put<IModuleAPIResponse>(
118-
`/sandboxes/${sandboxId}/modules/${module.shortid}`,
122+
`/sandboxes/${sandboxId}/modules/${moduleShortid}`,
119123
{
120-
module: { code: module.code },
124+
module: { code },
121125
}
122126
)
123127
.then(transformModule);

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { convertAuthorizationToPermissionType } from 'app/utils/authorization';
3333
import { clearCorrectionsFromAction } from 'app/utils/corrections';
3434
import history from 'app/utils/history';
35+
import { getSavedCode } from 'app/overmind/utils/sandbox';
3536
import { Selection, TextOperation } from 'ot';
3637
import { json } from 'overmind';
3738

@@ -371,12 +372,14 @@ export const codeChanged: Action<{
371372
return;
372373
}
373374

374-
// module.code !== code check is there to make sure that we don't end up sending
375-
// duplicate updates to live. module.code === code only when VSCode detected a change
376-
// from the filesystem (fs changed, vscode sees it, sends update). If this happens we
377-
// never want to send that code update, since this actual code change goes through this
378-
// specific code flow already.
379-
if (state.live.isLive && module.code !== code) {
375+
const savedCode = getSavedCode(module.code, module.savedCode);
376+
const isSavedCode = savedCode === code;
377+
const isFirstChange = !effects.live.hasClient(module.shortid);
378+
379+
// Don't send saved code of a moduke that has not been registered with yet, since the server
380+
// will take the saved code as base. Which means that the change that would generate the saved code
381+
// would be applied on the saved code by the server.
382+
if (state.live.isLive && !(isSavedCode && isFirstChange)) {
380383
let operation: TextOperation;
381384
if (event) {
382385
logBreadcrumb({

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88
ServerContainerStatus,
99
TabType,
1010
} from '@codesandbox/common/lib/types';
11-
import {
12-
captureException,
13-
logBreadcrumb,
14-
} from '@codesandbox/common/lib/utils/analytics/sentry';
1511
import { hasPermission } from '@codesandbox/common/lib/utils/permission';
1612
import slugify from '@codesandbox/common/lib/utils/slugify';
1713
import {
@@ -106,14 +102,6 @@ export const setModuleSavedCode: Action<{
106102
if (moduleIndex > -1) {
107103
const module = state.editor.sandboxes[sandbox.id].modules[moduleIndex];
108104

109-
if (savedCode === undefined) {
110-
logBreadcrumb({
111-
type: 'error',
112-
message: `SETTING UNDEFINED SAVEDCODE FOR CODE: ${module.code}`,
113-
});
114-
captureException(new Error('SETTING UNDEFINED SAVEDCODE'));
115-
}
116-
117105
module.savedCode = module.code === savedCode ? null : savedCode;
118106
}
119107
};
@@ -137,27 +125,20 @@ export const saveCode: AsyncAction<{
137125
return;
138126
}
139127

140-
if (module.code !== code) {
141-
actions.editor.codeChanged({ moduleShortid, code });
142-
}
143-
144128
try {
145129
await effects.live.saveModule(module);
146-
const updatedModule = await effects.api.saveModuleCode(sandbox.id, module);
130+
const updatedModule = await effects.api.saveModuleCode(
131+
sandbox.id,
132+
module.shortid,
133+
code
134+
);
147135

148136
module.insertedAt = updatedModule.insertedAt;
149137
module.updatedAt = updatedModule.updatedAt;
150138
module.isBinary = updatedModule.isBinary;
151139

152140
const savedCode =
153141
updatedModule.code === module.code ? null : updatedModule.code;
154-
if (savedCode === undefined) {
155-
logBreadcrumb({
156-
type: 'error',
157-
message: `SETTING UNDEFINED SAVEDCODE FOR CODE: ${updatedModule.code}`,
158-
});
159-
captureException(new Error('SETTING UNDEFINED SAVEDCODE'));
160-
}
161142

162143
module.savedCode = savedCode;
163144

@@ -192,7 +173,12 @@ export const saveCode: AsyncAction<{
192173
}
193174

194175
if (state.live.isLive && state.live.isCurrentEditor) {
195-
effects.live.sendModuleSaved(module);
176+
setTimeout(() => {
177+
// Send the save event 50ms later so the operation can be sent first (the operation that says the
178+
// file difference created by VSCode due to the file watch event). If the other client gets the save before the operation,
179+
// the other client will also send an operation with the same difference resulting in a duplicate event.
180+
effects.live.sendModuleSaved(module);
181+
}, 50);
196182
}
197183

198184
await actions.editor.internal.updateCurrentTemplate();

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Operator } from 'app/overmind';
1313
import { camelizeKeys } from 'humps';
1414
import { json, mutate } from 'overmind';
1515
import { logError } from '@codesandbox/common/lib/utils/analytics';
16+
import { getSavedCode } from 'app/overmind/utils/sandbox';
1617

1718
export const onJoin: Operator<LiveMessage<{
1819
status: 'connected';
@@ -170,16 +171,17 @@ export const onModuleSaved: Operator<LiveMessage<{
170171
);
171172

172173
if (module) {
173-
module.isNotSynced = false;
174-
175174
actions.editor.internal.setModuleSavedCode({
176175
moduleShortid: data.moduleShortid,
177176
savedCode: data.module.savedCode,
178177
});
179178

180179
effects.vscode.sandboxFsSync.writeFile(state.editor.modulesByPath, module);
181-
// We revert the module so that VSCode will flag saved indication correctly
182-
effects.vscode.syncModule(module);
180+
const savedCode = getSavedCode(module.code, module.savedCode);
181+
if (module.code === savedCode) {
182+
// We revert the module so that VSCode will flag saved indication correctly
183+
effects.vscode.syncModule(module);
184+
}
183185
actions.editor.internal.updatePreviewCode();
184186
}
185187
});

0 commit comments

Comments
 (0)