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
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‎

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+
constlistener=()=>watcher.close();
1587+
options.signal.addEventListener('abort',listener);
1588+
watcher.once('close',()=>{
1589+
options.signal.removeEventListener('abort',listener);
1590+
});
1591+
}
1592+
}
15821593

15831594
returnwatcher;
15841595
}
Lines changed: 32 additions & 0 deletions
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+
constcommon=require('../common');
7+
8+
if(common.isIBMi)
9+
common.skip('IBMi does not support `fs.watch()`');
10+
11+
constfs=require('fs');
12+
constfixtures=require('../common/fixtures');
13+
14+
15+
{
16+
// Signal aborted after creating the watcher
17+
constfile=fixtures.path('empty.js');
18+
constac=newAbortController();
19+
const{ signal }=ac;
20+
constwatcher=fs.watch(file,{ signal });
21+
watcher.once('close',common.mustCall());
22+
setImmediate(()=>ac.abort());
23+
}
24+
{
25+
// Signal aborted before creating the watcher
26+
constfile=fixtures.path('empty.js');
27+
constac=newAbortController();
28+
const{ signal }=ac;
29+
ac.abort();
30+
constwatcher=fs.watch(file,{ signal });
31+
watcher.once('close',common.mustCall());
32+
}

0 commit comments

Comments
 (0)