Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit acd087d

Browse filesBrowse 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
Copy full SHA for acd087d

File tree

Expand file treeCollapse file tree

3 files changed

+50
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+50
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/fs.md‎

Copy file name to clipboardExpand all lines: doc/api/fs.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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-->
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+11Lines 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
}
Collapse file
+32Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.