Skip to content

Commit 8f4024d

Browse files
authored
Only send updates to non-optimistic modules in live (codesandbox#3463)
1 parent 907aaae commit 8f4024d

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import uuid from 'uuid';
1414
import { SandboxAPIResponse } from '../api/types';
1515
import { transformSandbox } from '../utils/sandbox';
1616
import clientsFactory from './clients';
17+
import { OPTIMISTIC_ID_PREFIX } from '../utils';
1718

1819
type Options = {
1920
onApplyOperation(args: { moduleShortid: string; operation: any }): void;
@@ -188,6 +189,12 @@ export default {
188189
return;
189190
}
190191

192+
if (moduleShortid.startsWith(OPTIMISTIC_ID_PREFIX)) {
193+
// Module is an optimistic module, we will send a full code update
194+
// once the module has been created, until then, send nothing!
195+
return;
196+
}
197+
191198
try {
192199
clients.get(moduleShortid).applyClient(operation);
193200
} catch (e) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { resolveModule } from '@codesandbox/common/lib/sandbox/modules';
22
import { isEqual } from 'lodash-es';
33

44
let nextOptimisticId = 0;
5+
export const OPTIMISTIC_ID_PREFIX = 'OPTIMISTIC_';
56

67
export default {
78
createOptimisticId() {
8-
return 'OPTIMISTIC_' + nextOptimisticId++;
9+
return OPTIMISTIC_ID_PREFIX + nextOptimisticId++;
910
},
1011
resolveModule,
1112
isEqual,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ export const getErrorMessage: Action<{ error: ApiError | Error }, string> = (
369369
const fields = Object.keys(result.errors);
370370
if (Array.isArray(errors)) {
371371
if (errors[0]) {
372+
if (fields[0] === 'detail') {
373+
return errors[0];
374+
}
372375
return `${fields[0]}: ${errors[0]}`; // eslint-disable-line no-param-reassign,prefer-destructuring
373376
}
374377
} else {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getModulesAndDirectoriesInDirectory,
55
} from '@codesandbox/common/lib/sandbox/modules';
66
import getDefinition from '@codesandbox/common/lib/templates';
7+
import { getTextOperation } from '@codesandbox/common/lib/utils/diff';
78
import { Directory, Module } from '@codesandbox/common/lib/types';
89
import { AsyncAction } from 'app/overmind';
910
import { withOwnedSandbox } from 'app/overmind/factories';
@@ -593,10 +594,16 @@ export const moduleCreated: AsyncAction<{
593594
);
594595
state.editor.currentModuleShortid = module.shortid;
595596

597+
effects.executor.updateFiles(state.editor.currentSandbox);
598+
596599
if (state.live.isCurrentEditor) {
597600
effects.live.sendModuleCreated(module);
601+
// Update server with latest data
602+
effects.live.sendCodeUpdate(
603+
module.shortid,
604+
getTextOperation('', module.code)
605+
);
598606
}
599-
effects.executor.updateFiles(state.editor.currentSandbox);
600607
} catch (error) {
601608
sandbox.modules.splice(sandbox.modules.indexOf(module), 1);
602609
actions.editor.internal.setCurrentModule(state.editor.mainModule);

0 commit comments

Comments
 (0)