filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
44 lines (43 loc) • 1.53 kB
JavaScript
;
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) {
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, Local_1.watchChanges)(path, ac)) {
let newPath = path;
if (eventType == "rename" && (0, posix_1.basename)(path) !== filename) {
newPath = (0, posix_1.join)((0, posix_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;