1- import  {  FileStore  }  from  "../backend/mod.ts" ; 
1+ import  {  FileStore ,   ChangeNotifier  }  from  "../backend/mod.ts" ; 
22import  {  Bus ,  Node ,  RawNode  }  from  "../model/mod.ts" ; 
33import  {  Path  }  from  "./mod.ts" ; 
44import  *  as  module  from  "../model/module/mod.ts" ; 
@@ -20,12 +20,16 @@ export class Workspace {
2020  expanded : {  [ key : string ] : {  [ key : string ] : boolean  }  } ;  // [rootid][id] 
2121  settings : { } ; 
2222
23-   constructor ( fs : FileStore )  { 
23+   constructor ( fs : FileStore ,   changes ?:  ChangeNotifier )  { 
2424    this . fs  =  fs ; 
2525    this . bus  =  new  module . Bus ( ) ; 
2626    this . expanded  =  { } ; 
2727    this . settings  =  { } ; 
2828
29+     if  ( changes )  { 
30+       changes . registerNotifier ( this . reload . bind ( this ) ) ; 
31+     } 
32+ 
2933    this . writeDebounce  =  debounce ( async  ( path ,  contents )  =>  { 
3034      try  { 
3135        await  this . fs . writeFile ( path ,  contents ) ; 
@@ -60,16 +64,28 @@ export class Workspace {
6064    } 
6165  } 
6266
67+   migrateRawNode ( n : RawNode ) : RawNode  { 
68+     if  ( n . Name  ===  "treehouse.SearchNode" )  { 
69+       n . Name  =  "treehouse.SmartNode" ; 
70+     } 
71+     return  n ; 
72+   } 
73+ 
74+   async  reload ( nodeIDs : string [ ] )  { 
75+     let  doc  =  JSON . parse ( await  this . fs . readFile ( "workspace.json" )  ||  "{}" ) ; 
76+     if  ( doc . nodes )  { 
77+       doc . nodes  =  doc . nodes . filter ( n  =>  nodeIDs . includes ( n . ID ) ) ; 
78+       doc . nodes  =  doc . nodes . map ( this . migrateRawNode ) ; 
79+       this . bus . import ( doc . nodes ) ; 
80+       m . redraw ( ) ; 
81+       console . log ( `Reloaded ${ doc . nodes . length }  ) ; 
82+     } 
83+   } 
84+ 
6385  async  load ( )  { 
6486    let  doc  =  JSON . parse ( await  this . fs . readFile ( "workspace.json" )  ||  "{}" ) ; 
6587    if  ( doc . nodes )  { 
66-       doc . nodes  =  doc . nodes . map ( n  =>  { 
67-         // any node migrations: 
68-         if  ( n . Name  ===  "treehouse.SearchNode" )  { 
69-           n . Name  =  "treehouse.SmartNode" ; 
70-         } 
71-         return  n ; 
72-       } ) 
88+       doc . nodes  =  doc . nodes . map ( this . migrateRawNode ) ; 
7389      this . bus . import ( doc . nodes ) ; 
7490      console . log ( `Loaded ${ doc . nodes . length }  ) ; 
7591    } 
0 commit comments