node.js - 來幫我捋一下node中fs模塊watch實(shí)現(xiàn)原理
問題描述
來探索一下node中fs模塊watch實(shí)現(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如何實(shí)現(xiàn)針對(duì)文件變化做出響應(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:看一下這個(gè)吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關(guān)文章:
1. python+jinja2編寫前端界面,發(fā)生Unexpected end of template問題,求解決!2. python - django orm 過濾日期為當(dāng)天日期的數(shù)據(jù)3. javascript - 一個(gè)JS的算法,求大神解答4. Android "1"=="1" 到底是true還是false5. python - django 按日歸檔統(tǒng)計(jì)訂單求解6. android-studio - Android Studio 運(yùn)行項(xiàng)目的時(shí)候一堆警告,跑步起來!?7. 熱切期待朱老師的回復(fù),網(wǎng)頁視頻在線播放器插件配置錯(cuò)誤8. javascript - jQuery中l(wèi)ive事件在移動(dòng)微信端下沒有效果;代碼如下9. angular.js - 指令下的指令 面對(duì)上級(jí)指令ng-repeat的時(shí)候 ng-controller會(huì)出現(xiàn)多次的問題?10. css - 如何使用 vue transition 實(shí)現(xiàn) ios 按鈕一樣的平滑切換效果
