Improved a few things
This commit is contained in:
+9
-11
@@ -69,10 +69,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
|||||||
return connecting;
|
return connecting;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (socket && currentOrigin !== origin) {
|
if (socket && currentOrigin !== origin) closeClient();
|
||||||
socket.close();
|
|
||||||
socket = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
connecting = new Promise<WebSocket>((resolve, reject) => {
|
connecting = new Promise<WebSocket>((resolve, reject) => {
|
||||||
const ws = new WebSocket(origin);
|
const ws = new WebSocket(origin);
|
||||||
@@ -88,7 +85,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
|||||||
currentOrigin = origin;
|
currentOrigin = origin;
|
||||||
connecting = null;
|
connecting = null;
|
||||||
|
|
||||||
await sendUnsafe(useLocalhost, {
|
await sendUnsafe({
|
||||||
type: "auth",
|
type: "auth",
|
||||||
cookies: authCookies,
|
cookies: authCookies,
|
||||||
});
|
});
|
||||||
@@ -146,10 +143,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
|||||||
RPC helpers
|
RPC helpers
|
||||||
────────────────────────── */
|
────────────────────────── */
|
||||||
|
|
||||||
async function sendUnsafe<T>(
|
async function sendUnsafe<T>(payload: Record<string, any>): Promise<T> {
|
||||||
useLocalhost: boolean,
|
|
||||||
payload: Record<string, any>,
|
|
||||||
): Promise<T> {
|
|
||||||
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||||
throw new Error("WebSocket is not open");
|
throw new Error("WebSocket is not open");
|
||||||
}
|
}
|
||||||
@@ -164,7 +158,11 @@ async function sendUnsafe<T>(
|
|||||||
|
|
||||||
pending.set(requestId, { resolve, reject, timeout });
|
pending.set(requestId, { resolve, reject, timeout });
|
||||||
|
|
||||||
socket?.send(JSON.stringify({ ...payload, requestId }));
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||||
|
throw new Error("WebSocket closed while sending");
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.send(JSON.stringify({ ...payload, requestId }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +171,7 @@ async function send<T>(
|
|||||||
payload: Record<string, any>,
|
payload: Record<string, any>,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
await ensureSocket(useLocalhost);
|
await ensureSocket(useLocalhost);
|
||||||
return sendUnsafe(useLocalhost, payload);
|
return sendUnsafe(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ──────────────────────────
|
/* ──────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user