remove message and object loading methods from ChatModel

This commit is contained in:
marlon-erler
2024-07-20 23:40:58 +02:00
parent 0bd90ff891
commit 611b876509
+15 -30
View File
@@ -22,9 +22,6 @@ export class ChatModel {
info: ChatInfo; info: ChatInfo;
color: Color; color: Color;
messages = new Set<ChatMessage>();
objects = new Map<string, ChatObject>();
// sorting // sorting
get index(): number { get index(): number {
return this.chatListModel.getIndexOfPrimaryChannel( return this.chatListModel.getIndexOfPrimaryChannel(
@@ -89,15 +86,11 @@ export class ChatModel {
}; };
addMessage = (message: ChatMessage): void => { addMessage = (message: ChatMessage): void => {
if (!this.messages.has(message)) {
this.messages.add(message);
}
const messagePath: string[] = this.getMessagePath(message.id); const messagePath: string[] = this.getMessagePath(message.id);
this.storageModel.storeStringifiable(messagePath, message); this.storageModel.storeStringifiable(messagePath, message);
}; };
addObject = (object: ChatObject): void => { addObject = (object: ChatObject): void => {
this.objects.set(object.id, object);
const objectPath: string[] = this.getObjectPath(object.id); const objectPath: string[] = this.getObjectPath(object.id);
this.storageModel.storeStringifiable(objectPath, object); this.storageModel.storeStringifiable(objectPath, object);
}; };
@@ -105,7 +98,6 @@ export class ChatModel {
// delete // delete
delete = () => { delete = () => {
// untrack // untrack
this.unloadData();
this.chatListModel.untrackChat(this); this.chatListModel.untrackChat(this);
// delete // delete
@@ -133,33 +125,37 @@ export class ChatModel {
} }
}; };
restoreMessages = (): void => { get messages(): ChatMessage[] {
const messageIds: string[] = this.storageModel.list(this.messageDirPath); const messageIds: string[] = this.storageModel.list(this.messageDirPath);
if (!Array.isArray(messageIds)) return; if (!Array.isArray(messageIds)) return [];
let messages: ChatMessage[] = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const messagePath: string[] = this.getMessagePath(messageId); const messagePath: string[] = this.getMessagePath(messageId);
const message: any = this.storageModel.restoreStringifiable(messagePath); const message: any = this.storageModel.restoreStringifiable(messagePath);
if (checkIsValidObject(message) == false) return; if (checkIsValidObject(message) == false) continue;
this.messages.add(message); messages.push(message);
} }
}; return messages;
}
restoreObjects = (): void => { get objects(): ChatObject[] {
const objectIds: string[] = this.storageModel.list(this.objectDirPath); const objectIds: string[] = this.storageModel.list(this.objectDirPath);
if (!Array.isArray(objectIds)) return; if (!Array.isArray(objectIds)) return [];
let objects: ChatObject[] = [];
for (const objectId of objectIds) { for (const objectId of objectIds) {
const objectPathComponents: string[] = this.getObjectPath(objectId); const objectPathComponents: string[] = this.getObjectPath(objectId);
const object: any = const object: any =
this.storageModel.restoreStringifiable(objectPathComponents); this.storageModel.restoreStringifiable(objectPathComponents);
if (checkIsValidObject(object) == false) return; if (checkIsValidObject(object) == false) continue;
if (object.id != objectId) return; if (object.id != objectId) continue;
this.objects.set(objectId, object); objects.push(object);
} }
}; return objects;
}
// messaging // messaging
sendMessage = (body: string): boolean => { sendMessage = (body: string): boolean => {
@@ -183,17 +179,6 @@ export class ChatModel {
return true; return true;
}; };
// memory
loadData = (): void => {
this.restoreMessages();
this.restoreObjects();
};
unloadData = (): void => {
this.messages.clear();
this.objects.clear();
};
// init // init
constructor( constructor(
storageModel: StorageModel, storageModel: StorageModel,