Skip to content

Commit 53417ba

Browse files
bunch of fixes
1 parent 0f5b6ea commit 53417ba

File tree

14 files changed

+328
-375
lines changed

14 files changed

+328
-375
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ export const notificationRemoved: Action<{
5252
state.notifications.splice(notificationToRemoveIndex, 1);
5353
};
5454

55-
export const forceRender: Action = ({ state }) => {
56-
state.editor.forceRender++;
57-
};
58-
5955
export const cliInstructionsMounted: AsyncAction = withLoadApp();
6056

6157
export const githubPageMounted: AsyncAction = withLoadApp();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export { default as themes } from './themes';
2424
export { default as executor } from './executor';
2525
export { default as stripe } from './stripe';
2626
export { default as jwt } from './jwt';
27+
export { default as preview } from './preview';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Reaction } from '..';
2+
3+
let _preview;
4+
let _reaction: Reaction;
5+
6+
export default {
7+
initialize(reaction: Reaction) {
8+
_reaction = reaction;
9+
},
10+
initializePreview(preview: any) {
11+
_preview = preview;
12+
13+
const dispose = _reaction(
14+
state => [
15+
state.editor.isAllModulesSynced,
16+
state.editor.currentSandbox.template,
17+
state.preferences.settings.livePreviewEnabled,
18+
],
19+
([isAllModulesSynced, template, livePreviewEnabled]) => {
20+
if (
21+
isAllModulesSynced &&
22+
(template === 'static' || !livePreviewEnabled)
23+
) {
24+
_preview.handleRefresh();
25+
}
26+
}
27+
);
28+
29+
_preview.executeCodeImmediately();
30+
31+
return () => {
32+
_preview = null;
33+
dispose();
34+
};
35+
},
36+
executeCodeImmediately() {
37+
if (!_preview) {
38+
return;
39+
}
40+
_preview.executeCodeImmediately();
41+
},
42+
executeCode() {
43+
if (!_preview) {
44+
return;
45+
}
46+
_preview.executeCode();
47+
},
48+
refresh() {
49+
if (!_preview) {
50+
return;
51+
}
52+
_preview.handleRefresh();
53+
},
54+
};

packages/app/src/app/overmind/effects/vscode/extensionHostWorker/common/fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export async function initializeBrowserFS({
9494
currentSandboxFs = evt.data.$data;
9595
break;
9696
}
97-
case 'writeFile': {
97+
case 'write-file': {
9898
const module = evt.data.$data;
9999
writeFile(currentSandboxFs, module);
100100
break;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ export class VSCodeEffect {
265265
this.onFileChange,
266266
this.onOperationApplied
267267
);
268-
269-
this.fs.sync();
270268
}
271269

272270
public async openModule(module: Module) {
@@ -476,7 +474,6 @@ export class VSCodeEffect {
476474
const extensionEnablementService = accessor.get(
477475
IExtensionEnablementService
478476
);
479-
// const quickopenService = accessor.get(IQuickOpenService);
480477

481478
this.commandService.resolve(commandService);
482479
this.extensionService.resolve(extensionService);
@@ -574,7 +571,7 @@ export class VSCodeEffect {
574571
});
575572
}
576573

577-
private provideDocumentFormattingEdits(model, _, token) {
574+
private provideDocumentFormattingEdits = (model, _, token) =>
578575
prettify(
579576
model.uri.fsPath,
580577
() => model.getValue(),
@@ -587,7 +584,6 @@ export class VSCodeEffect {
587584
text: newCode,
588585
},
589586
]);
590-
}
591587

592588
private changeSettings = (settings: Settings) => {
593589
this.settings = settings;

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,6 @@ class SandboxFsSync {
114114
});
115115
}
116116

117-
public async sync() {
118-
await this.initializingWorkers.promise;
119-
120-
// eslint-disable-next-line
121-
console.log('## SYNCING SANDBOX AND TYPINGS WITH WORKERS');
122-
this.syncSandbox();
123-
124-
try {
125-
await this.syncDependencyTypings();
126-
} catch (error) {
127-
// Might not be a filesystem ready yet
128-
// console.log('ERROR SYNCING', error);
129-
}
130-
}
131-
132117
public create(sandbox: Sandbox): SandboxFs {
133118
const sandboxFs = {};
134119

@@ -154,6 +139,8 @@ class SandboxFsSync {
154139
}
155140
});
156141

