GeezerVison is showing github.com — fs: add AbortSignal support to watch · nodejs/node@7aa2e5d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
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 number | Diff line number | Diff line change |
|---|
@@ -4060,6 +4060,9 @@ this API: [`fs.utimes()`][]. |
4060 | 4060 | <!-- YAML |
4061 | 4061 | added: v0.5.10 |
4062 | 4062 | 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. |
4063 | 4066 | - version: v7.6.0 |
4064 | 4067 | pr-url: https://github.com/nodejs/node/pull/10739 |
4065 | 4068 | description: The `filename` parameter can be a WHATWG `URL` object using |
@@ -4079,6 +4082,7 @@ changes: |
4079 | 4082 | `false`. |
4080 | 4083 | * `encoding` {string} Specifies the character encoding to be used for the |
4081 | 4084 | filename passed to the listener. **Default:** `'utf8'`. |
| 4085 | + * `signal` {AbortSignal} allows closing the watcher with an AbortSignal. |
4082 | 4086 | * `listener` {Function|undefined} **Default:** `undefined` |
4083 | 4087 | * `eventType` {string} |
4084 | 4088 | * `filename` {string|Buffer} |
@@ -4101,6 +4105,9 @@ The listener callback is attached to the `'change'` event fired by |
4101 | 4105 | [`fs.FSWatcher`][], but it is not the same thing as the `'change'` value of |
4102 | 4106 | `eventType`. |
4103 | 4107 |
|
| 4108 | +If a `signal` is passed, aborting the corresponding AbortController will close |
| 4109 | +the returned [`fs.FSWatcher`][]. |
| 4110 | + |
4104 | 4111 | ### Caveats |
4105 | 4112 |
|
4106 | 4113 | <!--type=misc--> |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/fs.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1571,6 +1571,17 @@ function watch(filename, options, listener) { |
1571 | 1571 | if (listener) { |
1572 | 1572 | watcher.addListener('change', listener); |
1573 | 1573 | } |
| 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 | + } |
1574 | 1585 |
|
1575 | 1586 | return watcher; |
1576 | 1587 | } |
|
Collapse file
Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +} |