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
|
|
|
|
2024-12-21 17:42:48 +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: {}
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-21 17:42:48 +02:00
|
|
|
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'`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:42:48 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:42:48 +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
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:42:48 +02:00
|
|
|
function ignoreModification(entityType: string, entityId: string) {
|
2023-05-03 10:23:20 +02:00
|
|
|
checkType(entityType);
|
|
|
|
|
|
|
|
|
|
delete fileModificationStatus[entityType][entityId];
|
2021-04-24 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:42:48 +02:00
|
|
|
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,
|
2021-04-24 21:56:44 +02:00
|
|
|
fileModificationUploaded,
|
|
|
|
|
ignoreModification
|
2021-04-24 11:39:44 +02:00
|
|
|
}
|