node.js - 來幫我捋一下node中fs模塊watch實現原理
問題描述
來探索一下node中fs模塊watch實現原理,
const fs = require(’fs’);var fileName = ’a.txt’;fs.watch(fileName, (function () { var count = 0; return function () {count++;console.log('文件' + fileName + ' 內容剛剛改變。。第' + count + '次'); };})());console.log('watching file...');
fs如何實現針對文件變化做出響應的事件通知的呢?如果說是通過監聽文件大小變化,或者其他?
下面是通過node源碼看到的一些東西:
const FSEvent = process.binding(’fs_event_wrap’).FSEvent;function FSWatcher() { EventEmitter.call(this); var self = this; this._handle = new FSEvent(); this._handle.owner = this; this._handle.onchange = function(status, eventType, filename) { if (status < 0) { self._handle.close(); const error = !filename ? errnoException(status, ’Error watching file for changes:’) : errnoException(status, `Error watching file ${filename} for changes:`); error.filename = filename; self.emit(’error’, error); } else { self.emit(’change’, eventType, filename); } };}
后面的fs_event_wrap.cc 基本都是外星語言了。
下面我再docker當中掛載的數據卷監聽不到事件通知
問題解答
回答1:看一下這個吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關文章:
1. 求:阿里云服務器windows+小皮(phpstudy)+laravel部署方案(教程)2. html - 急求,能否用flex或者高端點的CSS來布局這個圖呢?不想用浮動或定位了3. mysql函數unix_timestamp如何處理1970.1.1以前的數據?4. javascript - vue-cli 發布之后,如何在接口地址中去掉在開發環境中設置的跨域proxyTable配置5. java - 關于使用POI解析excel中的時間格的值6. 請問永久和臨時重定向有什么區別7. java servlet后臺導出上萬條數據到excel,太慢!求解8. javascript - 一排三個框,各個框的間距是15px,距離外面的白框間距也是15px,這個css怎么寫?9. css3 - rem布局下,用戶瀏覽器的最小字號是12px怎么辦?10. 怎樣使留言的數據與登錄的用戶名,密碼保持一致(在數據庫上是一行的)。
