Files
Trilium/apps/client/src/services/file_watcher.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-04-24 11:39:44 +02:00
import ws from "./ws.js";
2022-12-01 13:07:23 +01:00
import appContext from "../components/app_context.js";
import { OpenedFileUpdateStatus } from "@triliumnext/commons";
2021-04-24 11:39:44 +02:00
const fileModificationStatus: Record<string, Record<string, OpenedFileUpdateStatus>> = {
2023-05-03 10:23:20 +02:00
notes: {},
attachments: {}
};
function checkType(type: string) {
2025-01-09 18:07:02 +02:00
if (type !== "notes" && type !== "attachments") {
2023-05-03 10:23:20 +02:00
throw new Error(`Unrecognized type '${type}', should be 'notes' or 'attachments'`);
}
}
function getFileModificationStatus(entityType: string, entityId: string) {
2023-05-03 10:23:20 +02:00
checkType(entityType);
2021-04-24 11:39:44 +02:00
2023-05-03 10:23:20 +02:00
return fileModificationStatus[entityType][entityId];
2021-04-24 11:39:44 +02:00
}
function fileModificationUploaded(entityType: string, entityId: string) {
2023-05-03 10:23:20 +02:00
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
2021-04-24 11:39:44 +02:00
}
function ignoreModification(entityType: string, entityId: string) {
2023-05-03 10:23:20 +02:00
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
}
ws.subscribeToMessages(async message => {
2025-01-09 18:07:02 +02:00
if (message.type !== "openedFileUpdated") {
2021-04-24 11:39:44 +02:00
return;
}
2023-05-03 10:23:20 +02:00
checkType(message.entityType);
fileModificationStatus[message.entityType][message.entityId] = message;
2021-04-24 11:39:44 +02:00
2025-01-09 18:07:02 +02:00
appContext.triggerEvent("openedFileUpdated", {
2023-05-03 10:23:20 +02:00
entityType: message.entityType,
entityId: message.entityId,
2021-04-24 11:39:44 +02:00
lastModifiedMs: message.lastModifiedMs,
filePath: message.filePath
});
});
export default {
getFileModificationStatus,
fileModificationUploaded,
ignoreModification
2025-01-09 18:07:02 +02:00
};