Files
CT/src/Model/connectionModel.ts
T

154 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-07-19 17:56:25 +02:00
// this file is responsible for managing UDN connections.
import StorageModel, { storageKeys } from "./storageModel";
2024-07-19 17:56:25 +02:00
import UDNFrontend, { Message } from "udn-frontend";
2024-07-19 19:47:40 +02:00
import { ChatMessage } from "./chatModel";
2024-07-20 20:03:46 +02:00
import { checkIsValidObject } from "./Utility/typeSafety";
2024-07-19 23:45:07 +02:00
import { stringify } from "./Utility/utility";
2024-07-19 17:56:25 +02:00
export default class ConnectionModel {
udn: UDNFrontend;
storageModel: StorageModel;
2024-07-19 17:56:25 +02:00
// data
get isConnected(): boolean {
return this.udn.ws != undefined && this.udn.ws.readyState == 1;
}
get address(): string | undefined {
2024-07-19 17:56:25 +02:00
return this.udn.ws?.url;
}
2024-07-20 22:44:23 +02:00
connectionChangeHandler: () => void = () => {};
messageHandler: (data: Message) => void = () => {};
2024-07-19 17:56:25 +02:00
// connection
connect = (address: string): void => {
2024-07-19 17:56:25 +02:00
this.udn.connect(address);
};
2024-07-19 17:56:25 +02:00
disconnect = (): void => {
2024-07-19 17:56:25 +02:00
this.udn.disconnect();
};
2024-07-19 17:56:25 +02:00
2024-07-20 22:44:23 +02:00
handleMessage = (data: Message): void => {
this.messageHandler(data);
};
2024-07-20 20:42:43 +02:00
2024-07-20 02:18:15 +02:00
handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address);
2024-07-20 22:44:23 +02:00
this.connectionChangeHandler();
if (this.isConnected == false) return;
if (this.address) {
this.storeAddress(this.address);
}
2024-07-20 22:44:23 +02:00
this.sendMessagesInOutbox();
};
2024-07-19 17:56:25 +02:00
2024-07-20 20:03:46 +02:00
// outbox
getOutboxMessags = (): ChatMessage[] => {
const outboxPath: string[] = storageKeys.outbox;
const messageIds: string[] = this.storageModel.list(outboxPath);
let messages: ChatMessage[] = [];
for (const messageId of messageIds) {
const message: any = this.storageModel.restoreStringifiable([
...outboxPath,
messageId,
]);
if (message == undefined) continue;
if (checkIsValidObject(message) == false) continue;
2024-07-20 22:44:23 +02:00
messages.push(message);
2024-07-20 20:03:46 +02:00
}
return messages;
};
addToOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
this.storageModel.storeStringifiable(messagePath, chatMessage);
};
removeFromOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
this.storageModel.remove(messagePath);
};
sendMessagesInOutbox = (): void => {
const messages: ChatMessage[] = this.getOutboxMessags();
for (const message of messages) {
const isSent = this.tryToSendMessage(message);
if (isSent == false) return;
this.removeFromOutbox(message);
}
};
2024-07-19 17:56:25 +02:00
// messaging
2024-07-20 20:03:46 +02:00
sendMessageOrStore = (chatMessage: ChatMessage): void => {
const isSent = this.tryToSendMessage(chatMessage);
if (isSent == true) return;
this.addToOutbox(chatMessage);
};
tryToSendMessage = (chatMessage: ChatMessage): boolean => {
2024-07-19 20:50:42 +02:00
const stringifiedBody: string = stringify(chatMessage);
2024-07-19 17:56:25 +02:00
return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
};
2024-07-19 17:56:25 +02:00
// storage
private getAddressPath = (address: string): string[] => {
const dirPath = storageKeys.previousAddresses;
return [...dirPath, address];
};
storeAddress = (address: string): void => {
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
};
removeAddress = (address: string): void => {
const addressPath = this.getAddressPath(address);
this.storageModel.remove(addressPath);
};
2024-07-20 02:59:40 +02:00
get addresses(): string[] {
const dirPath = storageKeys.previousAddresses;
return this.storageModel.list(dirPath);
2024-07-20 20:03:46 +02:00
}
2024-07-20 22:44:23 +02:00
// handlers
setConnectionChangeHandler = (handler: () => void): void => {
this.connectionChangeHandler = handler;
};
setMessageHandler = (handler: (data: Message) => void): void => {
this.messageHandler = handler;
};
2024-07-19 17:56:25 +02:00
// setup
2024-07-20 20:42:43 +02:00
constructor(storageModel: StorageModel) {
2024-07-19 18:17:00 +02:00
// create frontend
2024-07-19 17:56:25 +02:00
this.udn = new UDNFrontend();
2024-07-20 20:42:43 +02:00
this.storageModel = storageModel;
2024-07-19 18:17:00 +02:00
// setup handlers
this.udn.onmessage = (data: Message) => {
2024-07-20 20:42:43 +02:00
this.handleMessage(data);
2024-07-19 18:17:00 +02:00
};
this.udn.onconnect = () => {
this.handleConnectionChange();
2024-07-19 18:17:00 +02:00
};
this.udn.ondisconnect = () => {
this.handleConnectionChange();
2024-07-19 18:17:00 +02:00
};
2024-07-19 17:56:25 +02:00
}
2024-07-20 22:44:23 +02:00
}