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)針對文件變化做出響應(yīng)的事件通知的呢?如果說是通過監(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當(dāng)中掛載的數(shù)據(jù)卷監(jiān)聽不到事件通知
問題解答
回答1:看一下這個吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關(guān)文章:
1. Span標(biāo)簽2. docker-machine添加一個已有的docker主機(jī)問題3. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””4. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。5. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.6. javascript - 計算面積函數(shù)代碼7. SessionNotFoundException:會話ID為null。調(diào)用quit()后使用WebDriver嗎?(硒)8. redis啟動有問題?9. android新手一枚,android使用httclient獲取服務(wù)器端數(shù)據(jù)失敗,但是用java工程運行就可以成功獲取。10. java - Collections類里的swap函數(shù),源碼為什么要新定義一個final的List型變量l指向傳入的list?
