fs: add AbortSignal support to watch · nodejs/node@acd087d · GitHub
Skip to content

Commit acd087d

Browse files
benjamingrdanielleadams
authored andcommitted
fs: add AbortSignal support to watch
PR-URL: #37190 Refs: #37179 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 5906e85 commit acd087d

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

doc/api/fs.md

Lines changed: 7 additions & 0 deletions

lib/fs.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,17 @@ function watch(filename, options, listener) {
15791579
if (listener) {
15801580
watcher.addListener('change', listener);
15811581
}
1582+
if (options.signal) {
1583+
if (options.signal.aborted) {
1584+
process.nextTick(() => watcher.close());
1585+
} else {
1586+
const listener = () => watcher.close();
1587+
options.signal.addEventListener('abort', listener);
1588+
watcher.once('close', () => {
1589+
options.signal.removeEventListener('abort', listener);
1590+
});
1591+
}
1592+
}
15821593

15831594
return watcher;
15841595
}
Lines changed: 32 additions & 0 deletions

0 commit comments

Comments
 (0)