Skip to content

Commit c0d5d3d

Browse files
smooth fork live fixes and updating moving of files and dirs
1 parent ea9beca commit c0d5d3d

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

packages/app/src/app/overmind/effects/vscode/sandboxFsSync/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module, SandboxFs, Directory } from '@codesandbox/common/lib/types';
1+
import { Directory, Module, SandboxFs } from '@codesandbox/common/lib/types';
22
import { json } from 'overmind';
33

44
export const writeFile = (fs: SandboxFs, module: Module) => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ export const onOperationApplied: Action<{
182182
module,
183183
code,
184184
});
185+
186+
actions.editor.internal.updatePreviewCode();
185187
};
186188

187189
export const codeChanged: Action<{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ export const initialize: AsyncAction<string, Sandbox> = async (
7272
}
7373
state.editor.currentId = sandbox.id;
7474

75+
await effects.vscode.closeAllTabs();
76+
77+
state.editor.modulesByPath = effects.vscode.fs.create(
78+
state.editor.currentSandbox
79+
);
80+
81+
await effects.vscode.changeSandbox(sandbox);
82+
effects.vscode.openModule(state.editor.currentModule);
83+
effects.preview.executeCodeImmediately(true);
84+
7585
effects.analytics.track('Live Session Joined', {});
7686
effects.live.listen(actions.live.liveMessageReceived);
7787

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,24 @@ export const onModuleUpdated: Operator<
184184
const moduleIndex = sandbox.modules.findIndex(
185185
moduleEntry => moduleEntry.shortid === data.moduleShortid
186186
);
187+
const existingModule =
188+
state.editor.sandboxes[sandbox.id].modules[moduleIndex];
189+
190+
if (existingModule.path !== data.module.path) {
191+
effects.vscode.fs.rename(
192+
state.editor.modulesByPath,
193+
existingModule.path,
194+
data.module.path
195+
);
196+
}
187197

188-
Object.assign(
189-
state.editor.sandboxes[sandbox.id].modules[moduleIndex],
190-
data.module
191-
);
198+
Object.assign(existingModule, data.module);
192199

193-
effects.vscode.fs.writeFile(
194-
state.editor.modulesByPath,
195-
state.editor.sandboxes[sandbox.id].modules[moduleIndex]
196-
);
200+
effects.vscode.fs.writeFile(state.editor.modulesByPath, existingModule);
201+
202+
if (state.editor.currentModuleShortid === data.moduleShortid) {
203+
effects.vscode.openModule(existingModule);
204+
}
197205

198206
actions.editor.internal.updatePreviewCode();
199207
});
@@ -243,19 +251,30 @@ export const onDirectoryUpdated: Operator<
243251
directoryShortid: string;
244252
module: Directory; // Still very weird
245253
}>
246-
> = mutate(({ state }, { _isOwnMessage, data }) => {
254+
> = mutate(({ state, actions, effects }, { _isOwnMessage, data }) => {
247255
if (_isOwnMessage) {
248256
return;
249257
}
250258
const sandbox = state.editor.currentSandbox;
251259
const directoryIndex = sandbox.directories.findIndex(
252260
directoryEntry => directoryEntry.shortid === data.directoryShortid
253261
);
262+
const existingDirectory =
263+
state.editor.sandboxes[sandbox.id].directories[directoryIndex];
264+
const hasChangedPath = existingDirectory.path !== data.module.path;
254265

255-
Object.assign(
256-
state.editor.sandboxes[sandbox.id].directories[directoryIndex],
257-
data.module
258-
);
266+
Object.assign(existingDirectory, data.module);
267+
268+
if (hasChangedPath) {
269+
const prevCurrentModulePath = state.editor.currentModule.path;
270+
271+
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
272+
actions.editor.internal.updatePreviewCode();
273+
274+
if (prevCurrentModulePath !== state.editor.currentModule.path) {
275+
actions.editor.internal.setCurrentModule(state.editor.currentModule);
276+
}
277+
}
259278
});
260279

261280
export const onDirectoryDeleted: Operator<

0 commit comments

Comments
 (0)