fs.watch fired twice when I change the watched file

后端 未结 12 1306
星月不相逢
星月不相逢 2020-12-09 01:30
 fs.watch( \'example.xml\', function ( curr, prev ) {
   // on file change we can read the new xml
   fs.readFile( \'example.xml\',\'utf8\', function ( err, data ) {         


        
相关标签:
12条回答
  • 2020-12-09 02:10

    My custom solution

    I personally like using return to prevent a block of code to run when checking something, so, here is my method:

    var watching = false;
    fs.watch('./file.txt', () => {
        if(watching) return;
        watching = true;
    
        // do something
    
        // the timeout is to prevent the script to run twice with short functions
        // the delay can be longer to disable the function for a set time
        setTimeout(() => {
            watching = false;
        }, 100);
    };
    

    Feel free to use this example to simplify your code. It may NOT be better than using a module from others, but it works pretty well!

    0 讨论(0)
  • 2020-12-09 02:10

    Similar/same problem. I needed to do some stuff with images when they were added to a directory. Here's how I dealt with the double firing:

    var fs = require('fs');
    var working = false;
    
    fs.watch('directory', function (event, filename) {
      if (filename && event == 'change' && active == false) {
        active = true;
    
        //do stuff to the new file added
    
        active = false;
    });
    

    It will ignore the second firing until if finishes what it has to do with the new file.

    0 讨论(0)
  • 2020-12-09 02:11

    I suggest to work with chokidar (https://github.com/paulmillr/chokidar) which is much better than fs.watch:

    Commenting its README.md:

    Node.js fs.watch:

    • Doesn't report filenames on OS X.
    • Doesn't report events at all when using editors like Sublime on OS X.
    • Often reports events twice.
    • Emits most changes as rename.
    • Has a lot of other issues
    • Does not provide an easy way to recursively watch file trees.

    Node.js fs.watchFile:

    • Almost as bad at event handling.
    • Also does not provide any recursive watching.
    • Results in high CPU utilization.
    0 讨论(0)
  • 2020-12-09 02:12

    Easiest solution:

    const watch = (path, opt, fn) => {
      var lock = false
      fs.watch(path, opt, function () {
        if (!lock) {
          lock = true
          fn()
          setTimeout(() => lock = false, 1000)
        }
      })
    }
    watch('/path', { interval: 500 }, function () {
      // ...
    })
    
    0 讨论(0)
  • 2020-12-09 02:13

    I somtimes get multible registrations of the Watch event causing the Watch event to fire several times. I solved it by keeping a list of watching files and avoid registering the event if the file allready is in the list:

     var watchfiles = {};
    
    function initwatch(fn, callback) {
        if watchlist[fn] {
            watchlist[fn] = true;
            fs.watch(fn).on('change', callback);
        }
    }
    

    ......

    0 讨论(0)
  • 2020-12-09 02:15

    The fs.watch api:

    1. is unstable
    2. has known "behaviour" with regards repeated notifications. Specifically, the windows case being a result of windows design, where a single file modification can be multiple calls to the windows API
    0 讨论(0)
提交回复
热议问题