-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
fs: add .ref() and .unref() methods to the StatWatcher and FSWatcher #33134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -579,6 +579,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()` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| * 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()` | ||
| <!-- YAML | ||
| 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 | ||
| callback is invoked. Calling `watcher.unref()` multiple times will have | ||
| no effect. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods should probably also be documented for the return value of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, there are other methods like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| ## Class: `fs.StatWatcher` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| * Extends {EventEmitter} | ||
|
|
||
| A successful call to `fs.watchFile()` method will return a new `fs.StatWatcher` | ||
| object. | ||
|
|
||
| ### `watcher.ref()` | ||
| <!-- YAML | ||
| 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. | ||
|
|
||
| By default, all `StatWatcher` objects are "ref'ed", making it normally | ||
| unnecessary to call `watcher.ref()` unless `watcher.unref()` had been | ||
| called previously. | ||
|
|
||
rickyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### `watcher.unref()` | ||
| <!-- YAML | ||
| 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 | ||
| callback is invoked. Calling `watcher.unref()` multiple times will have | ||
| no effect. | ||
|
|
||
| ## Class: `fs.ReadStream` | ||
| <!-- YAML | ||
| added: v0.1.93 | ||
|
|
@@ -3958,6 +4024,7 @@ changes: | |
| * `listener` {Function} | ||
| * `current` {fs.Stats} | ||
| * `previous` {fs.Stats} | ||
| * Returns: {fs.StatWatcher} | ||
|
|
||
| Watch for changes on `filename`. The callback `listener` will be called each | ||
| time the file is accessed. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| if (common.isIBMi) | ||
| common.skip('IBMi does not support `fs.watch()`'); | ||
|
|
||
| const fs = require('fs'); | ||
|
|
||
| const watcher = fs.watch(__filename, common.mustNotCall()); | ||
|
|
||
| watcher.unref(); | ||
|
|
||
| setTimeout( | ||
| common.mustCall(() => { | ||
| watcher.ref(); | ||
| watcher.unref(); | ||
| }), | ||
| common.platformTimeout(100) | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| const fs = require('fs'); | ||
| const assert = require('assert'); | ||
|
|
||
| const uncalledListener = common.mustNotCall(); | ||
| const uncalledListener2 = common.mustNotCall(); | ||
| const watcher = fs.watchFile(__filename, uncalledListener); | ||
|
|
||
| watcher.unref(); | ||
| watcher.unref(); | ||
| watcher.ref(); | ||
| watcher.unref(); | ||
| watcher.ref(); | ||
| watcher.ref(); | ||
| watcher.unref(); | ||
|
|
||
| fs.unwatchFile(__filename, uncalledListener); | ||
|
|
||
| // Watch the file with two different listeners. | ||
| fs.watchFile(__filename, uncalledListener); | ||
| const watcher2 = fs.watchFile(__filename, uncalledListener2); | ||
|
|
||
| setTimeout( | ||
| common.mustCall(() => { | ||
| fs.unwatchFile(__filename, common.mustNotCall()); | ||
| assert.strictEqual(watcher2.listenerCount('change'), 2); | ||
| fs.unwatchFile(__filename, uncalledListener); | ||
| assert.strictEqual(watcher2.listenerCount('change'), 1); | ||
| watcher2.unref(); | ||
| }), | ||
| common.platformTimeout(100) | ||
| ); |
Uh oh!
There was an error while loading. Please reload this page.