2024-07-09 05:26:54 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
|
|
|
|
import UDNFrontend from "udn-frontend";
|
2024-07-09 11:39:23 +02:00
|
|
|
import { chats } from "./chatModel";
|
2024-07-09 05:26:54 +02:00
|
|
|
import { serverAddress } from "./viewModel";
|
|
|
|
|
|
|
|
|
|
export const UDN = new UDNFrontend();
|
|
|
|
|
|
|
|
|
|
// CONNECTION
|
|
|
|
|
// state
|
|
|
|
|
export const isConnected = new React.State(false);
|
|
|
|
|
export const currentAddress = new React.State("");
|
|
|
|
|
|
|
|
|
|
export const cannotDisonnect = React.createProxyState(
|
|
|
|
|
[isConnected],
|
|
|
|
|
() => isConnected.value == false
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const cannotConnect = React.createProxyState(
|
|
|
|
|
[serverAddress, currentAddress, isConnected],
|
|
|
|
|
() =>
|
|
|
|
|
serverAddress.value == "" ||
|
|
|
|
|
(currentAddress.value == serverAddress.value && isConnected.value == true)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
|
export function connect(): void {
|
|
|
|
|
if (cannotConnect.value == true) return;
|
|
|
|
|
currentAddress.value = serverAddress.value;
|
|
|
|
|
isConnected.value = false;
|
|
|
|
|
UDN.connect(serverAddress.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function disconnect(): void {
|
|
|
|
|
if (cannotDisonnect.value == true) return;
|
|
|
|
|
UDN.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LISTENERS
|
|
|
|
|
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 05:26:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UDN.ondisconnect = () => {
|
|
|
|
|
isConnected.value = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// INIT
|
|
|
|
|
if (serverAddress.value != "") {
|
|
|
|
|
connect();
|
|
|
|
|
}
|