@@ -13,6 +13,7 @@ import {
13
13
EMPTY_FN ,
14
14
STR_CLOSE ,
15
15
STR_END ,
16
+ WatchHandlers ,
16
17
} from './handler.js' ;
17
18
18
19
type AWF = {
@@ -58,7 +59,8 @@ export type FSWInstanceOptions = BasicOpts & {
58
59
} ;
59
60
60
61
export type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change' ;
61
- export type EmitArgs = [ EventName , Path | Error , any ?, any ?, any ?] ;
62
+ export type EmitArgs = [ Path | Error , Stats ?] ;
63
+ export type EmitArgsWithName = [ EventName , ...EmitArgs ] ;
62
64
export type MatchFunction = ( val : string , stats ?: Stats ) => boolean ;
63
65
export interface MatcherObject {
64
66
path : string ;
@@ -295,6 +297,17 @@ export class WatchHelper {
295
297
}
296
298
}
297
299
300
+ export interface FSWatcherKnownEventMap {
301
+ [ EV . READY ] : [ ] ;
302
+ [ EV . RAW ] : Parameters < WatchHandlers [ 'rawEmitter' ] > ;
303
+ [ EV . ERROR ] : Parameters < WatchHandlers [ 'errHandler' ] > ;
304
+ [ EV . ALL ] : [ EventName , ...EmitArgs ] ;
305
+ }
306
+
307
+ export type FSWatcherEventMap = FSWatcherKnownEventMap & {
308
+ [ k in Exclude < EventName , keyof FSWatcherKnownEventMap > ] : EmitArgs ;
309
+ } ;
310
+
298
311
/**
299
312
* Watches files & directories for changes. Emitted events:
300
313
* `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
@@ -303,7 +316,7 @@ export class WatchHelper {
303
316
* .add(directories)
304
317
* .on('add', path => log('File', path, 'was added'))
305
318
*/
306
- export class FSWatcher extends EventEmitter {
319
+ export class FSWatcher extends EventEmitter < FSWatcherEventMap > {
307
320
closed : boolean ;
308
321
options : FSWInstanceOptions ;
309
322
@@ -315,13 +328,13 @@ export class FSWatcher extends EventEmitter {
315
328
_watched : Map < string , DirEntry > ;
316
329
317
330
_pendingWrites : Map < string , any > ;
318
- _pendingUnlinks : Map < string , EmitArgs > ;
331
+ _pendingUnlinks : Map < string , EmitArgsWithName > ;
319
332
_readyCount : number ;
320
333
_emitReady : ( ) => void ;
321
334
_closePromise ?: Promise < void > ;
322
335
_userIgnored ?: MatchFunction ;
323
336
_readyEmitted : boolean ;
324
- _emitRaw : ( ) => void ;
337
+ _emitRaw : WatchHandlers [ 'rawEmitter' ] ;
325
338
_boundRemove : ( dir : string , item : string ) => void ;
326
339
327
340
_nodeFsHandler : NodeFsHandler ;
@@ -567,8 +580,8 @@ export class FSWatcher extends EventEmitter {
567
580
}
568
581
569
582
emitWithAll ( event : EventName , args : EmitArgs ) {
570
- this . emit ( ...args ) ;
571
- if ( event !== EV . ERROR ) this . emit ( EV . ALL , ...args ) ;
583
+ this . emit ( event , ...args ) ;
584
+ if ( event !== EV . ERROR ) this . emit ( EV . ALL , event , ...args ) ;
572
585
}
573
586
574
587
// Common helpers
@@ -588,7 +601,7 @@ export class FSWatcher extends EventEmitter {
588
601
const opts = this . options ;
589
602
if ( isWindows ) path = sysPath . normalize ( path ) ;
590
603
if ( opts . cwd ) path = sysPath . relative ( opts . cwd , path ) ;
591
- const args : EmitArgs = [ event , path ] ;
604
+ const args : EmitArgs = [ path ] ;
592
605
if ( stats != null ) args . push ( stats ) ;
593
606
594
607
const awf = opts . awaitWriteFinish ;
@@ -600,10 +613,10 @@ export class FSWatcher extends EventEmitter {
600
613
601
614
if ( opts . atomic ) {
602
615
if ( event === EV . UNLINK ) {
603
- this . _pendingUnlinks . set ( path , args ) ;
616
+ this . _pendingUnlinks . set ( path , [ event , ... args ] ) ;
604
617
setTimeout (
605
618
( ) => {
606
- this . _pendingUnlinks . forEach ( ( entry : EmitArgs , path : Path ) => {
619
+ this . _pendingUnlinks . forEach ( ( entry : EmitArgsWithName , path : Path ) => {
607
620
this . emit ( ...entry ) ;
608
621
this . emit ( EV . ALL , ...entry ) ;
609
622
this . _pendingUnlinks . delete ( path ) ;
@@ -614,21 +627,21 @@ export class FSWatcher extends EventEmitter {
614
627
return this ;
615
628
}
616
629
if ( event === EV . ADD && this . _pendingUnlinks . has ( path ) ) {
617
- event = args [ 0 ] = EV . CHANGE ;
630
+ event = EV . CHANGE ;
618
631
this . _pendingUnlinks . delete ( path ) ;
619
632
}
620
633
}
621
634
622
635
if ( awf && ( event === EV . ADD || event === EV . CHANGE ) && this . _readyEmitted ) {
623
636
const awfEmit = ( err ?: Error , stats ?: Stats ) => {
624
637
if ( err ) {
625
- event = args [ 0 ] = EV . ERROR ;
626
- args [ 1 ] = err ;
638
+ event = EV . ERROR ;
639
+ args [ 0 ] = err ;
627
640
this . emitWithAll ( event , args ) ;
628
641
} else if ( stats ) {
629
642
// if stats doesn't exist the file must have been deleted
630
- if ( args . length > 2 ) {
631
- args [ 2 ] = stats ;
643
+ if ( args . length > 1 ) {
644
+ args [ 1 ] = stats ;
632
645
} else {
633
646
args . push ( stats ) ;
634
647
}
0 commit comments