@@ -18,7 +18,7 @@ import { listen } from 'codesandbox-api';
1818import FontFaceObserver from 'fontfaceobserver' ;
1919import * 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' ;
2222import {
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 ' ;
3535import { getSelection } from './utils' ;
3636import loadScript from './vscode-script-loader' ;
3737import { Workbench } from './Workbench' ;
@@ -75,7 +75,9 @@ const context: any = window;
7575 */
7676export 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