Skip to content

Commit 322e851

Browse files
committed
fix: separate error emit from regular events
Separates the error event arguments from the regular event arguments. Before, we had a merged tuple of `[Path | Error, Stats?]` to account for the fact that some events can be `[Path, Stats?]` and some can be `[Error, Stats?]`. However, this results in incorrect types since nothing will ever pass `Path | Error` as a type (only one or the other). This separates them and uses a union instead, such that event handlers other than `error` only ever have a `Path`.
1 parent 3fa04b5 commit 322e851

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ export type FSWInstanceOptions = BasicOpts & {
6060
};
6161

6262
export type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change';
63-
export type EmitArgs = [Path | Error, Stats?];
64-
export type EmitArgsWithName = [EventName, ...EmitArgs];
63+
export type EmitArgs = [path: Path, stats?: Stats];
64+
export type EmitErrorArgs = [error: Error, stats?: Stats];
65+
export type EmitArgsWithName = [event: EventName, ...EmitArgs];
6566
export type MatchFunction = (val: string, stats?: Stats) => boolean;
6667
export interface MatcherObject {
6768
path: string;
@@ -302,9 +303,7 @@ export interface FSWatcherKnownEventMap {
302303
[EV.READY]: [];
303304
[EV.RAW]: Parameters<WatchHandlers['rawEmitter']>;
304305
[EV.ERROR]: Parameters<WatchHandlers['errHandler']>;
305-
[EV.ALL]: [EventName, ...EmitArgs];
306-
[EV.UNLINK]: [path: string];
307-
[EV.UNLINK_DIR]: [path: string];
306+
[EV.ALL]: [event: EventName, ...EmitArgs];
308307
}
309308

310309
export type FSWatcherEventMap = FSWatcherKnownEventMap & {
@@ -604,7 +603,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
604603
const opts = this.options;
605604
if (isWindows) path = sysPath.normalize(path);
606605
if (opts.cwd) path = sysPath.relative(opts.cwd, path);
607-
const args: EmitArgs = [path];
606+
const args: EmitArgs | EmitErrorArgs = [path];
608607
if (stats != null) args.push(stats);
609608

610609
const awf = opts.awaitWriteFinish;
@@ -639,7 +638,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
639638
const awfEmit = (err?: Error, stats?: Stats) => {
640639
if (err) {
641640
event = EV.ERROR;
642-
args[0] = err;
641+
(args as unknown as EmitErrorArgs)[0] = err;
643642
this.emitWithAll(event, args);
644643
} else if (stats) {
645644
// if stats doesn't exist the file must have been deleted

0 commit comments

Comments
 (0)