Files
CT/src/Model/model.ts
T

108 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-07-09 05:26:54 +02:00
import * as React from "bloatless-react";
2024-07-09 14:11:20 +02:00
import { Chat, chats } from "./chatModel";
2024-07-09 05:26:54 +02:00
import UDNFrontend from "udn-frontend";
export const UDN = new UDNFrontend();
// CONNECTION
// state
export const currentAddress = new React.State("");
2024-07-09 14:11:20 +02:00
export const isConnected = new React.State(false);
export const serverAddressInput = React.restoreState("socket-address", "");
2024-07-09 05:26:54 +02:00
export const cannotDisonnect = React.createProxyState(
[isConnected],
() => isConnected.value == false
);
export const cannotConnect = React.createProxyState(
2024-07-09 14:11:20 +02:00
[serverAddressInput, currentAddress, isConnected],
2024-07-09 05:26:54 +02:00
() =>
2024-07-09 14:11:20 +02:00
serverAddressInput.value == "" ||
(currentAddress.value == serverAddressInput.value &&
isConnected.value == true)
2024-07-09 05:26:54 +02:00
);
// methods
export function connect(): void {
if (cannotConnect.value == true) return;
2024-07-09 14:11:20 +02:00
currentAddress.value = serverAddressInput.value;
2024-07-09 05:26:54 +02:00
isConnected.value = false;
2024-07-09 14:11:20 +02:00
UDN.connect(serverAddressInput.value);
2024-07-09 05:26:54 +02:00
}
export function disconnect(): void {
if (cannotDisonnect.value == true) return;
UDN.disconnect();
}
2024-07-09 14:11:20 +02:00
export function resetAddressInput(): void {
this.serverAddressInput.value = this.serverAddress.value;
}
// listeners
2024-07-09 05:26:54 +02:00
UDN.onconnect = () => {
isConnected.value = true;
};
UDN.onmessage = (data) => {
2024-07-09 11:39:23 +02:00
chats.value.forEach((chat) => {
chat.onmessage(data);
2024-07-09 14:11:20 +02:00
});
2024-07-09 05:26:54 +02:00
};
UDN.ondisconnect = () => {
isConnected.value = false;
};
2024-07-09 14:11:20 +02:00
// MAILBOX
export const mailboxId = React.restoreState("mailbox-id", "");
export const isMailboxActive = new React.State(false);
export const cannotDeleteMailbox = React.createProxyState(
[mailboxId, isMailboxActive],
() => mailboxId.value == "" || isMailboxActive.value == false
);
export const cannotRequestMailbox = React.createProxyState(
[isConnected, isMailboxActive],
() => isConnected.value == false || isMailboxActive.value == true
);
export function requestMailbox(): void {
if (cannotRequestMailbox.value == true) return;
UDN.requestMailbox();
}
export function deleteMailbox(): void {
if (cannotDeleteMailbox.value == true) return;
UDN.deleteMailbox(mailboxId.value);
}
UDN.onmailboxcreate = (id) => {
mailboxId.value = id;
UDN.connectMailbox(id);
}
UDN.onmailboxconnect = () => {
isMailboxActive.value = true;
}
UDN.onmailboxdelete = () => {
isMailboxActive.value = false;
mailboxId.value = "";
}
// MISC
export const isEncryptionUnavailable = window.crypto.subtle == undefined;
export const senderName = React.restoreState("sender-name", "");
export const selectedChat = new React.State<Chat | undefined>(undefined);
2024-07-09 05:26:54 +02:00
// INIT
2024-07-09 14:11:20 +02:00
if (serverAddressInput.value != "") {
2024-07-09 05:26:54 +02:00
connect();
}