filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
45 lines (44 loc) • 1.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const Local_1 = require("./Local");
const ReferenceCountMap_1 = __importDefault(require("./utils/ReferenceCountMap"));
class default_1 {
constructor(listener) {
this.listener = listener;
this.watched = new ReferenceCountMap_1.default;
}
async watch(path) {
if (this.watched.inc(path)) {
return;
}
try {
const ac = new AbortController();
this.watched.set(path, ac);
for await (const { eventType, filename } of (0, promises_1.watch)(path, { signal: ac.signal })) {
let newPath = path;
if (eventType == "rename" && (0, node_path_1.basename)(path) !== filename) {
newPath = (0, node_path_1.join)((0, node_path_1.dirname)(path), filename);
this.watched.renameKey(path, newPath);
}
try {
this.listener(path, (0, Local_1.stat)(newPath), eventType);
}
catch (e) {
this.listener(path, null, eventType);
}
}
}
catch (e) {
console.error('Error while watch file', e);
}
}
unwatch(dir) {
this.watched.dec(dir)?.abort();
}
}
exports.default = default_1;