44 */
55
66/*
7- Watch A DIRECTORY for changes. Use ./watcher.ts for a single file.
8-
7+ Watch A DIRECTORY for changes of the files in *that* directory only (not recursive).
8+ Use ./watcher.ts for a single file.
99
1010Slightly generalized fs.watch that works even when the directory doesn't exist,
1111but also doesn't provide any information about what changed.
@@ -54,7 +54,7 @@ const logger = getLogger("backend:path-watcher");
5454const POLLING = true ;
5555
5656const DEFAULT_POLL_MS = parseInt (
57- process . env . COCALC_FS_WATCHER_POLL_INTERVAL_MS ?? "1500 " ,
57+ process . env . COCALC_FS_WATCHER_POLL_INTERVAL_MS ?? "3000 " ,
5858) ;
5959
6060const ChokidarOpts : WatchOptions = {
@@ -79,17 +79,13 @@ export class Watcher extends EventEmitter {
7979 private exists : boolean ;
8080 private watchContents ?: FSWatcher ;
8181 private watchExistence ?: FSWatcher ;
82- private interval_ms : number ;
8382 private debounce_ms : number ;
8483 private debouncedChange : any ;
8584 private log : Function ;
8685
8786 constructor (
8887 path : string ,
89- {
90- debounce : debounce_ms = 0 ,
91- interval : interval_ms ,
92- } : { debounce ?: number ; interval ?: number } = { } ,
88+ { debounce : debounce_ms = DEFAULT_POLL_MS } : { debounce ?: number } = { } ,
9389 ) {
9490 super ( ) ;
9591 this . log = logger . extend ( path ) . debug ;
@@ -99,7 +95,6 @@ export class Watcher extends EventEmitter {
9995 }
10096 this . path = path . startsWith ( "/" ) ? path : join ( process . env . HOME , path ) ;
10197 this . debounce_ms = debounce_ms ;
102- this . interval_ms = interval_ms ?? DEFAULT_POLL_MS ;
10398 this . debouncedChange = this . debounce_ms
10499 ? debounce ( this . change , this . debounce_ms , {
105100 leading : true ,
@@ -122,16 +117,8 @@ export class Watcher extends EventEmitter {
122117 }
123118 }
124119
125- private chokidarOptions = ( ) => {
126- return {
127- ...ChokidarOpts ,
128- interval : this . interval_ms ,
129- binaryInterval : this . interval_ms ,
130- } ;
131- } ;
132-
133120 private initWatchContents ( ) : void {
134- this . watchContents = watch ( this . path , this . chokidarOptions ( ) ) ;
121+ this . watchContents = watch ( this . path , ChokidarOpts ) ;
135122 this . watchContents . on ( "all" , this . debouncedChange ) ;
136123 this . watchContents . on ( "error" , ( err ) => {
137124 this . log ( `error watching listings -- ${ err } ` ) ;
@@ -140,7 +127,7 @@ export class Watcher extends EventEmitter {
140127
141128 private async initWatchExistence ( ) : Promise < void > {
142129 const containing_path = path_split ( this . path ) . head ;
143- this . watchExistence = watch ( containing_path , this . chokidarOptions ( ) ) ;
130+ this . watchExistence = watch ( containing_path , ChokidarOpts ) ;
144131 this . watchExistence . on ( "all" , this . watchExistenceChange ( containing_path ) ) ;
145132 this . watchExistence . on ( "error" , ( err ) => {
146133 this . log ( `error watching for existence of ${ this . path } -- ${ err } ` ) ;
0 commit comments