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. css - transform: translateY(-50%)在360浏览器极速模式下使得文字变模糊了2. mysql优化 - mysql慢查询copying to tmp table3. nginx英文文档的WebSocket proxying部分没看太明白,麻烦推荐一点中文文章4. python - 关于matplotlib的x轴显示的问题5. android 文件File删除问题6. css - .clearfix:after中为什么设置display: table7. angular.js - 怎样在使用ng-repeat属性的标签里面监听单个事件,使其能够单个改变CSS。8. angular.js - js 点击事件onclick=“”,引号内的函数名字 可以为 变量吗9. 请教: 关于 python 反斜杠转义的疑问10. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安装失败???
