diff options
| author | Adam Soltys <[email protected]> | 2024-10-11 22:56:28 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-11 22:56:28 -0700 |
| commit | b8aa7709e8a43017eb9c705033ea8702364ff6f7 (patch) | |
| tree | d287109122adbc3ab2a70d00bec1736d016f7ea9 /src/lib/socket.ts | |
| parent | 366013fe696882ef75e6adb56f5e5dbb5356af5e (diff) | |
| download | coinos-ui-b8aa7709e8a43017eb9c705033ea8702364ff6f7.tar.gz coinos-ui-b8aa7709e8a43017eb9c705033ea8702364ff6f7.zip | |
socket improvements
Diffstat (limited to 'src/lib/socket.ts')
| -rw-r--r-- | src/lib/socket.ts | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/lib/socket.ts b/src/lib/socket.ts index c82810c9..26ab543c 100644 --- a/src/lib/socket.ts +++ b/src/lib/socket.ts @@ -1,17 +1,24 @@ import { navigating } from "$app/stores"; import { get } from "svelte/store"; import { event, invoice, request, last } from "$lib/store"; -import { success, sat, sleep } from "$lib/utils"; +import { success, sat, sleep, wait } from "$lib/utils"; import { PUBLIC_SOCKET } from "$env/static/public"; import { invalidate } from "$app/navigation"; -let socket, token; +let socket; +let token; export const auth = () => token && send("login", token); -export const send = (type, data) => { - socket?.readyState === 1 && socket.send(JSON.stringify({ type, data })); - return true; +export const send = async (type, data) => { + try { + await wait(() => socket && socket.readyState === 1, 100, 10); + } catch (e: any) { + if (e.message === "timeout") reconnectToWebsocket(); + } + + await wait(() => socket.readyState === 1, 100, 10); + socket.send(JSON.stringify({ type, data })); }; export const messages = (data) => ({ @@ -32,7 +39,6 @@ export const messages = (data) => ({ async payment() { let { amount, confirmed } = data; - invalidate("app:user"); invalidate("app:invoice"); invalidate("app:payments"); |
