fs.watch fired twice when I change the watched file

后端 未结 12 1507
星月不相逢
星月不相逢 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:17

    If you need to watch your file for changes then you can check out my small library on-file-change. It checks file sha1 hash between fired change events.

    Explanation of why we have multiple fired events:

    You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

    Source

提交回复
热议问题