142+
this.sync();
143+
157144
return sandboxFs;
158145
}
159146

@@ -208,6 +195,8 @@ class SandboxFsSync {
208195
}
209196

210197
private syncSandbox() {
198+
// eslint-disable-next-line
199+
console.log('## SYNCING SANDBOX AND TYPINGS WITH WORKERS');
211200
global.postMessage(
212201
{
213202
$broadcast: true,
@@ -218,6 +207,19 @@ class SandboxFsSync {
218207
);
219208
}
220209

210+
private async sync() {
211+
await this.initializingWorkers.promise;
212+
213+
this.syncSandbox();
214+
215+
try {
216+
await this.syncDependencyTypings();
217+
} catch (error) {
218+
// Might not be a filesystem ready yet
219+
// console.log('ERROR SYNCING', error);
220+
}
221+
}
222+
221223
private send(type: string, data: any) {
222224
global.postMessage(
223225
{

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

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { clearCorrectionsFromAction } from 'app/utils/corrections';
1818
import { json } from 'overmind';
1919

20+
import getTemplate from '@codesandbox/common/lib/templates';
2021
import * as internalActions from './internalActions';
2122

2223
export const internal = internalActions;
@@ -43,6 +44,8 @@ export const addNpmDependency: AsyncAction<{
4344
version: newVersion,
4445
isDev: Boolean(isDev),
4546
});
47+
48+
actions.editor.internal.updatePreviewCode();
4649
}
4750
);
4851

@@ -61,6 +64,8 @@ export const npmDependencyRemoved: AsyncAction<{
6164
moduleShortid: state.editor.currentPackageJSON.shortid,
6265
cbID: null,
6366
});
67+
68+
actions.editor.internal.updatePreviewCode();
6469
});
6570

6671
export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
@@ -233,6 +238,12 @@ export const codeChanged: Action<{
233238
module,
234239
code,
235240
});
241+
242+
const { isServer } = getTemplate(state.editor.currentSandbox.template);
243+
244+
if (!isServer && state.preferences.settings.livePreviewEnabled) {
245+
actions.editor.internal.updatePreviewCode();
246+
}
236247
};
237248

