@@ -33,6 +33,7 @@ export class Mirror {
3333 private watchedFolder : Record < string , FSWatcher > = { } ;
3434 private readonly diskList : Record < string , { ts : number ; source : string ; name : string } > ;
3535 private dbList : Record < string , ioBroker . BaseObject > = { } ;
36+ private ready = false ;
3637
3738 constructor ( options : { diskRoot : string ; adapter : ioBroker . Adapter ; log ?: ioBroker . Logger } ) {
3839 if ( ! options . adapter ) {
@@ -64,7 +65,7 @@ export class Mirror {
6465 error : text => console . error ( text ) ,
6566 } ;
6667 }
67- if ( ! options . diskRoot || options . adapter . namespace !== 'javascript.0' ) {
68+ if ( ! options . diskRoot ?. trim ( ) || this . adapter . namespace !== 'javascript.0' ) {
6869 // only instance 0 can sync objects
6970 return ;
7071 }
@@ -79,17 +80,18 @@ export class Mirror {
7980 }
8081
8182 this . diskList = this . scanDisk ( ) ;
82- this . checkLastSyncObject ( lastSyncTime => {
83+ this . checkLastSyncObject ( lastSyncTime =>
8384 this . scanDB ( list => {
8485 this . dbList = list ;
8586 this . sync ( lastSyncTime ) ;
8687
8788 this . adapter . setForeignState ( this . lastSyncID , Date . now ( ) , true ) ;
8889
90+ this . ready = true ;
8991 // monitor all folders
9092 this . watchFolders ( this . diskRoot ) ;
91- } ) ;
92- } ) ;
93+ } ) ,
94+ ) ;
9395 }
9496
9597 watchFolders ( root_ : string ) : void {
@@ -170,10 +172,10 @@ export class Mirror {
170172 } ;
171173
172174 this . adapter . setForeignObject ( this . lastSyncID , obj , ( ) =>
173- this . adapter . setForeignState ( this . lastSyncID , 0 , true , ( ) => cb && cb ( 0 ) ) ,
175+ this . adapter . setForeignState ( this . lastSyncID , 0 , true , ( ) => cb ?. ( 0 ) ) ,
174176 ) ;
175177 } else {
176- void this . adapter . getForeignState ( this . lastSyncID , ( _err , state ) => cb && cb ( state ?. val as number ) ) ;
178+ void this . adapter . getForeignState ( this . lastSyncID , ( _err , state ) => cb ?. ( state ?. val as number ) ) ;
177179 }
178180 } ) ;
179181 }
@@ -412,6 +414,9 @@ export class Mirror {
412414 }
413415
414416 onFileChange ( event : 'change' | 'create' | 'delete' | 'rename' , file : string ) : void {
417+ if ( ! this . ready ) {
418+ return ;
419+ }
415420 let stats : Stats | undefined ;
416421 const exists = existsSync ( file ) ;
417422 if ( exists ) {
@@ -542,13 +547,14 @@ export class Mirror {
542547 }
543548
544549 onObjectChange ( id : string , obj : ioBroker . ScriptObject | null | undefined ) : void {
545- if ( ! this . dbList || ! id ) {
550+ if ( ! this . dbList || ! id || ! this . ready ) {
546551 return ;
547552 }
548553
549554 const file = this . _scriptId2FileName ( id , obj ?. common ?. engineType as ScriptType ) ;
550555
551- if ( ! obj || ! obj . common ) {
556+ if ( ! obj ?. common ) {
557+ // File was deleted
552558 if ( this . dbList [ id ] ) {
553559 delete this . dbList [ id ] ;
554560 const folderId = Mirror . getDBFolder ( id ) ;
@@ -570,6 +576,7 @@ export class Mirror {
570576 }
571577 } else if ( obj . type === 'script' && id . startsWith ( 'script.js.' ) ) {
572578 if ( this . dbList [ id ] ) {
579+ // File changed
573580 if ( this . dbList [ id ] . common . source !== obj . common . source ) {
574581 this . dbList [ id ] = obj ;
575582 this . log . debug ( `Update ${ file } on disk` ) ;
@@ -691,9 +698,7 @@ export class Mirror {
691698 }
692699 }
693700 }
694- if ( cb ) {
695- cb ( list ) ;
696- }
701+ cb ?.( list ) ;
697702 } ,
698703 ) ;
699704 } ,
0 commit comments