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當中掛載的數(shù)據(jù)卷監(jiān)聽不到事件通知
問題解答
回答1:看一下這個吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. 實現(xiàn)bing搜索工具urlAPI提交4. 如何解決Centos下Docker服務(wù)啟動無響應(yīng),且輸入docker命令無響應(yīng)?5. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化6. javascript - 移動端上不能實現(xiàn)拖拽布局嗎?7. index.php錯誤,求指點8. javascript - ios返回不執(zhí)行js怎么解決?9. angular.js - 單頁應(yīng)用(ng/vue)該如何監(jiān)聽用戶離開當前頁面(或者路由)?10. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙

網(wǎng)公網(wǎng)安備