@@ -52,7 +52,7 @@ export type OnOperationAppliedCallback = (data: OnOperationAppliedData) => void;
5252
5353export type ModuleModel = {
5454 changeListener : { dispose : Function } ;
55- selections : any [ ] ;
55+ selections : EditorSelection [ ] ;
5656 currentLine : number ;
5757 path : string ;
5858 model : Promise < any > ;
@@ -116,9 +116,15 @@ export class ModelsHandler {
116116 }
117117 }
118118
119- public getModuleModelByPath ( path : string ) {
119+ private getModuleModelByPath ( path : string ) : ModuleModel | undefined {
120120 const fullPath = '/sandbox' + path ;
121- this . moduleModels [ fullPath ] = this . moduleModels [ fullPath ] || {
121+
122+ return this . moduleModels [ fullPath ] ;
123+ }
124+
125+ private getOrCreateModuleModelByPath ( path : string ) : ModuleModel {
126+ const fullPath = '/sandbox' + path ;
127+ this . moduleModels [ fullPath ] = this . getModuleModelByPath ( path ) || {
122128 changeListener : null ,
123129 model : null ,
124130 currentLine : 0 ,
@@ -149,7 +155,7 @@ export class ModelsHandler {
149155 }
150156
151157 public changeModule = async ( module : Module ) => {
152- const moduleModel = this . getModuleModelByPath ( module . path ) ;
158+ const moduleModel = this . getOrCreateModuleModelByPath ( module . path ) ;
153159
154160 if ( getCurrentModelPath ( this . editorApi ) !== module . path ) {
155161 await this . editorApi . openFile ( module . path ) ;
@@ -196,7 +202,10 @@ export class ModelsHandler {
196202
197203 // Ensure we have the moduleModels
198204 Object . keys ( commentThreadsByPath ) . forEach ( path => {
199- this . getModuleModelByPath ( path ) . comments = commentThreadsByPath [ path ] ;
205+ // TODO(@christianalfoni): We should probably make this dynamic on model load instead of
206+ // on editor load? Preferably we don't keep all moduleModels for files that are not opened.
207+ this . getOrCreateModuleModelByPath ( path ) . comments =
208+ commentThreadsByPath [ path ] ;
200209 } ) ;
201210
202211 // Apply the decorations
@@ -248,14 +257,14 @@ export class ModelsHandler {
248257 ) ;
249258 }
250259
251- public async applyOperation ( moduleShortid : string , operation : any ) {
260+ public async applyOperation ( moduleShortid : string , operation : TextOperation ) {
252261 const module = this . sandbox . modules . find ( m => m . shortid === moduleShortid ) ;
253262
254263 if ( ! module ) {
255264 return ;
256265 }
257266
258- const moduleModel = this . getModuleModelByPath ( module . path ) ;
267+ const moduleModel = this . getOrCreateModuleModelByPath ( module . path ) ;
259268
260269 const modelEditor = this . editorApi . editorService . editors . find (
261270 editor => editor . resource && editor . resource . path === moduleModel . path
@@ -291,11 +300,11 @@ export class ModelsHandler {
291300
292301 public async setModuleCode ( module : Module ) {
293302 const moduleModel = this . getModuleModelByPath ( module . path ) ;
294- const model = await moduleModel . model ;
295303
296- if ( ! model ) {
304+ if ( moduleModel ?. model ) {
297305 return ;
298306 }
307+ const model = await moduleModel . model ;
299308
300309 const oldCode = model . getValue ( ) ;
301310 const changeOperation = getTextOperation ( oldCode , module . code ) ;
@@ -335,14 +344,18 @@ export class ModelsHandler {
335344 userSelections : EditorSelection [ ] ,
336345 showNameTag = true
337346 ) {
338- const moduleModel = this . getModuleModelByPath ( module . path ) ;
347+ if ( ! this . moduleModels [ '/sandbox' + module . path ] ) {
348+ return ;
349+ }
339350
340- moduleModel . selections = userSelections ;
351+ const moduleModel = this . getModuleModelByPath ( module . path ) ;
341352
342- if ( ! moduleModel . model ) {
353+ if ( ! moduleModel ? .model ) {
343354 return ;
344355 }
345356
357+ moduleModel . selections = userSelections ;
358+
346359 const model = await moduleModel . model ;
347360 const lines = model . getLinesContent ( ) || [ ] ;
348361
@@ -641,7 +654,7 @@ export class ModelsHandler {
641654 this . sandbox . directories
642655 ) ;
643656
644- const moduleModel = this . getModuleModelByPath ( module . path ) ;
657+ const moduleModel = this . getOrCreateModuleModelByPath ( module . path ) ;
645658
646659 moduleModel . model = model ;
647660 moduleModel . changeListener = this . getModelContentChangeListener (
0 commit comments