Skip to content
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

fs: is fs.unwatchFile() available on Windows? #18305

Closed
vsemozhetbyt opened this issue Jan 23, 2018 · 4 comments
Closed

fs: is fs.unwatchFile() available on Windows? #18305

vsemozhetbyt opened this issue Jan 23, 2018 · 4 comments
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jan 23, 2018

  • Version: v4 – master
  • Platform: Windows 7 x64
  • Subsystem: fs
  1. Create a script:
'use strict';

const fs = require('fs');

fs.watchFile(__filename, { interval: 1000 }, (current, previous) => {
  console.log(current.size, previous.size);
  fs.unwatchFile(__filename, () => { console.log('File unwatched'); });
  console.log('unwatchFile() called');
});
  1. Launch it, then change and save several times.

  2. Expected output:

275 274
unwatchFile() called
File unwatched
[exit]

Real output

275 274
unwatchFile() called
276 275
unwatchFile() called
277 276
unwatchFile() called
[.., no exit]

I cannot find any caveats in fs doc about this. Is there any?

@vsemozhetbyt vsemozhetbyt added the fs Issues and PRs related to the fs subsystem / file system. label Jan 23, 2018
@vsemozhetbyt
Copy link
Contributor Author

This works as intended:

'use strict';

const fs = require('fs');

const watcher = fs.watch(__filename, (eventType) => {
  console.log(eventType);
  watcher.close();
  console.log('watcher.close() called');
});
change
watcher.close() called
[exit]

@richardlau
Copy link
Member

The listener argument passed to fs.unwatchFile should be a callback function that was previously registered with fs.watchFile and is not expected to be called as part of the invocation of fs.unwatchFile.

@richardlau
Copy link
Member

To clarify, in your example the fs.unwatchFile call does not remove the listener registered with the fs.watchFile call so never exits because there is still an active registered callback.

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jan 23, 2018

@richardlau Thank you! This do work as intended:

'use strict';

const fs = require('fs');

const listener = (current, previous) => {
  console.log(current.size, previous.size);
  fs.unwatchFile(__filename, listener);
  console.log('unwatchFile() called');
}

fs.watchFile(__filename, { interval: 1000 }, listener);

I was misguided by the listener signature in last descriptions:

https://nodejs.org/download/nightly/v10.0.0-nightly20180122e7ff00d0c5/docs/api/fs.html#fs_fs_unwatchfile_filename_listener

Is this signature intended? Added here: #13424

@vsemozhetbyt vsemozhetbyt added the doc Issues and PRs related to the documentations. label Jan 23, 2018
evanlucas pushed a commit that referenced this issue Jan 30, 2018
PR-URL: #18310
Fixes: #18305
Refs: #13424
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
MylesBorins pushed a commit that referenced this issue Feb 27, 2018
PR-URL: #18310
Fixes: #18305
Refs: #13424
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this issue May 8, 2018
PR-URL: nodejs#18310
Fixes: nodejs#18305
Refs: nodejs#13424
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

No branches or pull requests

2 participants