import { goto, invalidate } from "$app/navigation";
import { navigating, page } from "$app/stores";
import { PUBLIC_SOCKET } from "$env/static/public";
import { event, invoice, last, request } from "$lib/store";
import { s, sleep, success, wait } from "$lib/utils";
import { get } from "svelte/store";
let socket;
let token;
export const auth = () => token && send("login", token);
export const send = async (type, data) => {
try {
await wait(() => socket && socket.readyState === 1, 1000, 10);
} catch (e: any) {
if (e.message === "timeout") reconnectToWebsocket();
}
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);
},
request() {
request.set(data);
invalidate("app:invoice");
},
async payment() {
const { amount, confirmed, iid } = data;
invalidate("app:user");
invalidate("app:invoice");
invalidate("app:payments");
const {
url: { pathname },
} = get(page);
if (amount > 0) {
if (pathname.includes("receive")) {
await wait(() => !get(navigating));
await goto(`/invoice/${iid}`);
} else success(`${confirmed ? "Received" : "Detected"} ⚡️${s(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(PUBLIC_SOCKET);
socket.addEventListener("open", onWebsocketOpen);
socket.addEventListener("close", onWebsocketClose);
socket.addEventListener("message", onWebsocketMessage);
}
export function close() {
if (socket) socket.close();
}
async function onWebsocketMessage(msg) {
const { type, data } = JSON.parse(msg.data);
if (get(navigating)) await sleep(2000);
if (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(token);
}
lue='notice'>notice
| coinos ui — legacy coinos.io web app (SvelteKit) | coinos |
blob: 51f6e3d0ccbc326cf4db7b8a8d5520672faa519b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
|
import { auth, post } from "$lib/utils";
export async function POST({ cookies, request }) {
const body = await request.json();
await post("/payments", body, auth(cookies));
return new Response(JSON.stringify("ok"), {
headers: { "content-type": "application/json" },
});
}
|