centralize handlers, allow multiple handlers per model

This commit is contained in:
marlon-erler
2024-07-23 01:58:34 +02:00
parent 5483c46ca1
commit 68dae8099a
7 changed files with 76 additions and 58 deletions
+28 -25
View File
@@ -314,6 +314,22 @@
function createTimestamp() { function createTimestamp() {
return (/* @__PURE__ */ new Date()).toISOString(); return (/* @__PURE__ */ new Date()).toISOString();
} }
var HandlerManager = class {
handlers = /* @__PURE__ */ new Set();
// manage
addHandler = (handler) => {
this.handlers.add(handler);
};
deleteHandler = (handler) => {
this.handlers.delete(handler);
};
// trigger
trigger = (item) => {
for (const handler of this.handlers) {
handler(item);
}
};
};
function stringify(data) { function stringify(data) {
return JSON.stringify(data, null, 4); return JSON.stringify(data, null, 4);
} }
@@ -531,8 +547,7 @@
storageModel; storageModel;
chatModel; chatModel;
fileModel; fileModel;
boardHandler = () => { boardHandlerManager = new HandlerManager();
};
// paths // paths
getBasePath = () => { getBasePath = () => {
return this.fileModel.getModelContainerPath("taskModel"); return this.fileModel.getModelContainerPath("taskModel");
@@ -576,9 +591,9 @@
return boardInfoFileContent; return boardInfoFileContent;
}; };
updateBoard = (boardInfoFileContent) => { updateBoard = (boardInfoFileContent) => {
this.boardHandler(boardInfoFileContent);
this.storeBoard(boardInfoFileContent); this.storeBoard(boardInfoFileContent);
this.chatModel.sendMessage("", boardInfoFileContent); this.chatModel.sendMessage("", boardInfoFileContent);
this.boardHandlerManager.trigger(boardInfoFileContent);
}; };
storeBoard = (boardInfoFileContent) => { storeBoard = (boardInfoFileContent) => {
this.fileModel.addFileContent(boardInfoFileContent); this.fileModel.addFileContent(boardInfoFileContent);
@@ -642,16 +657,12 @@
this.storageModel.removeRecursively(taskFilePath); this.storageModel.removeRecursively(taskFilePath);
this.storageModel.removeRecursively(taskReferencePath); this.storageModel.removeRecursively(taskReferencePath);
}; };
// other
setBoardHandler = (handler) => {
this.boardHandler = handler;
};
// init // init
constructor(storageModel2, chatModel, fileModel) { constructor(storageModel2, chatModel, fileModel) {
this.chatModel = chatModel; this.chatModel = chatModel;
this.fileModel = fileModel; this.fileModel = fileModel;
this.storageModel = storageModel2; this.storageModel = storageModel2;
fileModel.setFileContentHandler(this.handleFileContent); fileModel.fileContentHandlerManager.addHandler(this.handleFileContent);
} }
// utility // utility
static createBoardInfoFileContent = (fileId, name, color) => { static createBoardInfoFileContent = (fileId, name, color) => {
@@ -705,8 +716,7 @@
chatModel; chatModel;
storageModel; storageModel;
taskModel; taskModel;
fileContentHandler = () => { fileContentHandlerManager = new HandlerManager();
};
// paths // paths
getBasePath = () => { getBasePath = () => {
return StorageModel.getPath( return StorageModel.getPath(
@@ -740,7 +750,7 @@
const didStore = this.storeFileContent(fileContent); const didStore = this.storeFileContent(fileContent);
if (didStore == false) return; if (didStore == false) return;
this.taskModel.handleFileContent(fileContent); this.taskModel.handleFileContent(fileContent);
this.fileContentHandler(fileContent); this.fileContentHandlerManager.trigger(fileContent);
}; };
// methods // methods
addFileContent = (fileContent) => { addFileContent = (fileContent) => {
@@ -788,10 +798,6 @@
); );
return fileContent; return fileContent;
}; };
// other
setFileContentHandler = (handler) => {
this.fileContentHandler = handler;
};
// init // init
constructor(chatModel, storageModel2) { constructor(chatModel, storageModel2) {
this.chatModel = chatModel; this.chatModel = chatModel;
@@ -911,8 +917,7 @@
id; id;
info; info;
color; color;
chatMessageHandler = () => { chatMessageHandlerManager = new HandlerManager();
};
get secondaryChannels() { get secondaryChannels() {
return this.info.secondaryChannels.sort(localeCompare); return this.info.secondaryChannels.sort(localeCompare);
} }
@@ -970,7 +975,7 @@
if (chatMessage.body != "") { if (chatMessage.body != "") {
const messagePath = this.getMessagePath(chatMessage.id); const messagePath = this.getMessagePath(chatMessage.id);
this.storageModel.writeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandlerManager.trigger(chatMessage);
} }
this.fileModel.handleStringifiedFileContent(chatMessage.stringifiedFile); this.fileModel.handleStringifiedFileContent(chatMessage.stringifiedFile);
}; };
@@ -1020,10 +1025,6 @@
const dirPath = this.getBasePath(); const dirPath = this.getBasePath();
this.storageModel.removeRecursively(dirPath); this.storageModel.removeRecursively(dirPath);
}; };
// other
setMessageHandler = (handler) => {
this.chatMessageHandler = handler;
};
// load // load
loadInfo = () => { loadInfo = () => {
const info = this.storageModel.readStringifiable( const info = this.storageModel.readStringifiable(
@@ -1439,9 +1440,11 @@
// init // init
constructor(taskModel, storageModel2) { constructor(taskModel, storageModel2) {
this.taskModel = taskModel; this.taskModel = taskModel;
taskModel.setBoardHandler((boardInfoFileContent) => { taskModel.boardHandlerManager.addHandler(
(boardInfoFileContent) => {
this.showBoard(boardInfoFileContent); this.showBoard(boardInfoFileContent);
}); }
);
} }
}; };
@@ -1492,7 +1495,7 @@
); );
this.messagePageViewModel = new MessagePageViewModel(this); this.messagePageViewModel = new MessagePageViewModel(this);
this.settingsPageViewModel = new SettingsPageViewModel(this); this.settingsPageViewModel = new SettingsPageViewModel(this);
chatModel.setMessageHandler((chatMessage) => { chatModel.chatMessageHandlerManager.addHandler((chatMessage) => {
this.messagePageViewModel.showChatMessage(chatMessage); this.messagePageViewModel.showChatMessage(chatMessage);
}); });
this.loadPageSelection(); this.loadPageSelection();
+9 -10
View File
@@ -1,14 +1,18 @@
// this file is responsible for managing chats. // this file is responsible for managing chats.
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
import FileModel, { FileContent } from "../Files/fileModel";
import StorageModel, { filePaths } from "../Global/storageModel";
import { import {
DATA_VERSION,
ValidObject,
} from "../Utility/typeSafety";
import FileModel, { FileContent } from "../Files/fileModel";
import {
HandlerManager,
createTimestamp, createTimestamp,
localeCompare, localeCompare,
parseValidObject, parseValidObject,
stringify, stringify,
} from "../Utility/utility"; } from "../Utility/utility";
import StorageModel, { filePaths } from "../Global/storageModel";
import { decryptString, encryptString } from "../Utility/crypto"; import { decryptString, encryptString } from "../Utility/crypto";
import ChatListModel from "./chatListModel"; import ChatListModel from "./chatListModel";
@@ -30,7 +34,7 @@ export default class ChatModel {
info: ChatInfo; info: ChatInfo;
color: Color; color: Color;
chatMessageHandler: (chatMessage: ChatMessage) => void = () => {}; chatMessageHandlerManager: HandlerManager<ChatMessage> = new HandlerManager();
get secondaryChannels(): string[] { get secondaryChannels(): string[] {
return this.info.secondaryChannels.sort(localeCompare); return this.info.secondaryChannels.sort(localeCompare);
@@ -104,7 +108,7 @@ export default class ChatModel {
if (chatMessage.body != "") { if (chatMessage.body != "") {
const messagePath: string[] = this.getMessagePath(chatMessage.id); const messagePath: string[] = this.getMessagePath(chatMessage.id);
this.storageModel.writeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandlerManager.trigger(chatMessage);
} }
// file // file
@@ -173,11 +177,6 @@ export default class ChatModel {
this.storageModel.removeRecursively(dirPath); this.storageModel.removeRecursively(dirPath);
}; };
// other
setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => {
this.chatMessageHandler = handler;
};
// load // load
loadInfo = (): void => { loadInfo = (): void => {
const info: ChatInfo | null = this.storageModel.readStringifiable( const info: ChatInfo | null = this.storageModel.readStringifiable(
+4 -8
View File
@@ -1,12 +1,13 @@
// this file is responsible for managing files within chats. // this file is responsible for managing files within chats.
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety"; import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
import StorageModel, { filePaths } from "../Global/storageModel";
import { import {
HandlerManager,
createTimestamp, createTimestamp,
parseValidObject, parseValidObject,
stringify, stringify,
} from "../Utility/utility"; } from "../Utility/utility";
import StorageModel, { filePaths } from "../Global/storageModel";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
import TaskModel from "./taskModel"; import TaskModel from "./taskModel";
@@ -18,7 +19,7 @@ export default class FileModel {
taskModel: TaskModel; taskModel: TaskModel;
fileContentHandler: (fileContent: FileContent<string>) => void = () => {}; fileContentHandlerManager: HandlerManager<FileContent<string>> = new HandlerManager();
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
@@ -63,7 +64,7 @@ export default class FileModel {
if (didStore == false) return; if (didStore == false) return;
this.taskModel.handleFileContent(fileContent); this.taskModel.handleFileContent(fileContent);
this.fileContentHandler(fileContent); this.fileContentHandlerManager.trigger(fileContent);
}; };
// methods // methods
@@ -134,11 +135,6 @@ export default class FileModel {
return fileContent; return fileContent;
}; };
// other
setFileContentHandler = (handler: (fileContent: FileContent<string>) => void): void => {
this.fileContentHandler = handler;
};
// init // init
constructor(chatModel: ChatModel, storageModel: StorageModel) { constructor(chatModel: ChatModel, storageModel: StorageModel) {
this.chatModel = chatModel; this.chatModel = chatModel;
+5 -10
View File
@@ -8,6 +8,7 @@ import FileModel, { FileContent } from "./fileModel";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
import { Color } from "../../colors"; import { Color } from "../../colors";
import { HandlerManager } from "../Utility/utility";
import StorageModel from "../Global/storageModel"; import StorageModel from "../Global/storageModel";
import { v4 } from "uuid"; import { v4 } from "uuid";
@@ -16,7 +17,8 @@ export default class TaskModel {
chatModel: ChatModel; chatModel: ChatModel;
fileModel: FileModel; fileModel: FileModel;
boardHandler: (boardInfoFileContent: BoardInfoFileContent) => void = () => {}; boardHandlerManager: HandlerManager<BoardInfoFileContent> =
new HandlerManager();
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
@@ -78,9 +80,9 @@ export default class TaskModel {
}; };
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => { updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
this.boardHandler(boardInfoFileContent);
this.storeBoard(boardInfoFileContent); this.storeBoard(boardInfoFileContent);
this.chatModel.sendMessage("", boardInfoFileContent); this.chatModel.sendMessage("", boardInfoFileContent);
this.boardHandlerManager.trigger(boardInfoFileContent);
}; };
storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => { storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
@@ -163,13 +165,6 @@ export default class TaskModel {
this.storageModel.removeRecursively(taskReferencePath); this.storageModel.removeRecursively(taskReferencePath);
}; };
// other
setBoardHandler = (
handler: (boardInfoFileContent: BoardInfoFileContent) => void
): void => {
this.boardHandler = handler;
};
// init // init
constructor( constructor(
storageModel: StorageModel, storageModel: StorageModel,
@@ -181,7 +176,7 @@ export default class TaskModel {
this.storageModel = storageModel; this.storageModel = storageModel;
// handlers // handlers
fileModel.setFileContentHandler(this.handleFileContent); fileModel.fileContentHandlerManager.addHandler(this.handleFileContent);
} }
// utility // utility
+23
View File
@@ -5,6 +5,29 @@ export function createTimestamp(): string {
return new Date().toISOString(); return new Date().toISOString();
} }
// handlers
export type Handler<T> = (item: T) => void;
export class HandlerManager<T> {
handlers: Set<Handler<T>> = new Set();
// manage
addHandler = (handler: Handler<T>): void => {
this.handlers.add(handler);
};
deleteHandler = (handler: Handler<T>): void => {
this.handlers.delete(handler);
};
// trigger
trigger = (item: T): void => {
for (const handler of this.handlers) {
handler(item);
}
};
}
// string // string
export function stringify(data: any): string { export function stringify(data: any): string {
return JSON.stringify(data, null, 4); return JSON.stringify(data, null, 4);
+1 -1
View File
@@ -74,7 +74,7 @@ export default class ChatViewModel {
this.settingsPageViewModel = new SettingsPageViewModel(this); this.settingsPageViewModel = new SettingsPageViewModel(this);
// handlers // handlers
chatModel.setMessageHandler((chatMessage: ChatMessage) => { chatModel.chatMessageHandlerManager.addHandler((chatMessage: ChatMessage) => {
this.messagePageViewModel.showChatMessage(chatMessage); this.messagePageViewModel.showChatMessage(chatMessage);
}); });
+4 -2
View File
@@ -70,8 +70,10 @@ export default class TaskPageViewModel {
this.taskModel = taskModel; this.taskModel = taskModel;
// handlers // handlers
taskModel.setBoardHandler((boardInfoFileContent: BoardInfoFileContent) => { taskModel.boardHandlerManager.addHandler(
(boardInfoFileContent: BoardInfoFileContent) => {
this.showBoard(boardInfoFileContent); this.showBoard(boardInfoFileContent);
}); }
);
} }
} }