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 <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 5906e85 commit acd087d

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
@@ -4123,6 +4123,9 @@ this API: [`fs.utimes()`][].
41234123
<!-- YAML
41244124
added: v0.5.10
41254125
changes:
4126+
- version: REPLACEME
4127+
pr-url: https://github.com/nodejs/node/pull/37190
4128+
description: Added support for closing the watcher with an AbortSignal.
41264129
- version: v7.6.0
41274130
pr-url: https://github.com/nodejs/node/pull/10739
41284131
description: The `filename` parameter can be a WHATWG `URL` object using
@@ -4142,6 +4145,7 @@ changes:
41424145
`false`.
41434146
* `encoding` {string} Specifies the character encoding to be used for the
41444147
filename passed to the listener. **Default:** `'utf8'`.
4148+
* `signal` {AbortSignal} allows closing the watcher with an AbortSignal.
41454149
* `listener` {Function|undefined} **Default:** `undefined`
41464150
* `eventType` {string}
41474151
* `filename` {string|Buffer}
@@ -4164,6 +4168,9 @@ The listener callback is attached to the `'change'` event fired by
41644168
[`fs.FSWatcher`][], but it is not the same thing as the `'change'` value of
41654169
`eventType`.
41664170

4171+
If a `signal` is passed, aborting the corresponding AbortController will close
4172+
the returned [`fs.FSWatcher`][].
4173+
41674174
### Caveats
41684175

41694176
<!--type=misc-->

lib/fs.js

+11
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --expose-internals
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)