Skip to content

Commit

Permalink
fix: tweak re-connect
Browse files Browse the repository at this point in the history
  • Loading branch information
v0l committed Jun 18, 2024
1 parent d8e0c93 commit 1a405f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/app/src/Cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ export async function initRelayWorker() {
if (!conn) {
conn = await tryUseCacheRelay("ws://umbrel:4848");
}
if (conn) return;
if (conn) {
window.location.reload();
return;
}
} else if (Relay instanceof ConnectionCacheRelay) {
await Relay.connection.connect(true);
return;
}
} catch (e) {
localStorage.removeItem("cache-relay");
console.error(e);
if (cacheRelay) {
window.location.reload();
}
}

try {
Expand Down
12 changes: 6 additions & 6 deletions packages/system/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
#activeRequests = new Set<string>();
#connectStarted = false;
#syncModule?: ConnectionSyncModule;
#wasUp = false;

id: string;
readonly address: string;
Expand Down Expand Up @@ -169,19 +170,20 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
#onOpen(wasReconnect: boolean) {
this.#downCount = 0;
this.#connectStarted = false;
this.#wasUp = true;
this.#log(`Open!`);
this.#setupEphemeral();
this.emit("connected", wasReconnect);
this.#sendPendingRaw();
}

#onClose(e: WebSocket.CloseEvent) {
// remote server closed the connection, dont re-connect
if (!this.#closing) {
// if not explicity closed or closed after, start re-connect timer
if (this.#wasUp && !this.#closing) {
this.#downCount++;
this.#reconnectTimer(e);
} else {
this.#log(`Closed!`);
this.#log(`Closed: connecting=${this.#connectStarted}, closing=${this.#closing}`);
this.#downCount = 0;
if (this.ReconnectTimer) {
clearTimeout(this.ReconnectTimer);
Expand All @@ -206,9 +208,7 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
this.ReconnectTimer = undefined;
try {
this.connect();
} catch {
this.emit("disconnect", -1);
}
} catch {}
}, this.ConnectTimeout);
}

Expand Down

0 comments on commit 1a405f7

Please sign in to comment.