Skip to content

Commit a0eb186

Browse files
author
Ives van Hoorne
committed
Alter saving logic to become more robust
1 parent 067f9fe commit a0eb186

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

packages/app/src/app/pages/Sandbox/Editor/Header/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Margin from 'common/components/spacing/Margin';
1919
import HeaderSearchBar from 'app/components/HeaderSearchBar';
2020
import UserMenu from 'app/pages/common/UserMenu';
2121
import theme from 'common/theme';
22+
import { saveAllModules } from 'app/store/modules/editor/utils';
2223

2324
import Logo from './Logo';
2425
import Action from './Action';
@@ -56,7 +57,7 @@ const Header = ({ store, signals }) => {
5657
onClick={
5758
store.editor.isAllModulesSynced
5859
? null
59-
: () => signals.editor.saveClicked()
60+
: () => saveAllModules(store, signals)
6061
}
6162
placeholder={
6263
store.editor.isAllModulesSynced ? 'All modules are saved' : false

packages/app/src/app/pages/Sandbox/Editor/Workspace/OpenedTabs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { inject, observer } from 'mobx-react';
44
import EntryIcons from 'app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons';
55
import getType from 'app/utils/get-type';
66
import { getModulePath } from 'common/sandbox/modules';
7+
import { saveAllModules } from 'app/store/modules/editor/utils';
78

89
import CrossIcon from 'react-icons/lib/md/clear';
910

@@ -35,7 +36,7 @@ const OpenedTabs = ({ store, signals }) => {
3536
e.preventDefault();
3637
e.stopPropagation();
3738
}
38-
signals.editor.saveClicked();
39+
saveAllModules(store, signals);
3940
}}
4041
/>
4142
}

packages/app/src/app/store/modules/editor/actions.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,6 @@ export function saveModuleCode({ props, state, api, recover }) {
397397
module: { code: codeToSave },
398398
})
399399
.then(x => {
400-
recover.remove(sandbox.id, moduleToSave);
401-
402-
if (x.code !== codeToSave) {
403-
throw new Error(
404-
`Something went wrong while saving the code of '${title}', please try again.`
405-
);
406-
}
407-
408400
const newSandbox = state.get('editor.currentSandbox');
409401
const newModuleToSave = sandbox.modules.find(
410402
module => module.shortid === props.moduleShortid
@@ -415,15 +407,19 @@ export function saveModuleCode({ props, state, api, recover }) {
415407
);
416408

417409
if (index > -1) {
418-
if (newModuleToSave.code === moduleToSave.code) {
410+
if (newModuleToSave.code === codeToSave) {
419411
state.set(
420412
`editor.sandboxes.${newSandbox.id}.modules.${index}.savedCode`,
421413
undefined
422414
);
415+
recover.remove(sandbox.id, moduleToSave);
423416
} else {
424417
state.set(
425418
`editor.sandboxes.${newSandbox.id}.modules.${index}.savedCode`,
426-
moduleToSave.code
419+
x.code
420+
);
421+
throw new Error(
422+
`The code of '${title}' changed while saving, will ignore the save now. Please try again with saving.`
427423
);
428424
}
429425
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function saveAllModules(store, signals) {
2+
const sandbox = store.editor.currentSandbox;
3+
4+
// In case you don't own the sandbox we cannot do >1 calls for saving module as you would then
5+
// also fork >1 times. The reason that we want to save seperately is because we want to have
6+
// fine-grained control of which saves succeed and which saves fail
7+
if (sandbox.owned) {
8+
sandbox.modules
9+
.filter(m => !store.editor.isModuleSynced(m.shortid))
10+
.forEach(module => {
11+
signals.editor.codeSaved({
12+
code: module.code,
13+
moduleShortid: module.shortid,
14+
});
15+
});
16+
} else {
17+
signals.saveClicked();
18+
}
19+
}

packages/app/src/app/utils/prettify.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,32 @@ export default function prettify(
8080
let timeout = setTimeout(() => {
8181
// If worker doesn't respond in time
8282
reject({ error: 'Prettify timeout' });
83+
timeout = null;
8384
}, 5000);
8485

85-
worker.onmessage = e => {
86+
const handler = e => {
8687
const { formatted, text, error } = e.data;
88+
8789
if (timeout) {
88-
clearTimeout(timeout);
89-
timeout = null;
9090
if (text === getCode()) {
91+
worker.removeEventListener('message', handler);
92+
clearTimeout(timeout);
93+
timeout = null;
94+
9195
if (error) {
9296
console.error(error);
9397
reject({ error });
9498
}
99+
95100
if (formatted) {
96101
resolve(formatted);
97102
}
98103
}
104+
} else {
105+
worker.removeEventListener('message', handler);
99106
}
100107
};
108+
109+
worker.addEventListener('message', handler);
101110
});
102111
}

0 commit comments

Comments
 (0)