From decfc2ae5c1a5664eaf85f71829919cd4765fca9 Mon Sep 17 00:00:00 2001 From: rickyes Date: Wed, 29 Apr 2020 02:00:35 +0800 Subject: [PATCH] fs: add .ref() and .unref() methods to watcher classes Backport-PR-URL: https://github.com/nodejs/node/pull/35555 PR-URL: https://github.com/nodejs/node/pull/33134 Fixes: https://github.com/nodejs/node/issues/33096 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- doc/api/fs.md | 67 ++++++++++++++++++++ lib/fs.js | 6 ++ lib/internal/fs/watchers.js | 56 +++++++++++++++- src/fs_event_wrap.cc | 3 +- test/parallel/test-fs-watch-ref-unref.js | 20 ++++++ test/parallel/test-fs-watchfile-ref-unref.js | 35 ++++++++++ tools/doc/type-parser.js | 1 + 7 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-fs-watch-ref-unref.js create mode 100644 test/parallel/test-fs-watchfile-ref-unref.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 57af737a2f6444..f48f2e3b2ddf6a 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -598,6 +598,72 @@ added: v0.5.8 Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable. +### `watcher.ref()` + + +* Returns: {fs.FSWatcher} + +When called, requests that the Node.js event loop *not* exit so long as the +`FSWatcher` is active. Calling `watcher.ref()` multiple times will have +no effect. + +By default, all `FSWatcher` objects are "ref'ed", making it normally +unnecessary to call `watcher.ref()` unless `watcher.unref()` had been +called previously. + +### `watcher.unref()` + + +* Returns: {fs.FSWatcher} + +When called, the active `FSWatcher` object will not require the Node.js +event loop to remain active. If there is no other activity keeping the +event loop running, the process may exit before the `FSWatcher` object's +callback is invoked. Calling `watcher.unref()` multiple times will have +no effect. + +## Class: `fs.StatWatcher` + + +* Extends {EventEmitter} + +A successful call to `fs.watchFile()` method will return a new `fs.StatWatcher` +object. + +### `watcher.ref()` + + +* Returns: {fs.StatWatcher} + +When called, requests that the Node.js event loop *not* exit so long as the +`StatWatcher` is active. Calling `watcher.ref()` multiple times will have +no effect. + +By default, all `StatWatcher` objects are "ref'ed", making it normally +unnecessary to call `watcher.ref()` unless `watcher.unref()` had been +called previously. + +### `watcher.unref()` + + +* Returns: {fs.StatWatcher} + +When called, the active `StatWatcher` object will not require the Node.js +event loop to remain active. If there is no other activity keeping the +event loop running, the process may exit before the `StatWatcher` object's +callback is invoked. Calling `watcher.unref()` multiple times will have +no effect. + ## Class: `fs.ReadStream`