From bb8580f66c4416d24b546b12f6f6eeb786231b5a Mon Sep 17 00:00:00 2001 From: rickyes Date: Wed, 29 Apr 2020 02:00:35 +0800 Subject: [PATCH 1/3] fs: add .ref() and .unref() methods to the StatWatcher and FSWatcher classes --- doc/api/fs.md | 23 +++++++++++++ lib/fs.js | 3 ++ lib/internal/fs/watchers.js | 33 ++++++++++++++++-- src/fs_event_wrap.cc | 3 +- test/parallel/test-fs-watch-ref-unref.js | 35 +++++++++++++++++++ test/parallel/test-fs-watchfile-ref-unref.js | 36 ++++++++++++++++++++ 6 files changed, 129 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 ec8e49d5b12adc..733ff6b4bc19e9 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -579,6 +579,29 @@ 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()` + + +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()` + +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.ReadStream` + 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()` + + +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()` + + +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` +* 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. @@ -597,6 +599,8 @@ called previously. added: REPLACEME --> +* 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 @@ -618,6 +622,8 @@ object. added: REPLACEME --> +* 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. @@ -631,6 +637,8 @@ called previously. added: REPLACEME --> +* 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