238249
export const saveClicked: AsyncAction = withOwnedSandbox(
@@ -256,6 +267,8 @@ export const saveClicked: AsyncAction = withOwnedSandbox(
256267
) {
257268
actions.git.internal.fetchGitChanges();
258269
}
270+
271+
effects.preview.executeCodeImmediately();
259272
} catch (error) {
260273
// Put back any unsaved modules taking into account that you
261274
// might have changed some modules waiting for saving
@@ -367,6 +380,10 @@ export const moduleSelected: Action<{
367380
}
368381

369382
effects.live.sendUserCurrentModule(module.shortid);
383+
384+
if (!state.editor.isInProjectView) {
385+
actions.editor.internal.updatePreviewCode();
386+
}
370387
}
371388
} catch (error) {
372389
// Do nothing, it is most likely VSCode selecting a file
@@ -453,8 +470,9 @@ export const toggleStatusBar: Action = ({ state }) => {
453470
state.editor.statusBar = !state.editor.statusBar;
454471
};
455472

456-
export const projectViewToggled: Action = ({ state }) => {
473+
export const projectViewToggled: Action = ({ state, actions }) => {
457474
state.editor.isInProjectView = !state.editor.isInProjectView;
475+
actions.editor.internal.updatePreviewCode();
458476
};
459477

460478
export const frozenUpdated: AsyncAction<{ frozen: boolean }> = async (
@@ -710,7 +728,6 @@ export const previewActionReceived: Action<{
710728
actions.editor.addNpmDependency({
711729
name,
712730
});
713-
actions.forceRender();
714731
break;
715732
}
716733
}
@@ -719,37 +736,35 @@ export const previewActionReceived: Action<{
719736
export const renameModule: AsyncAction<{
720737
title: string;
721738
moduleShortid: string;
722-
}> = withOwnedSandbox(
723-
async ({ state, effects, actions }, { title, moduleShortid }) => {
724-
const sandbox = state.editor.currentSandbox;
725-
const module = sandbox.modules.find(
726-
moduleItem => moduleItem.shortid === moduleShortid
727-
);
739+
}> = withOwnedSandbox(async ({ state, effects }, { title, moduleShortid }) => {
740+
const sandbox = state.editor.currentSandbox;
741+
const module = sandbox.modules.find(
742+
moduleItem => moduleItem.shortid === moduleShortid
743+
);
728744

729-
if (!module) {
730-
return;
731-
}
745+
if (!module) {
746+
return;
747+
}
732748

733-
const oldTitle = module.title;
749+
const oldTitle = module.title;
734750

735-
module.title = title;
751+
module.title = title;
736752

737-
try {
738-
await effects.api.saveModuleTitle(
739-
state.editor.currentId,
740-
moduleShortid,
741-
title
742-
);
753+
try {
754+
await effects.api.saveModuleTitle(
755+
state.editor.currentId,
756+
moduleShortid,
757+
title
758+
);
743759

744-
if (state.live.isCurrentEditor) {
745-
effects.live.sendModuleUpdate(module);
746-
}
747-
} catch (error) {
748-
module.title = oldTitle;
749-
effects.notificationToast.error('Could not rename file');
760+
if (state.live.isCurrentEditor) {
761+
effects.live.sendModuleUpdate(module);
750762
}
763+
} catch (error) {
764+
module.title = oldTitle;
765+
effects.notificationToast.error('Could not rename file');
751766
}
752-
);
767+
});
753768

754769
export const onDevToolsTabAdded: Action<{
755770
tab: any;

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const saveCode: AsyncAction<{
7373
code: string;
7474
moduleShortid: string;
7575
cbID?: string | null;
76-
}> = async ({ state, effects, actions }, { code, moduleShortid, cbID }) => {
76+
}> = async ({ state, effects, actions }, { moduleShortid, cbID }) => {
7777
effects.analytics.track('Save Code');
7878

7979
const sandbox = state.editor.currentSandbox;
@@ -83,11 +83,6 @@ export const saveCode: AsyncAction<{
8383
return;
8484
}
8585

86-
await actions.editor.codeChanged({
87-
code,
88-
moduleShortid,
89-
});
90-
9186
try {
9287
const updatedModule = await effects.api.saveModuleCode(sandbox.id, module);
9388

@@ -245,9 +240,7 @@ export const setModuleCode: Action<{
245240
state.editor.changedModuleShortids.push(module.shortid);
246241
}
247242

248-
if (state.preferences.settings.experimentVSCode) {
249-
effects.vscode.runCommand('workbench.action.keepEditor');
250-
}
243+
effects.vscode.runCommand('workbench.action.keepEditor');
251244

252245
const tabs = state.editor.tabs as ModuleTab[];
253246
const tab = tabs.find(tabItem => tabItem.moduleShortid === module.shortid);
@@ -315,7 +308,7 @@ export const forkSandbox: AsyncAction<{
315308
state.editor.isForkingSandbox = false;
316309
};
317310

318-
export const setCurrentModule: Action<Module> = (
311+
export const setCurrentModule: AsyncAction<Module> = async (
319312
{ state, effects },
320313
module
321314
) => {
@@ -340,7 +333,9 @@ export const setCurrentModule: Action<Module> = (
340333
}
341334

342335
state.editor.currentModuleShortid = module.shortid;
343-
effects.vscode.openModule(module);
336+
await effects.vscode.openModule(module);
337+
effects.vscode.setErrors(state.editor.errors);
338+
effects.vscode.setCorrections(state.editor.corrections);
344339
};
345340

346341
export const updateSandboxPackageJson: AsyncAction = async ({
@@ -392,3 +387,11 @@ export const updateDevtools: AsyncAction<{
392387
state.editor.workspaceConfigCode = code;
393388
}
394389
};
390+
391+
export const updatePreviewCode: Action = ({ state, effects }) => {
392+
if (state.preferences.settings.instantPreviewEnabled) {
393+
effects.preview.executeCodeImmediately();
394+
} else {
395+
effects.preview.executeCode();
396+
}
397+
};

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type State = {
4242
errors: ModuleError[];
4343
corrections: ModuleCorrection[];
4444
isInProjectView: boolean;
45-
forceRender: number;
4645
initialPath: string;
4746
highlightedLines: number[];
4847
isUpdatingPrivacy: boolean;
@@ -84,7 +83,6 @@ export const state: State = {
8483
sessionFrozen: true,
8584
corrections: [],
8685
isInProjectView: false,
87-
forceRender: 0,
8886
initialPath: '/',
8987
highlightedLines: [],
9088
isUpdatingPrivacy: false,

0 commit comments

Comments
 (0)