UNPKG

event-storage

Version:

An optimized embedded event store for node.js

55 lines (47 loc) 1.07 kB
const Watcher = require('./Watcher'); /** * A mixin that provides a file watcher for this.fileName which triggers a method `onChange` on the class, that needs to be implemented. * * @template T * @param {T} Base * @returns {T} An anonymous class that extends Base */ const WatchesFile = Base => class extends Base { /** * @private */ watchFile() { this.stopWatching(); this.watcher = new Watcher(this.fileName); this.watcher.on('change', this.onChange.bind(this)); this.watcher.on('rename', this.onRename.bind(this)); } /** * @private */ stopWatching() { if (this.watcher) { this.watcher.close(); this.watcher = null; } } /** * @api * @returns {boolean} */ open() { if (this.fd) { return false; } this.watchFile(); return super.open(); } /** * @api */ close() { this.stopWatching(); super.close(); } }; module.exports = WatchesFile;