Files
Trilium/src/public/app/services/file_watcher.ts

64 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";
2021-04-24 11:39:44 +02:00
// TODO: Deduplicate
interface Message {
type: string;
entityType: string;
entityId: string;
lastModifiedMs: number;
filePath: string;
}
const fileModificationStatus: Record<string, Record<string, Message>> = {
2023-05-03 10:23:20 +02:00
notes: {},
attachments: {}
};
function checkType(type: string) {
2023-05-03 10:23:20 +02:00
if (type !== 'notes' && type !== 'attachments') {
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: Message) => {
2021-04-24 11:39:44 +02:00
if (message.type !== 'openedFileUpdated') {
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
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
2021-04-24 11:39:44 +02:00
}