BUGFIX: Fix issues with RFA auto-reconnecting feature (#2535)

This commit is contained in:
catloversg
2026-02-27 04:34:28 +07:00
committed by GitHub
parent 47d8db8b91
commit cfb536cd01
2 changed files with 19 additions and 5 deletions

View File

@@ -21,6 +21,10 @@ declare interface Document {
achievements: string[]; achievements: string[];
} }
declare interface WebSocket {
intentionallyClosed?: boolean;
}
declare global { declare global {
/** /**
* "loader" is not exposed in the public API. * "loader" is not exposed in the public API.

View File

@@ -6,12 +6,12 @@ import { Settings } from "../Settings/Settings";
import { EventEmitter } from "../utils/EventEmitter"; import { EventEmitter } from "../utils/EventEmitter";
import type { getRemoteFileApiConnectionStatus } from "./RemoteFileAPI"; import type { getRemoteFileApiConnectionStatus } from "./RemoteFileAPI";
const timeOutIds = new Set<number>();
function showErrorMessage(address: string, detail: string) { function showErrorMessage(address: string, detail: string) {
SnackbarEvents.emit(`Error with websocket ${address}, details: ${detail}`, ToastVariant.ERROR, 5000); SnackbarEvents.emit(`Error with websocket ${address}, details: ${detail}`, ToastVariant.ERROR, 5000);
} }
const eventCodeWhenIntentionallyStoppingConnection = 3000;
export const RemoteFileApiConnectionEvents = new EventEmitter<[ReturnType<typeof getRemoteFileApiConnectionStatus>]>(); export const RemoteFileApiConnectionEvents = new EventEmitter<[ReturnType<typeof getRemoteFileApiConnectionStatus>]>();
export class Remote { export class Remote {
@@ -26,7 +26,15 @@ export class Remote {
} }
public stopConnection(): void { public stopConnection(): void {
this.connection?.close(eventCodeWhenIntentionallyStoppingConnection); // Cancel all pending retries immediately. This function is only called when we intentionally close the current
// connection before starting a new one. The new connection will retry on its own if needed.
timeOutIds.forEach((id) => window.clearTimeout(id));
timeOutIds.clear();
if (this.connection) {
this.connection.intentionallyClosed = true;
}
this.connection?.close();
RemoteFileApiConnectionEvents.emit("Offline"); RemoteFileApiConnectionEvents.emit("Offline");
} }
@@ -69,7 +77,7 @@ export class Remote {
* unexpectedly (e.g., show a warning, reconnect after a delay), so we need to check whether the close event is * unexpectedly (e.g., show a warning, reconnect after a delay), so we need to check whether the close event is
* unexpected. * unexpected.
*/ */
if (event.code === eventCodeWhenIntentionallyStoppingConnection) { if (event.currentTarget instanceof WebSocket && event.currentTarget.intentionallyClosed) {
return; return;
} }
@@ -83,7 +91,8 @@ export class Remote {
if (Settings.RemoteFileApiReconnectionDelay > 0) { if (Settings.RemoteFileApiReconnectionDelay > 0) {
this.reconnecting = true; this.reconnecting = true;
setTimeout(() => { const timeOutId = window.setTimeout(() => {
timeOutIds.delete(timeOutId);
if (autoConnectAttempt === 1) { if (autoConnectAttempt === 1) {
SnackbarEvents.emit(`Attempting to auto connect Remote API`, ToastVariant.WARNING, 2000); SnackbarEvents.emit(`Attempting to auto connect Remote API`, ToastVariant.WARNING, 2000);
} }
@@ -93,6 +102,7 @@ export class Remote {
this.startConnection(attempts); this.startConnection(attempts);
}, Settings.RemoteFileApiReconnectionDelay * 1000); }, Settings.RemoteFileApiReconnectionDelay * 1000);
timeOutIds.add(timeOutId);
RemoteFileApiConnectionEvents.emit("Reconnecting"); RemoteFileApiConnectionEvents.emit("Reconnecting");
} else { } else {
this.reconnecting = false; this.reconnecting = false;