Skip to content

Commit 9d624ee

Browse files
better syncing
1 parent 586d984 commit 9d624ee

File tree

9 files changed

+129
-95
lines changed

9 files changed

+129
-95
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ export class ModelsHandler {
7676
return null;
7777
}
7878

79-
public changeModule = (module: Module) => {
79+
public changeModule = async (module: Module) => {
8080
if (getCurrentModelPath(this.editorApi) !== module.path) {
81-
return this.editorApi.openFile(module.path);
81+
const file = await this.editorApi.openFile(module.path);
82+
83+
return file.getModel();
8284
}
8385

84-
return Promise.resolve();
86+
return Promise.resolve(getModel(this.editorApi));
8587
};
8688

8789
public applyOperations(operations: { [moduleShortid: string]: any }) {

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import { commonPostMessage } from '@codesandbox/common/lib/utils/global';
2+
3+
import { FileSystemConfiguration } from '../../../../../../../../../standalone-packages/codesandbox-browserfs';
4+
import { EXTENSIONS_LOCATION } from '../../constants';
15
import {
2-
writeFile,
6+
mkdir,
37
rename,
48
rmdir,
59
unlink,
6-
mkdir,
10+
writeFile,
711
} from '../../sandboxFsSync/utils';
8-
import { FileSystemConfiguration } from '../../../../../../../../../standalone-packages/codesandbox-browserfs';
912
import { getTypeFetcher } from './type-downloader';
10-
import { EXTENSIONS_LOCATION } from '../../constants';
1113

1214
const global = self as any;
1315

@@ -85,18 +87,12 @@ export async function initializeBrowserFS({
8587
console.error(e);
8688
return;
8789
}
88-
let resolved = false;
8990

9091
if (syncSandbox) {
9192
self.addEventListener('message', evt => {
9293
switch (evt.data.$type) {
9394
case 'sandbox-fs': {
9495
currentSandboxFs = evt.data.$data;
95-
96-
if (!resolved) {
97-
resolve();
98-
resolved = true;
99-
}
10096
break;
10197
}
10298
case 'writeFile': {
@@ -126,10 +122,16 @@ export async function initializeBrowserFS({
126122
}
127123
}
128124
});
129-
} else {
130-
resolve();
125+
126+
commonPostMessage({
127+
$broadcast: true,
128+
$type: 'sync-sandbox',
129+
$data: {},
130+
});
131131
}
132132

133+
resolve();
134+
133135
// BrowserFS is initialized and ready-to-use!
134136
});
135137
});

packages/app/src/app/overmind/effects/vscode/extensionHostWorker/common/type-downloader.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
2-
getGlobal,
32
commonPostMessage,
3+
getGlobal,
44
} from '@codesandbox/common/lib/utils/global';
5+
56
import {
6-
IModule,
77
IManager,
8+
IModule,
89
} from '../../../../../../../../../standalone-packages/codesandbox-browserfs/dist/node/backend/CodeSandboxFS';
910

