Skip to content

Commit

Permalink
fs: add AbortSignal support to watch
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr committed Feb 2, 2021
1 parent b750f74 commit bf378d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4133,6 +4133,9 @@ this API: [`fs.utimes()`][].
<!-- YAML
added: v0.5.10
changes:
- version: REPLACEME
pr-url: REPLACEME
description: Added support for closing the watcher with an AbortSignal.
- version: v7.6.0
pr-url: https://github.com/nodejs/node/pull/10739
description: The `filename` parameter can be a WHATWG `URL` object using
Expand All @@ -4152,6 +4155,7 @@ changes:
`false`.
* `encoding` {string} Specifies the character encoding to be used for the
filename passed to the listener. **Default:** `'utf8'`.
* `signal` {AbortSignal} allows closing the watcher with an AbortSignal.
* `listener` {Function|undefined} **Default:** `undefined`
* `eventType` {string}
* `filename` {string|Buffer}
Expand All @@ -4174,6 +4178,9 @@ The listener callback is attached to the `'change'` event fired by
[`fs.FSWatcher`][], but it is not the same thing as the `'change'` value of
`eventType`.

If a `signal` is passed, aborting the corresponding AbortController will close
the returned [`fs.FSWatcher`][].

### Caveats

<!--type=misc-->
Expand Down
10 changes: 10 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,16 @@ function watch(filename, options, listener) {
if (listener) {
watcher.addListener('change', listener);
}
if (options.signal) {
if (options.signal.aborted) {
process.nextTick(() => watcher.close());
}
const listener = () => watcher.close();
options.signal.addEventListener('abort', listener);
watcher.once('close', () => {
options.signal.removeEventListener('abort', listener);
});
}

return watcher;
}
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-fs-watch-abort-signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Flags: --expose-internals
'use strict';

// Verify that AbortSignal integration works for fs.watch

const common = require('../common');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

const fs = require('fs');
const fixtures = require('../common/fixtures');


{
// Signal aborted after creating the watcher
const file = fixtures.path('empty.js');
const ac = new AbortController();
const { signal } = ac;
const wathcer = fs.watch(file, { signal });
wathcer.once('close', common.mustCall());
setImmediate(() => ac.abort());
}
{
// Signal aborted before creating the watcher
const file = fixtures.path('empty.js');
const ac = new AbortController();
const { signal } = ac;
ac.abort();
const wathcer = fs.watch(file, { signal });
wathcer.once('close', common.mustCall());
}

0 comments on commit bf378d4

Please sign in to comment.