UNPKG

@cocalc/project

Version:
107 lines (92 loc) 3.17 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## /* Watch a file for changes Watch for changes to the given file. Returns obj, which is an event emitter with events: - 'change', ctime - when file changes or is created - 'delete' - when file is deleted and a method .close(). The ctime might be undefined, in case it can't be determined. If debounce is given, only fires after the file definitely has not had its ctime changed for at least debounce ms. Does NOT fire when the file first has ctime changed. */ var EventEmitter, fs, ref, boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } }; fs = require('fs'); ({EventEmitter} = require('events')); ref = exports.Watcher = class Watcher extends EventEmitter { constructor(path, interval, debounce) { super(); this.close = this.close.bind(this); this._listen = this._listen.bind(this); this._emit_when_stable = this._emit_when_stable.bind(this); this.path = path; this.interval = interval; this.debounce = debounce; fs.watchFile(this.path, { interval: this.interval, persistent: false }, this._listen); } close() { boundMethodCheck(this, ref); this.removeAllListeners(); return fs.unwatchFile(this.path, this.listener); } _listen(curr, prev) { boundMethodCheck(this, ref); if (curr.dev === 0) { this.emit('delete'); return; } if (this.debounce) { return this._emit_when_stable(true); } else { return fs.stat(this.path, (err, stats) => { if (!err) { return this.emit('change', stats.ctime); } }); } } _emit_when_stable(first) { boundMethodCheck(this, ref); /* @_emit_when_stable gets called periodically until the last ctime of the file is at least @debounce ms in the past, or there is an error. */ if (first && this._waiting_for_stable) { return; } this._waiting_for_stable = true; return fs.stat(this.path, (err, stats) => { var elapsed; if (err) { // maybe file deleted; give up. delete this._waiting_for_stable; return; } elapsed = new Date() - stats.ctime; if (elapsed < this.debounce) { // File keeps changing - try again soon return setTimeout((() => { return this._emit_when_stable(false); }), Math.max(500, this.debounce - elapsed + 100)); } else { delete this._waiting_for_stable; return this.emit('change', stats.ctime); } }); } }; }).call(this); //# sourceMappingURL=watcher.js.map