1011
const ctx = getGlobal();
@@ -32,6 +33,7 @@ export function getTypeFetcher() {
3233
self.addEventListener('message', evt => {
3334
if (evt.data.$type === 'typings-sync') {
3435
types = evt.data.$data;
36+
3537
// This forces the file watchers to emit, which causes typescript to reload
3638
ctx.BrowserFS.BFSRequire('fs').rename(
3739
'/sandbox/node_modules/@types',
@@ -43,7 +45,7 @@ export function getTypeFetcher() {
4345

4446
commonPostMessage({
4547
$broadcast: true,
46-
$type: 'request-data',
48+
$type: 'sync-types',
4749
$data: {},
4850
});
4951

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ import {
44
EditorSelection,
55
Module,
66
Sandbox,
7-
Settings,
87
SandboxFs,
8+
Settings,
99
} from '@codesandbox/common/lib/types';
1010
import {
1111
convertTypeToStatus,
1212
notificationState,
1313
} from '@codesandbox/common/lib/utils/notifications';
14-
import FontFaceObserver from 'fontfaceobserver';
1514
import { NotificationMessage } from '@codesandbox/notifications/lib/state';
1615
import { Reaction } from 'app/overmind';
1716
import prettify from 'app/src/app/utils/prettify';
1817
import { blocker } from 'app/utils/blocker';
1918
import { listen } from 'codesandbox-api';
19+
import FontFaceObserver from 'fontfaceobserver';
2020
import * as childProcess from 'node-services/lib/child_process';
2121

22-
import sandboxFsSync from './sandboxFsSync';
2322
import {
2423
initializeCustomTheme,
2524
initializeExtensionsFolder,
@@ -28,6 +27,7 @@ import {
2827
} from './initializers';
2928
import { Linter } from './Linter';
3029
import { ModelsHandler, OnFileChangeData } from './ModelsHandler';
30+
import sandboxFsSync from './sandboxFsSync';
3131
import { getCode, getModel, getSelection, getVSCodePath } from './utils';
3232
import loadScript from './vscode-script-loader';
3333
import { Workbench } from './Workbench';
@@ -123,10 +123,7 @@ export class VSCodeEffect {
123123
sandboxFsSync.initialize({
124124
getSandboxFs: options.getSandboxFs,
125125
}),
126-
]).then(() => {
127-
sandboxFsSync.sync();
128-
return this.loadEditor(window.monaco, container);
129-
});
126+
]).then(() => this.loadEditor(window.monaco, container));
130127

131128
return this.initialized;
132129
}
@@ -262,16 +259,16 @@ export class VSCodeEffect {
262259
sandbox,
263260
this.onFileChange
264261
);
262+
263+
this.fs.sync();
265264
}
266265

267266
public async openModule(module: Module) {
268267
await this.initialized;
269-
await this.modelsHandler.changeModule(module);
270-
271-
const model = getModel(this.editorApi);
268+
const model = await this.modelsHandler.changeModule(module);
272269

273270
this.linter.lint(
274-
getCode(this.editorApi),
271+
model.getValue(),
275272
module.title,
276273
model.getVersionId(),
277274
this.options.getCurrentSandbox().template

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

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import { dirname } from 'path';
22

3-
import { getAbsoluteDependencies } from '@codesandbox/common/lib/utils/dependencies';
4-
import { getGlobal } from '@codesandbox/common/lib/utils/global';
5-
import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator';
63
import {
74
getDirectoryPath,
85
getModulePath,
96
} from '@codesandbox/common/lib/sandbox/modules';
107
import {
11-
SandboxFs,
12-
Module,
138
Directory,
9+
Module,
1410
Sandbox,
11+
SandboxFs,
1512
} from '@codesandbox/common/lib/types';
13+
import { getAbsoluteDependencies } from '@codesandbox/common/lib/utils/dependencies';
14+
import { getGlobal } from '@codesandbox/common/lib/utils/global';
15+
import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator';
16+
import { blocker } from 'app/utils/blocker';
1617
import { json } from 'overmind';
18+
1719
import { EXTENSIONS_LOCATION } from '../constants';
1820
import { getTypeFetcher } from '../extensionHostWorker/common/type-downloader';
19-
import { writeFile, rename, rmdir, unlink, mkdir } from './utils';
21+
import { mkdir, rename, rmdir, unlink, writeFile } from './utils';
2022

2123
const global = getGlobal() as Window & { BrowserFS: any };
2224

@@ -39,12 +41,27 @@ class SandboxFsSync {
3941
private options: SandboxFsSyncOptions;
4042
private types: any;
4143
private typesInfo: Promise<any>;
44+
private initializingWorkers = blocker<void>();
45+
// We do not want to send initial sandbox until
46+
// all 3 filesystems are running
47+
private workersInitializedCount = 0;
4248
public initialize(options: SandboxFsSyncOptions) {
4349
this.options = options;
4450
self.addEventListener('message', evt => {
45-
if (evt.data.$type === 'request-data') {
46-
this.sendTypes();
47-
this.sync();
51+
if (this.initializingWorkers.isResolved()) {
52+
if (evt.data.$type === 'sync-types') {
53+
this.syncDependencyTypings();
54+
}
55+
56+
if (evt.data.$type === 'sync-sandbox') {
57+
this.syncSandbox();
58+
}
59+
} else if (evt.data.$type === 'sync-sandbox') {
60+
this.workersInitializedCount++;
61+
62+
if (this.workersInitializedCount === 3) {
63+
this.initializingWorkers.resolve();
64+
}
4865
}
4966
});
5067

@@ -97,17 +114,18 @@ class SandboxFsSync {
97114
});
98115
}
99116

100-
public sync() {
101-
global.postMessage(
102-
{
103-
$broadcast: true,
104-
$type: 'sandbox-fs',
105-
$data: json(this.options.getSandboxFs()),
106-
},
107-
protocolAndHost()
108-
);
117+
public async sync() {
118+
await this.initializingWorkers.promise;
119+
120+
console.log('## SYNCING SANDBOX AND TYPINGS WITH WORKERS');
121+
this.syncSandbox();
109122

110-
this.syncDependencyTypings();
123+
try {
124+
await this.syncDependencyTypings();
125+
} catch (error) {
126+
// Might not be a filesystem ready yet
127+
// console.log('ERROR SYNCING', error);
128+
}
111129
}
112130

113131
public create(sandbox: Sandbox): SandboxFs {
@@ -138,21 +156,6 @@ class SandboxFsSync {
138156
return sandboxFs;
139157
}
140158

141-
public reset(sandbox: Sandbox): SandboxFs {
142-
const sandboxFs = this.create(sandbox);
143-
144-
global.postMessage(
145-
{
146-
$broadcast: true,
147-
$type: 'sandbox-fs',
148-
$data: json(sandboxFs),
149-
},
150-
protocolAndHost()
151-
);
152-
153-
return sandboxFs;
154-
}
155-
156159
public writeFile(fs: SandboxFs, module: Module) {
157160
writeFile(fs, json(module));
158161

@@ -203,6 +206,17 @@ class SandboxFsSync {
203206
);
204207
}
205208

209+
private syncSandbox() {
210+
global.postMessage(
211+
{
212+
$broadcast: true,
213+
$type: 'sandbox-fs',
214+
$data: json(this.options.getSandboxFs()),
215+
},
216+
protocolAndHost()
217+
);
218+
}
219+
206220
private send(type: string, data: any) {
207221
global.postMessage(
208222
{
@@ -215,18 +229,14 @@ class SandboxFsSync {
215229
}
216230

217231
private async syncDependencyTypings() {
218-
try {
219-
const syncDetails = await this.getDependencyTypingsSyncDetails();
220-
221-
if (syncDetails) {
222-
this.types = await this.getDependencyTypings(
223-
syncDetails.packageJsonContent,
224-
syncDetails.autoInstall
225-
);
226-
this.sendTypes();
227-
}
228-
} catch (error) {
229-
console.error('DependencyTypingsSync', error);
232+
const syncDetails = await this.getDependencyTypingsSyncDetails();
233+
234+
if (syncDetails) {
235+
this.types = await this.getDependencyTypings(
236+
syncDetails.packageJsonContent,
237+
syncDetails.autoInstall
238+
);
239+
this.sendTypes();
230240
}
231241
}
232242

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ export const likeSandboxToggled: AsyncAction<{
290290
state.editor.sandboxes[id].userLiked = !state.editor.sandboxes[id].userLiked;
291291
};
292292

293+
// This might be called from explorer or vscode
293294
export const moduleSelected: Action<{
294295
path?: string;
295296
id?: string;
@@ -313,9 +314,11 @@ export const moduleSelected: Action<{
313314
);
314315
}
315316

316-
actions.editor.internal.setCurrentModule(module);
317+
if (module.shortid === state.editor.currentModuleShortid) {
318+
return;
319+
}
317320

318-
effects.vscode.openModule(module);
321+
actions.editor.internal.setCurrentModule(module);
319322

320323
if (state.live.isLive) {
321324
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Action, AsyncAction } from 'app/overmind';
1515
import { sortObjectByKeys } from 'app/overmind/utils/common';
1616
import { getTemplate as computeTemplate } from 'codesandbox-import-utils/lib/create-sandbox/templates';
1717
import { mapValues } from 'lodash-es';
18-
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
1918

2019
export const ensureSandboxId: Action<string, string> = ({ state }, id) => {
2120
if (state.editor.sandboxes[id]) {
@@ -335,6 +334,7 @@ export const setCurrentModule: Action<Module> = (
335334
}
336335

337336
state.editor.currentModuleShortid = module.shortid;
337+
effects.vscode.openModule(module);
338338
};
339339

340340
export const updateSandboxPackageJson: AsyncAction = async ({

0 commit comments

Comments
 (0)