fix: dev client script being loaded twice (#1944)

This commit is contained in:
Marvin Hagemeister
2023-10-16 09:47:56 +02:00
committed by GitHub
parent 206d5002d6
commit bbedea5377
2 changed files with 9 additions and 11 deletions
@@ -1,7 +1,5 @@
let ws: WebSocket; let ws: WebSocket;
let closed = false;
let reconnectTimer: number; let reconnectTimer: number;
const backoff = [ const backoff = [
0, 0,
@@ -25,7 +23,7 @@ const backoff = [
]; ];
let backoffIdx = 0; let backoffIdx = 0;
function reconnect() { function reconnect() {
if (!closed) return; if (ws.readyState !== ws.CLOSED) return;
reconnectTimer = setTimeout(() => { reconnectTimer = setTimeout(() => {
if (backoffIdx === 0) { if (backoffIdx === 0) {
@@ -39,7 +37,6 @@ function reconnect() {
try { try {
connect(true); connect(true);
closed = false;
clearTimeout(reconnectTimer); clearTimeout(reconnectTimer);
} catch (_err) { } catch (_err) {
reconnect(); reconnect();
@@ -54,18 +51,19 @@ function connect(forceReload?: boolean) {
); );
ws.addEventListener("open", () => { ws.addEventListener("open", () => {
console.log(
`%c Fresh %c Connected to development server.`,
"background-color: #86efac; color: black",
"color: inherit",
);
if (forceReload) { if (forceReload) {
location.reload(); location.reload();
} else {
backoffIdx = 0;
console.log(
`%c Fresh %c Connected to development server.`,
"background-color: #86efac; color: black",
"color: inherit",
);
} }
}); });
ws.addEventListener("close", () => { ws.addEventListener("close", () => {
closed = true;
reconnect(); reconnect();
}); });
+1 -1
View File
@@ -1,3 +1,3 @@
import "preact/debug"; import "preact/debug";
import "./client.ts"; import "../client.ts";
export * from "./main.ts"; export * from "./main.ts";