Skip to content

Commit 7aa2e5d

Browse files
benjamingrtargos
authored andcommitted
fs: add AbortSignal support to watch
PR-URL: #37190 Backport-PR-URL: #38386 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 81cd06b commit 7aa2e5d

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

doc/api/fs.md

+7
Original file line numberDiff line numberDiff line change
@@ -4060,6 +4060,9 @@ this API: [`fs.utimes()`][].
40604060
<!-- YAML
40614061
added: v0.5.10
40624062
changes:
4063+
- version: REPLACEME
4064+
pr-url: https://github.com/nodejs/node/pull/37190
4065+
description: Added support for closing the watcher with an AbortSignal.
40634066
- version: v7.6.0
40644067
pr-url: https://github.com/nodejs/node/pull/10739
40654068
description: The `filename` parameter can be a WHATWG `URL` object using
@@ -4079,6 +4082,7 @@ changes:
40794082
`false`.
40804083
* `encoding` {string} Specifies the character encoding to be used for the
40814084
filename passed to the listener. **Default:** `'utf8'`.
4085+
* `signal` {AbortSignal} allows closing the watcher with an AbortSignal.
40824086
* `listener` {Function|undefined} **Default:** `undefined`
40834087
* `eventType` {string}
40844088
* `filename` {string|Buffer}
@@ -4101,6 +4105,9 @@ The listener callback is attached to the `'change'` event fired by
41014105
[`fs.FSWatcher`][], but it is not the same thing as the `'change'` value of
41024106
`eventType`.
41034107

4108+
If a `signal` is passed, aborting the corresponding AbortController will close
4109+
the returned [`fs.FSWatcher`][].
4110+
41044111
### Caveats
41054112

41064113
<!--type=misc-->

lib/fs.js

+11
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,17 @@ function watch(filename, options, listener) {
15711571
if (listener) {
15721572
watcher.addListener('change', listener);
15731573
}
1574+
if (options.signal) {
1575+
if (options.signal.aborted) {
1576+
process.nextTick(() => watcher.close());
1577+
} else {
1578+
const listener = () => watcher.close();
1579+
options.signal.addEventListener('abort', listener);
1580+
watcher.once('close', () => {
1581+
options.signal.removeEventListener('abort', listener);
1582+
});
1583+
}
1584+
}
15741585

15751586
return watcher;
15761587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --expose-internals --experimental-abortcontroller
2+
'use strict';
3+
4+
// Verify that AbortSignal integration works for fs.watch
5+
6+
const common = require('../common');
7+
8+
if (common.isIBMi)
9+
common.skip('IBMi does not support `fs.watch()`');
10+
11+
const fs = require('fs');
12+
const fixtures = require('../common/fixtures');
13+
14+
15+
{
16+
// Signal aborted after creating the watcher
17+
const file = fixtures.path('empty.js');
18+
const ac = new AbortController();
19+
const { signal } = ac;
20+
const watcher = fs.watch(file, { signal });
21+
watcher.once('close', common.mustCall());
22+
setImmediate(() => ac.abort());
23+
}
24+
{
25+
// Signal aborted before creating the watcher
26+
const file = fixtures.path('empty.js');
27+
const ac = new AbortController();
28+
const { signal } = ac;
29+
ac.abort();
30+
const watcher = fs.watch(file, { signal });
31+
watcher.once('close', common.mustCall());
32+
}

0 commit comments

Comments
 (0)