diff options
| author | Adam Soltys <adam@coinos.io> | 2024-11-16 21:19:34 -0800 |
|---|---|---|
| committer | Adam Soltys <adam@coinos.io> | 2024-11-16 21:19:34 -0800 |
| commit | b3653c0a6d448d215c96511ae88c5f3bc741e52f (patch) | |
| tree | 89dbc4e3f01be23883fb542df18b275d3454db33 /bun.lockb | |
| parent | 1b6ac8df832ad6f4b469b2197c6b529f8d9d757f (diff) | |
| download | coinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.tar.gz coinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.zip | |
get system theme
Diffstat (limited to 'bun.lockb')
| -rwxr-xr-x | bun.lockb | bin | 184167 -> 184530 bytes |
1 files changed, 0 insertions, 0 deletions
import { get } from 'svelte/store';
import { invoices, invoice, request, newPayment, last, rate, txns, user } from '$lib/store';
import { success, sat } from '$lib/utils';
import { env } from '$env/dynamic/public';
import { invalidate } from '$app/navigation';
const socketUrl = env.PUBLIC_SOCKET;
const btc = env.PUBLIC_BTC;
let socket, token;
export const auth = () => token && send('login', token) && send('heartbeat');
export const send = (type, data) => {
socket?.readyState === 1 && socket.send(JSON.stringify({ type, data }));
return true;
};
export const messages = (data) => ({
id() {
last.set(Date.now());
},
invoice() {
invoice.set(data);
},
rate() {
rate.set(data);
},
request() {
request.set(data);
},
async payment() {
let { amount, invoice } = data;
if (invoice) invalidate('app:invoice');
if (get(user)?.account_id !== data.account_id) return;
let payments = get(txns);
let i = payments.findIndex((p) => p.id === data.id);
if (~i) (payments[i] = data), txns.set(payments);
else newPayment.set(true);
invalidate((url) => url.pathname === '/payments');
if (amount > 0) {
success(`${data.confirmed ? 'Received' : 'Detected'} ${sat(amount)}!`);
} else {
success(`Sent ${sat(amount)}!`);
}
},
connected: auth
});
const initialReconnectDelay = 1000;
const maxReconnectDelay = 16000;
let currentReconnectDelay = initialReconnectDelay;
export function connect(t) {
token = t;
if (socket) return auth();
socket = new WebSocket(socketUrl);
socket.addEventListener('open', onWebsocketOpen);
socket.addEventListener('close', onWebsocketClose);
socket.addEventListener('message', onWebsocketMessage);
}
export function close() {
if (socket) socket.close();
}
function onWebsocketMessage(msg) {
let { type, data } = JSON.parse(msg.data);
messages(data)[type] && messages(data)[type]();
}
function onWebsocketOpen() {
currentReconnectDelay = initialReconnectDelay;
}
function onWebsocketClose() {
socket = null;
setTimeout(() => {
reconnectToWebsocket();
}, currentReconnectDelay + Math.floor(Math.random() * 3000));
}
function reconnectToWebsocket() {
if (currentReconnectDelay < maxReconnectDelay) {
currentReconnectDelay *= 2;
}
connect();
}
|
