Skip to content

Commit 6c14467

Browse files
committed
Deduplicate handling operation errors
1 parent a607be7 commit 6c14467

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ type Options = {
3131
isLiveBlockerExperiement(): boolean;
3232
onOperationError(payload: {
3333
moduleShortid: string;
34-
code: string;
35-
revision: number;
36-
saved_code: string;
34+
moduleInfo: IModuleStateModule;
3735
}): void;
3836
};
3937

@@ -77,9 +75,7 @@ class Live {
7775
private provideJwtToken: () => string;
7876
private onOperationError: (payload: {
7977
moduleShortid: string;
80-
code: string;
81-
revision: number;
82-
saved_code: string;
78+
moduleInfo: IModuleStateModule;
8379
}) => void;
8480

8581
private operationToElixir(ot: (number | string)[]) {
@@ -167,7 +163,7 @@ class Live {
167163
captureException(error);
168164
if (error.module_state) {
169165
this.onOperationError({
170-
...error.module_state[moduleShortid],
166+
moduleInfo: error.module_state[moduleShortid],
171167
moduleShortid,
172168
});
173169
}

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,12 @@ export const signInToRoom: AsyncAction<{
2929

3030
export const onOperationError: Action<{
3131
moduleShortid: string;
32-
} & IModuleStateModule> = (
33-
{ state, effects },
34-
{ moduleShortid, revision, code, saved_code }
35-
) => {
36-
if (!state.editor.currentSandbox) {
37-
return;
38-
}
39-
effects.live.resetClient(moduleShortid, revision || 0);
40-
const module = state.editor.currentSandbox.modules.find(
41-
moduleItem => moduleItem.shortid === moduleShortid
42-
);
43-
44-
if (!module) {
45-
return;
46-
}
47-
48-
if (code !== undefined) {
49-
module.code = code;
50-
}
51-
if (saved_code !== undefined) {
52-
module.savedCode = saved_code;
53-
}
54-
55-
effects.vscode.setModuleCode(module);
32+
moduleInfo: IModuleStateModule;
33+
}> = ({ actions }, { moduleShortid, moduleInfo }) => {
34+
actions.live.internal.initializeModuleFromState({
35+
moduleShortid,
36+
moduleInfo,
37+
});
5638
};
5739

5840
export const roomJoined: AsyncAction<{

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

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,54 @@ interface IModuleState {
104104
[moduleId: string]: IModuleStateModule;
105105
}
106106

107+
export const initializeModuleFromState: Action<{
108+
moduleShortid: string;
109+
moduleInfo: IModuleStateModule;
110+
}> = ({ state, effects }, { moduleShortid, moduleInfo }) => {
111+
const sandbox = state.editor.currentSandbox;
112+
if (!sandbox) {
113+
return;
114+
}
115+
116+
// Module has not been saved, so is different
117+
const module = sandbox.modules.find(m => m.shortid === moduleShortid);
118+
119+
if (module) {
120+
effects.live.createClient(moduleShortid, moduleInfo.revision || 0);
121+
if (!('code' in moduleInfo)) {
122+
return;
123+
}
124+
125+
const savedCodeChanged =
126+
getSavedCode(moduleInfo.code, moduleInfo.saved_code) !==
127+
getSavedCode(module.code, module.savedCode);
128+
const moduleChanged =
129+
moduleInfo.code !== module.code ||
130+
moduleInfo.saved_code !== module.savedCode;
131+
132+
if (moduleChanged) {
133+
if (moduleInfo.saved_code !== undefined) {
134+
module.savedCode = moduleInfo.saved_code;
135+
}
136+
if (moduleInfo.code !== undefined) {
137+
module.code = moduleInfo.code;
138+
}
139+
140+
if (savedCodeChanged) {
141+
effects.vscode.sandboxFsSync.writeFile(
142+
state.editor.modulesByPath,
143+
module
144+
);
145+
}
146+
if (moduleInfo.synced) {
147+
effects.vscode.syncModule(module);
148+
} else {
149+
effects.vscode.setModuleCode(module);
150+
}
151+
}
152+
}
153+
};
154+
107155
export const initializeModuleState: Action<IModuleState> = (
108156
{ state, actions, effects },
109157
moduleState
@@ -118,44 +166,11 @@ export const initializeModuleState: Action<IModuleState> = (
118166
});
119167
Object.keys(moduleState).forEach(moduleShortid => {
120168
const moduleInfo = moduleState[moduleShortid];
121-
effects.live.createClient(moduleShortid, moduleInfo.revision || 0);
122169

123-
// Module has not been saved, so is different
124-
const module = sandbox.modules.find(m => m.shortid === moduleShortid);
125-
126-
if (module) {
127-
if (!('code' in moduleInfo)) {
128-
return;
129-
}
130-
131-
const savedCodeChanged =
132-
getSavedCode(moduleInfo.code, moduleInfo.saved_code) !==
133-
getSavedCode(module.code, module.savedCode);
134-
const moduleChanged =
135-
moduleInfo.code !== module.code ||
136-
moduleInfo.saved_code !== module.savedCode;
137-
138-
if (moduleChanged) {
139-
if (moduleInfo.saved_code !== undefined) {
140-
module.savedCode = moduleInfo.saved_code;
141-
}
142-
if (moduleInfo.code !== undefined) {
143-
module.code = moduleInfo.code;
144-
}
145-
146-
if (savedCodeChanged) {
147-
effects.vscode.sandboxFsSync.writeFile(
148-
state.editor.modulesByPath,
149-
module
150-
);
151-
}
152-
if (moduleInfo.synced) {
153-
effects.vscode.syncModule(module);
154-
} else {
155-
effects.vscode.setModuleCode(module);
156-
}
157-
}
158-
}
170+
actions.live.internal.initializeModuleFromState({
171+
moduleShortid,
172+
moduleInfo,
173+
});
159174
});
160175
// TODO: enable once we know exactly when we want to recover
161176
// actions.files.internal.recoverFiles();

0 commit comments

Comments
 (0)