Skip to content

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

Description

@vsemozhetbyt
  • 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?

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jan 23, 2018
  2. vsemozhetbyt commented on Jan 23, 2018

    @vsemozhetbyt
    ContributorAuthor

    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]
  3. richardlau commented on Jan 23, 2018

    @richardlau
    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.

  4. richardlau commented on Jan 23, 2018

    @richardlau
    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.

  5. vsemozhetbyt commented on Jan 23, 2018

    @vsemozhetbyt
    ContributorAuthor

    @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

  6. added
    docIssues and PRs related to Node.js documentation.
    on Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions