aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/socket.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-11 17:51:24 -0800
committerAdam Soltys <[email protected]>2026-02-11 17:51:24 -0800
commit13466248004327e9a026cb89f94926f9b060d1c1 (patch)
tree47b4972c33809ccdf866b043c92cd3e0ecf5dcd0 /src/lib/socket.ts
parent3aa9ce1b5985397d7f568887ec6f881df13c1034 (diff)
downloadcoinos-ui-13466248004327e9a026cb89f94926f9b060d1c1.tar.gz
coinos-ui-13466248004327e9a026cb89f94926f9b060d1c1.zip
linting
Diffstat (limited to 'src/lib/socket.ts')
-rw-r--r--src/lib/socket.ts138
1 files changed, 69 insertions, 69 deletions
diff --git a/src/lib/socket.ts b/src/lib/socket.ts
index b87cc165..e40eb39b 100644
--- a/src/lib/socket.ts
+++ b/src/lib/socket.ts
@@ -12,57 +12,57 @@ let token;
export const auth = () => token && send("login", token);
export const send = async (type, data) => {
- try {
- await wait(() => socket?.readyState === 1, 1000, 10);
- } catch (e) {
- const { message } = e as Error;
- if (message === "timeout") reconnectToWebsocket();
- }
+ try {
+ await wait(() => socket?.readyState === 1, 1000, 10);
+ } catch (e) {
+ const { message } = e as Error;
+ if (message === "timeout") reconnectToWebsocket();
+ }
- await wait(() => socket?.readyState === 1, 1000, 10);
- socket.send(JSON.stringify({ type, data }));
+ await wait(() => socket?.readyState === 1, 1000, 10);
+ socket.send(JSON.stringify({ type, data }));
};
export const messages = (data) => ({
- id() {
- last.set(Date.now());
- },
- invoice() {
- invoice.set(data);
- },
- event() {
- event.set(data);
- },
+ id() {
+ last.set(Date.now());
+ },
+ invoice() {
+ invoice.set(data);
+ },
+ event() {
+ event.set(data);
+ },
- request() {
- request.set(data);
- invalidate("app:invoice");
- },
+ request() {
+ request.set(data);
+ invalidate("app:invoice");
+ },
- async payment() {
- const { amount, confirmed, iid } = data;
- invalidate("app:user");
- invalidate("app:invoice");
- invalidate("app:payments");
+ async payment() {
+ const { amount, confirmed, iid } = data;
+ invalidate("app:user");
+ invalidate("app:invoice");
+ invalidate("app:payments");
- const {
- url: { pathname },
- } = get(page);
+ const {
+ url: { pathname },
+ } = get(page);
- const username = cookies.get("username");
+ const username = cookies.get("username");
- if (amount > 0) {
- if (
- pathname.includes(`/${username}`) ||
- pathname.includes("/receive") ||
- pathname.includes("/invoice")
- ) {
- if (!get(navigating)) goto(`/invoice/${iid}`);
- } else success(`${confirmed ? "Received" : "Detected"} ⚡️${s(amount)}!`);
- }
- },
+ if (amount > 0) {
+ if (
+ pathname.includes(`/${username}`) ||
+ pathname.includes("/receive") ||
+ pathname.includes("/invoice")
+ ) {
+ if (!get(navigating)) goto(`/invoice/${iid}`);
+ } else success(`${confirmed ? "Received" : "Detected"} ⚡️${s(amount)}!`);
+ }
+ },
- connected: auth,
+ connected: auth,
});
const initialReconnectDelay = 1000;
@@ -72,51 +72,51 @@ let currentReconnectDelay = initialReconnectDelay;
let connecting;
export function connect(t) {
- if (connecting) return;
- connecting = true;
- setTimeout(() => {
- connecting = false;
- }, 5000);
+ if (connecting) return;
+ connecting = true;
+ setTimeout(() => {
+ connecting = false;
+ }, 5000);
- token = t;
+ token = t;
- if (socket) return auth();
+ if (socket) return auth();
- socket = new WebSocket(PUBLIC_SOCKET);
- socket.addEventListener("open", onWebsocketOpen);
- socket.addEventListener("close", onWebsocketClose);
- socket.addEventListener("message", onWebsocketMessage);
+ socket = new WebSocket(PUBLIC_SOCKET);
+ socket.addEventListener("open", onWebsocketOpen);
+ socket.addEventListener("close", onWebsocketClose);
+ socket.addEventListener("message", onWebsocketMessage);
}
export function close() {
- if (socket) socket.close();
+ if (socket) socket.close();
}
async function onWebsocketMessage(msg) {
- const { type, data } = JSON.parse(msg.data);
- if (get(navigating)) await sleep(2000);
+ const { type, data } = JSON.parse(msg.data);
+ if (get(navigating)) await sleep(2000);
- if (messages(data)[type]) messages(data)[type]();
+ if (messages(data)[type]) messages(data)[type]();
}
function onWebsocketOpen() {
- currentReconnectDelay = initialReconnectDelay;
- send("heartbeat", token);
+ currentReconnectDelay = initialReconnectDelay;
+ send("heartbeat", token);
}
function onWebsocketClose() {
- socket = null;
- setTimeout(
- () => {
- reconnectToWebsocket();
- },
- currentReconnectDelay + Math.floor(Math.random() * 3000),
- );
+ socket = null;
+ setTimeout(
+ () => {
+ reconnectToWebsocket();
+ },
+ currentReconnectDelay + Math.floor(Math.random() * 3000),
+ );
}
function reconnectToWebsocket() {
- if (currentReconnectDelay < maxReconnectDelay) {
- currentReconnectDelay *= 2;
- }
- connect(token);
+ if (currentReconnectDelay < maxReconnectDelay) {
+ currentReconnectDelay *= 2;
+ }
+ connect(token);
}