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

55 lines
1.3 KiB
JavaScript
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
2023-05-03 10:23:20 +02:00
const fileModificationStatus = {
notes: {},
attachments: {}
};
function checkType(type) {
if (type !== 'notes' && type !== 'attachments') {
throw new Error(`Unrecognized type '${type}', should be 'notes' or 'attachments'`);
}
}
function getFileModificationStatus(entityType, entityId) {
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
}
2023-05-03 10:23:20 +02:00
function fileModificationUploaded(entityType, entityId) {
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
2021-04-24 11:39:44 +02:00
}
2023-05-03 10:23:20 +02:00
function ignoreModification(entityType, entityId) {
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
}
2021-04-24 11:39:44 +02:00
ws.subscribeToMessages(async message => {
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
}