node.js - 來幫我捋一下node中fs模塊watch實現(xiàn)原理
問題描述
來探索一下node中fs模塊watch實現(xiàn)原理,
const fs = require(’fs’);var fileName = ’a.txt’;fs.watch(fileName, (function () { var count = 0; return function () {count++;console.log('文件' + fileName + ' 內(nèi)容剛剛改變。。第' + count + '次'); };})());console.log('watching file...');
fs如何實現(xiàn)針對文件變化做出響應的事件通知的呢?如果說是通過監(jiān)聽文件大小變化,或者其他?
下面是通過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當中掛載的數(shù)據(jù)卷監(jiān)聽不到事件通知
問題解答
回答1:看一下這個吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關(guān)文章:
1. javascript - 這不是對象字面量函數(shù)嗎?為什么要new初始化?2. sql語句 - mysql中關(guān)聯(lián)表查詢問題3. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變4. html5 - 有可以一次性把所有 css外部樣式轉(zhuǎn)為html標簽內(nèi)style=" "的方法嗎?5. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。6. html - vue項目中用到了elementUI問題7. javascript - 有什么比較好的網(wǎng)頁版shell前端組件?8. javascript - iframe 為什么加載網(wǎng)頁的時候滾動條這樣顯示?9. javascript - [js]為什么畫布里不出現(xiàn)圖片呢?在線等10. javascript - 原生canvas中如何獲取到觸摸事件的canvas內(nèi)坐標?
