@@ -87,10 +87,6 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
8787 state . editor . isLoading = ! hasExistingSandbox ;
8888 state . editor . notFound = false ;
8989
90- // Only reset changed modules if sandbox wasn't in memory, otherwise a fork
91- // can mark real changed modules as unchanged
92- state . editor . changedModuleShortids = [ ] ;
93-
9490 try {
9591 const sandbox = await effects . api . getSandbox ( newId ) ;
9692
@@ -232,17 +228,42 @@ export const codeChanged: Action<{
232228export const saveClicked : AsyncAction = withOwnedSandbox (
233229 async ( { state, effects, actions } ) => {
234230 const sandbox = state . editor . currentSandbox ;
235- const currentlyChangedModuleShortids = state . editor . changedModuleShortids . slice ( ) ;
236231
237232 try {
238233 const changedModules = sandbox . modules . filter ( module =>
239234 state . editor . changedModuleShortids . includes ( module . shortid )
240235 ) ;
241236
242- state . editor . changedModuleShortids = [ ] ;
237+ const updatedModules = await effects . api . saveModules (
238+ sandbox . id ,
239+ changedModules
240+ ) ;
241+
242+ updatedModules . forEach ( updatedModule => {
243+ const module = sandbox . modules . find (
244+ moduleItem => moduleItem . shortid === updatedModule . shortid
245+ ) ;
243246
244- await effects . api . saveModules ( sandbox . id , changedModules ) ;
245- effects . moduleRecover . clearSandbox ( sandbox . id ) ;
247+ if ( module ) {
248+ module . insertedAt = updatedModule . insertedAt ;
249+ module . updatedAt = updatedModule . updatedAt ;
250+
251+ module . savedCode =
252+ updatedModule . code === module . code ? null : updatedModule . code ;
253+
254+ effects . vscode . sandboxFsSync . writeFile (
255+ state . editor . modulesByPath ,
256+ module
257+ ) ;
258+ effects . moduleRecover . remove ( sandbox . id , module ) ;
259+ } else {
260+ // We might not have the module, as it was created by the server. In
261+ // this case we put it in. There is an edge case here where the user
262+ // might delete the module while it is being updated, but it will very
263+ // likely not happen
264+ sandbox . modules . push ( updatedModule ) ;
265+ }
266+ } ) ;
246267
247268 if (
248269 state . editor . currentSandbox . originalGit &&
@@ -253,13 +274,6 @@ export const saveClicked: AsyncAction = withOwnedSandbox(
253274
254275 effects . preview . executeCodeImmediately ( ) ;
255276 } catch ( error ) {
256- // Put back any unsaved modules taking into account that you
257- // might have changed some modules waiting for saving
258- currentlyChangedModuleShortids . forEach ( moduleShortid => {
259- if ( ! state . editor . changedModuleShortids . includes ( moduleShortid ) ) {
260- state . editor . changedModuleShortids . push ( moduleShortid ) ;
261- }
262- } ) ;
263277 actions . internal . handleError ( {
264278 message : 'There was a problem with saving the files, please try again' ,
265279 error,
@@ -510,11 +524,6 @@ export const discardModuleChanges: Action<{
510524
511525 module . updatedAt = new Date ( ) . toString ( ) ;
512526 effects . vscode . revertModule ( module ) ;
513-
514- state . editor . changedModuleShortids . splice (
515- state . editor . changedModuleShortids . indexOf ( moduleShortid ) ,
516- 1
517- ) ;
518527} ;
519528
520529export const fetchEnvironmentVariables : AsyncAction = async ( {
0 commit comments