Skip to content

Commit 4661bb4

Browse files
refactor type sync, diffing coming tomorrow
1 parent 215541e commit 4661bb4

File tree

10 files changed

+311
-256
lines changed

10 files changed

+311
-256
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export const EXTENSIONS_LOCATION = process.env.VSCODE
33
: '/public/vscode-extensions/v10';
44

55
export const VIM_EXTENSION_ID = 'vscodevim.vim';
6+
7+
export const WAIT_INITIAL_TYPINGS_MS = 2000;

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

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {
1111
rmdir,
1212
unlink,
1313
writeFile,
14-
} from '../../sandboxFsSync/utils';
15-
import { getTypeFetcher } from './type-downloader';
14+
} from '../../SandboxFsSync/utils';
15+
16+
import { IModule } from '../../../../../../../../../standalone-packages/codesandbox-browserfs/dist/node/backend/CodeSandboxFS';
1617

1718
const global = getGlobal();
1819

@@ -59,17 +60,29 @@ export async function initializeBrowserFS({
5960
extraMounts = {},
6061
} = {}) {
6162
let isInitialSync = true;
63+
let types: {
64+
[path: string]: {
65+
module: IModule;
66+
};
67+
} = {};
68+
6269
return new Promise(resolve => {
6370
const config = { ...BROWSER_FS_CONFIG };
6471
let currentSandboxFs = {};
6572

6673
if (syncSandbox) {
6774
if (syncTypes) {
68-
const { options } = getTypeFetcher();
69-
7075
config.options['/sandbox/node_modules'] = {
7176
fs: 'CodeSandboxFS',
72-
options,
77+
options: {
78+
manager: {
79+
getTranspiledModules: () => types,
80+
addModule(module: IModule) {},
81+
removeModule(module: IModule) {},
82+
moveModule(module: IModule, newPath) {},
83+
updateModule(module: IModule) {},
84+
},
85+
},
7386
};
7487
}
7588

@@ -85,6 +98,15 @@ export async function initializeBrowserFS({
8598

8699
config.options = { ...config.options, ...extraMounts };
87100

101+
function touchFileSystem() {
102+
// This forces the file watchers to emit, which causes typescript to reload
103+
global.BrowserFS.BFSRequire('fs').rename(
104+
'/sandbox/node_modules/@types',
105+
'/sandbox/node_modules/@types',
106+
() => {}
107+
);
108+
}
109+
88110
global.BrowserFS.configure(config, e => {
89111
if (e) {
90112
console.error(e);
@@ -94,27 +116,27 @@ export async function initializeBrowserFS({
94116
if (syncSandbox) {
95117
self.addEventListener('message', evt => {
96118
switch (evt.data.$type) {
97-
case 'typings-sync': {
98-
if (isInitialSync) {
99-
commonPostMessage({
100-
$broadcast: true,
101-
$type: 'sync-sandbox',
102-
$data: {},
103-
});
104-
}
119+
case 'types-sync': {
120+
types = evt.data.$data;
121+
touchFileSystem();
122+
break;
123+
}
124+
case 'type-sync': {
125+
Object.assign(types, evt.data.$data);
126+
touchFileSystem();
105127
break;
106128
}
107129
case 'sandbox-fs': {
108130
currentSandboxFs = evt.data.$data;
109131
if (isInitialSync) {
110132
isInitialSync = false;
111-
global.BrowserFS.BFSRequire(
112-
'fs'
113-
).rename(
114-
'/sandbox/node_modules/@types',
115-
'/sandbox/node_modules/@types',
116-
() => {}
117-
);
133+
commonPostMessage({
134+
$broadcast: true,
135+
$type: 'sync-types',
136+
$data: {},
137+
});
138+
resolve();
139+
} else {
118140
resolve();
119141
}
120142
break;
@@ -147,24 +169,14 @@ export async function initializeBrowserFS({
147169
}
148170
});
149171

150-
if (syncTypes) {
151-
commonPostMessage({
152-
$broadcast: true,
153-
$type: 'sync-types',
154-
$data: {},
155-
});
156-
} else {
157-
commonPostMessage({
158-
$broadcast: true,
159-
$type: 'sync-sandbox',
160-
$data: {},
161-
});
162-
}
172+
commonPostMessage({
173+
$broadcast: true,
174+
$type: 'sync-sandbox',
175+
$data: {},
176+
});
163177
} else {
164178
resolve();
165179
}
166-
167-
// BrowserFS is initialized and ready-to-use!
168180
});
169181
});
170182
}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { listen } from 'codesandbox-api';
1818
import FontFaceObserver from 'fontfaceobserver';
1919
import * as childProcess from 'node-services/lib/child_process';
2020

21-
import { VIM_EXTENSION_ID } from './constants';
21+
import { EXTENSIONS_LOCATION, VIM_EXTENSION_ID } from './constants';
2222
import {
2323
initializeCustomTheme,
2424
initializeExtensionsFolder,
@@ -31,7 +31,7 @@ import {
3131
OnFileChangeData,
3232
OnOperationAppliedData,
3333
} from './ModelsHandler';
34-
import sandboxFsSync from './sandboxFsSync';
34+
import SandboxFsSync from './SandboxFsSync';
3535
import { getSelection } from './utils';
3636
import loadScript from './vscode-script-loader';
3737
import { Workbench } from './Workbench';
@@ -75,7 +75,9 @@ const context: any = window;
7575
*/
7676
export class VSCodeEffect {
7777
public initialized: Promise<void>;
78+
public sandboxFsSync: SandboxFsSync;
7879

80+
private isFirstTypingsSync = true;
7981
private monaco: any;
8082
private editorApi: any;
8183
private options: VsCodeOptions;
@@ -87,7 +89,6 @@ export class VSCodeEffect {
8789
private settings: Settings;
8890
private linter: Linter;
8991
private modelsHandler: ModelsHandler;
90-
private isApplyingOperation: boolean = false;
9192
private modelSelectionListener: { dispose: Function };
9293
private readOnly: boolean;
9394
private elements = {
@@ -120,34 +121,28 @@ export class VSCodeEffect {
120121
// It will only load the editor once. We should probably call this
121122
const container = this.elements.editor;
122123

123-
this.initialized = sandboxFsSync
124-
.initialize({
125-
getSandboxFs: options.getSandboxFs,
126-
})
127-
.then(() => {
128-
// We want to initialize before VSCode, but after browserFS is configured
129-
// For first-timers initialize a theme in the cache so it doesn't jump colors
130-
initializeExtensionsFolder();
131-
initializeCustomTheme();
132-
initializeThemeCache();
133-
initializeSettings();
134-
this.setVimExtensionEnabled(
135-
localStorage.getItem('settings.vimmode') === 'true'
136-
);
124+
this.initialized = this.initializeFileSystem().then(() => {
125+
// We want to initialize before VSCode, but after browserFS is configured
126+
// For first-timers initialize a theme in the cache so it doesn't jump colors
127+
initializeExtensionsFolder();
128+
initializeCustomTheme();
129+
initializeThemeCache();
130+
initializeSettings();
131+
this.setVimExtensionEnabled(
132+
localStorage.getItem('settings.vimmode') === 'true'
133+
);
137134

138-
return Promise.all([
139-
new FontFaceObserver('dm').load(),
140-
new Promise(resolve => {
141-
loadScript(true, ['vs/editor/codesandbox.editor.main'])(resolve);
142-
}),
143-
]).then(() => this.loadEditor(window.monaco, container));
144-
});
135+
return Promise.all([
136+
new FontFaceObserver('dm').load(),
137+
new Promise(resolve => {
138+
loadScript(true, ['vs/editor/codesandbox.editor.main'])(resolve);
139+
}),
140+
]).then(() => this.loadEditor(window.monaco, container));
141+
});
145142

146143
return this.initialized;
147144
}
148145

149-
public fs = sandboxFsSync;
150-
151146
public getEditorElement(
152147
getCustomEditor: ICustomEditorApi['getCustomEditor']
153148
) {
@@ -254,13 +249,32 @@ export class VSCodeEffect {
254249
this.modelsHandler.dispose();
255250
}
256251

252+
if (this.sandboxFsSync) {
253+
this.sandboxFsSync.dispose();
254+
}
255+
257256
this.modelsHandler = new ModelsHandler(
258257
this.editorApi,
259258
this.monaco,
260259
sandbox,
261260
this.onFileChange,
262261
this.onOperationApplied
263262
);
263+
this.sandboxFsSync = new SandboxFsSync(this.options);
264+
265+
return this.sandboxFsSync.create(sandbox);
266+
}
267+
268+
public syncTypings() {
269+
if (this.isFirstTypingsSync) {
270+
this.isFirstTypingsSync = false;
271+
this.sandboxFsSync.syncTypings(() => {});
272+
} else {
273+
this.editorApi.extensionService.stopExtensionHost();
274+
this.sandboxFsSync.syncTypings(() => {
275+
this.editorApi.extensionService.startExtensionHost();
276+
});
277+
}
264278
}
265279

266280
public async closeAllTabs() {
@@ -396,6 +410,64 @@ export class VSCodeEffect {
396410
}
397411
}
398412

413+
private initializeFileSystem() {
414+
return new Promise((resolve, reject) => {
415+
window.BrowserFS.configure(
416+
{
417+
fs: 'MountableFileSystem',
418+
options: {
419+
'/': { fs: 'InMemory', options: {} },
420+
'/sandbox': {
421+
fs: 'CodeSandboxEditorFS',
422+
options: {
423+
api: {
424+
getSandboxFs: this.options.getSandboxFs,
425+
},
426+
},
427+
},
428+
'/sandbox/node_modules': {
429+
fs: 'CodeSandboxFS',
430+
options: {
431+
manager: {
432+
getTranspiledModules: () => this.sandboxFsSync.getTypes(),
433+
addModule() {},
434+
removeModule() {},
435+
moveModule() {},
436+
updateModule() {},
437+
},
438+
},
439+
},
440+
'/vscode': {
441+
fs: 'LocalStorage',
442+
},
443+
'/home': {
444+
fs: 'LocalStorage',
445+
},
446+
'/extensions': {
447+
fs: 'BundledHTTPRequest',
448+
options: {
449+
index: EXTENSIONS_LOCATION + '/extensions/index.json',
450+
baseUrl: EXTENSIONS_LOCATION + '/extensions',
451+
bundle: EXTENSIONS_LOCATION + '/bundles/main.min.json',
452+
logReads: process.env.NODE_ENV === 'development',
453+
},
454+
},
455+
'/extensions/custom-theme': {
456+
fs: 'InMemory',
457+
},
458+
},
459+
},
460+
async e => {
461+
if (e) {
462+
reject(e);
463+
} else {
464+
resolve();
465+
}
466+
}
467+
);
468+
});
469+
}
470+
399471
private initializeReactions() {
400472
const { reaction } = this.options;
401473

@@ -521,6 +593,7 @@ export class VSCodeEffect {
521593
editorPart,
522594
editorService,
523595
codeEditorService,
596+
extensionService,
524597
};
525598

526599
window.CSEditor = {

0 commit comments

Comments
 (0)