@eggjs/观察者
鸡蛋的文件监视器插件
使用量
在工人过程中:
app.watcher.watch(路径,听众)
开始监视文件 .
- 路径( String- QArray): 文件路径
- 收听器( 功能): 文件更改调用
app.watcher.unwatch(路径[,收听者])
停止观看文件 .
- 路径( String- QArray): 文件路径
- 收听器( 功能): 文件更改调用
在代理过程中:
代理. watch.watch(路径,收听器)
开始监视文件 .
- 路径( String- QArray): 文件路径
- 收听器( 功能): 文件更改调用
代理. watcher.unwatch (路径 [,收听者])
停止观看文件 .
- 路径( String- QArray): 文件路径
- 收听器( 功能): 文件更改调用
监视模式
development 模式
有一个内在的 开发模式 在本地工作( env 是) local) (中文(简体) ). 一旦磁盘上的文件被修改,它就会发出一个 change 立即行动.
自定义监视模式
说我们希望构建自定义事件源插件( package name : egg-watcher-custom,蛋浆。名称: watcherCustom) (中文(简体) ).
首先定义我们的自定义事件源 :
// {plugin_root}/lib/custom_event_source.js
import { Base } from 'sdk-base';
export default class CustomEventSource extends Base {
// `opts` comes from app.config[${eventSourceName}]
// `eventSourceName` will be registered later in
// `config.watcher.eventSources` as the key shown below
constructor(opts) {
super(opts);
this.ready(true);
}
watch(path) {
// replace this with your desired way of watching,
// when aware of any change, emit a `change` event
// with an info object containing `path` property
// specifying the changed directory or file.
this._h = setInterval(() => {
this.emit('change', { path });
}, 1000);
}
unwatch() {
// replace this with your implementation
if (this._h) {
clearInterval(this._h);
}
}
}
事件源执行因运行环境而异. 在与流浪者,道克人,桑巴人或其他这种非标准的发展方式合作时,您应该使用不同的表 API 来指定您的工作对象.
然后将您的自定义事件源添加到配置 :
// config/config.default.js
import CustomEventSource from '../lib/custom_event_source';
export default {
watcher: {
eventSources: {
custom: CustomEventSource,
},
},
};
选择在您想要的内嵌中使用您的自定义监视模式 .
// config/config.${env}.js
export default {
watcher: {
type: 'custom',
},
// this will pass to your CustomEventSource constructor as opts
watcherCustom: {
// foo: 'bar',
},
};
如果可能, 命名为 egg-watcher-${customName}(单位:千美元)egg-watcher-vagrant 建议(例如).
问题和建议
请打开一个问题 这里。 。 。 .
许可证
贡献者
制造的 贡献者-img。 。 。 .