Files
ignis/packages/shim/src/fs/watcher-client.js

88 lines
1.9 KiB
JavaScript
Raw Normal View History

// Bridges WebSocket file events to the fs shim's metadata/content caches and fs.watch listeners.
// The WebSocket itself is owned by ws-client.js; this module is a consumer.
2026-03-22 14:56:05 +01:00
import { isRecentLocalOp } from "./echo-guard.js";
export function createWatcherClient(metadataCache, contentCache, fsWatch, wsClient) {
function handleCreated(msg) {
const { path, stat } = msg;
2026-03-22 14:56:05 +01:00
if (!path || isRecentLocalOp(path)) {
2026-03-22 14:56:05 +01:00
return;
}
if (stat) {
metadataCache.set(path, {
type: "file",
size: stat.size,
mtime: stat.mtime,
ctime: stat.ctime,
});
2026-03-22 14:56:05 +01:00
}
contentCache.invalidate(path);
fsWatch._dispatch("created", path);
2026-03-22 14:56:05 +01:00
}
function handleFolderCreated(msg) {
const { path } = msg;
2026-03-22 14:56:05 +01:00
if (!path || isRecentLocalOp(path)) {
return;
}
2026-03-22 14:56:05 +01:00
metadataCache.set(path, { type: "directory" });
fsWatch._dispatch("folder-created", path);
2026-03-22 14:56:05 +01:00
}
function handleModified(msg) {
const { path, stat } = msg;
if (!path || isRecentLocalOp(path)) {
2026-03-28 16:22:15 +01:00
return;
}
if (stat) {
metadataCache.set(path, {
type: "file",
size: stat.size,
mtime: stat.mtime,
ctime: stat.ctime,
});
}
2026-03-22 14:56:05 +01:00
contentCache.invalidate(path);
fsWatch._dispatch("modified", path);
}
2026-03-22 14:56:05 +01:00
function handleDeleted(msg) {
const { path } = msg;
if (!path || isRecentLocalOp(path)) {
2026-03-22 14:56:05 +01:00
return;
}
metadataCache.delete(path);
contentCache.invalidate(path);
fsWatch._dispatch("deleted", path);
2026-03-22 14:56:05 +01:00
}
wsClient.subscribe("created", handleCreated);
wsClient.subscribe("folder-created", handleFolderCreated);
wsClient.subscribe("modified", handleModified);
wsClient.subscribe("deleted", handleDeleted);
2026-03-22 14:56:05 +01:00
function connect(vaultId) {
wsClient.connect(vaultId);
}
function disconnect() {
wsClient.disconnect();
2026-03-22 14:56:05 +01:00
}
return {
connect,
disconnect,
};
}