UNPKG

filefive

Version:

SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux

53 lines (52 loc) 1.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const posix_1 = require("node:path/posix"); const Local_1 = require("./Local"); const ReferenceCountMap_1 = __importDefault(require("./utils/ReferenceCountMap")); class default_1 { constructor(listener, onMissing) { this.listener = listener; this.onMissing = onMissing; this.watched = new ReferenceCountMap_1.default; } watch(dir) { try { try { this.watched.inc(dir) || this.watched.set(dir, (0, Local_1.watch)(dir, (event, target) => { if (event == 'rename') { const child = (0, posix_1.join)(dir, target); if (this.watched.has(child) && !(0, Local_1.stat)(child)) { this.watched.del(child); this.onMissing(child); } } try { this.listener(dir, (0, Local_1.list)(dir), event, target); } catch (e) { this.watched.del(dir); this.onMissing(dir); } }, () => this.watched.del(dir))); } catch (e) { // an existing dir may be not watchable, e.g. network drive in Windows this.watched.del(dir); } const files = (0, Local_1.list)(dir); this.listener(dir, files); return files; } catch (e) { this.watched.del(dir); this.onMissing(dir); } return []; } unwatch(dir) { this.watched.dec(dir)?.(); } } exports.default = default_1;