Convert reconnect method to exponential backoff timeout (#1293)

* Convert reconnect method to exponential backoff timeout

* Reset to the starting duration after getting a response

* Fix broken scope enclosures
This commit is contained in:
Nicholas Galauxy 2024-05-24 17:57:49 -05:00 committed by GitHub
parent c8b77cffc1
commit 815b37d23c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ import { updateUserInfo } from "#app/account";
export default class UnavailableModalUiHandler extends ModalUiHandler { export default class UnavailableModalUiHandler extends ModalUiHandler {
private reconnectTimer: number; private reconnectTimer: number;
private reconnectInterval: number; private reconnectDuration: number;
private reconnectCallback: () => void; private reconnectCallback: () => void;
private readonly minTime = 1000 * 5; private readonly minTime = 1000 * 5;
@ -16,7 +16,7 @@ export default class UnavailableModalUiHandler extends ModalUiHandler {
constructor(scene: BattleScene, mode?: Mode) { constructor(scene: BattleScene, mode?: Mode) {
super(scene, mode); super(scene, mode);
this.reconnectInterval = this.minTime; this.reconnectDuration = this.minTime;
} }
getModalTitle(): string { getModalTitle(): string {
@ -51,19 +51,17 @@ export default class UnavailableModalUiHandler extends ModalUiHandler {
tryReconnect(): void { tryReconnect(): void {
updateUserInfo().then(response => { updateUserInfo().then(response => {
if (response[0] || [200, 400].includes(response[1])) { if (response[0] || [200, 400].includes(response[1])) {
clearInterval(this.reconnectTimer);
this.reconnectTimer = null; this.reconnectTimer = null;
this.reconnectInterval = this.minTime; this.reconnectDuration = this.minTime;
this.scene.playSound("pb_bounce_1"); this.scene.playSound("pb_bounce_1");
this.reconnectCallback(); this.reconnectCallback();
} else { } else {
clearInterval(this.reconnectTimer); this.reconnectDuration = Math.min(this.reconnectDuration * 2, this.maxTime); // Set a max delay so it isn't infinite
this.reconnectInterval = Math.min(this.reconnectInterval * 2, this.maxTime); // Set a max delay so it isn't infinite
this.reconnectTimer = this.reconnectTimer =
setTimeout( setTimeout(
() => this.tryReconnect(), () => this.tryReconnect(),
// Adds a random factor to avoid pendulum effect during long total breakdown // Adds a random factor to avoid pendulum effect during long total breakdown
this.reconnectInterval + (Math.random() * this.randVarianceTime)); this.reconnectDuration + (Math.random() * this.randVarianceTime));
} }
}); });
} }
@ -75,8 +73,8 @@ export default class UnavailableModalUiHandler extends ModalUiHandler {
}; };
this.reconnectCallback = args[0]; this.reconnectCallback = args[0];
this.reconnectDuration = this.minTime;
this.reconnectTimer = setInterval(() => this.tryReconnect(), this.reconnectInterval); this.reconnectTimer = setTimeout(() => this.tryReconnect(), this.reconnectDuration);
return super.show([ config ]); return super.show([ config ]);
} }