2024-05-31 20:09:43 +00:00
|
|
|
(() => {
|
2026-07-10 07:01:41 +00:00
|
|
|
const maximumRetryCount = 12;
|
2024-05-31 20:09:43 +00:00
|
|
|
const reconnectModal = document.getElementById('reconnect-modal');
|
2026-07-10 07:01:41 +00:00
|
|
|
const retryDelaysMilliseconds = [
|
|
|
|
|
0,
|
|
|
|
|
1_000,
|
|
|
|
|
2_000,
|
|
|
|
|
5_000,
|
|
|
|
|
10_000,
|
|
|
|
|
15_000,
|
|
|
|
|
30_000,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let currentReconnectionProcess = null;
|
|
|
|
|
let isConnectionDown = false;
|
|
|
|
|
|
|
|
|
|
const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
|
|
|
|
|
|
|
|
const getRetryDelayMilliseconds = attempt => retryDelaysMilliseconds[Math.min(attempt, retryDelaysMilliseconds.length - 1)];
|
|
|
|
|
|
|
|
|
|
const showReconnectModal = () => {
|
|
|
|
|
if (reconnectModal)
|
|
|
|
|
reconnectModal.style.display = 'flex';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hideReconnectModal = () => {
|
|
|
|
|
if (reconnectModal)
|
|
|
|
|
reconnectModal.style.display = 'none';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setReconnectModalText = text => {
|
|
|
|
|
if (reconnectModal)
|
|
|
|
|
reconnectModal.textContent = text;
|
|
|
|
|
};
|
2024-05-31 20:09:43 +00:00
|
|
|
|
|
|
|
|
const startReconnectionProcess = () => {
|
2026-07-10 07:01:41 +00:00
|
|
|
showReconnectModal();
|
2024-05-31 20:09:43 +00:00
|
|
|
|
|
|
|
|
let isCanceled = false;
|
2026-07-10 07:01:41 +00:00
|
|
|
let forceAttempt = false;
|
|
|
|
|
|
|
|
|
|
const waitForNextAttempt = async milliseconds => {
|
|
|
|
|
const startedAt = Date.now();
|
|
|
|
|
while (!isCanceled && !forceAttempt && Date.now() - startedAt < milliseconds)
|
|
|
|
|
await delay(250);
|
2024-05-31 20:09:43 +00:00
|
|
|
|
2026-07-10 07:01:41 +00:00
|
|
|
forceAttempt = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void (async () => {
|
|
|
|
|
for (let attempt = 0; attempt < maximumRetryCount && !isCanceled; attempt++) {
|
|
|
|
|
setReconnectModalText(`Reconnecting to AI Studio (${attempt + 1}/${maximumRetryCount})...`);
|
2024-05-31 20:09:43 +00:00
|
|
|
|
2026-07-10 07:01:41 +00:00
|
|
|
const retryDelayMilliseconds = getRetryDelayMilliseconds(attempt);
|
|
|
|
|
if (retryDelayMilliseconds > 0)
|
|
|
|
|
await waitForNextAttempt(retryDelayMilliseconds);
|
2024-05-31 20:09:43 +00:00
|
|
|
|
2026-07-10 07:01:41 +00:00
|
|
|
if (isCanceled)
|
2024-05-31 20:09:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await Blazor.reconnect();
|
2026-07-10 07:01:41 +00:00
|
|
|
if (result === false) {
|
2024-05-31 20:09:43 +00:00
|
|
|
// The server was reached, but the connection was rejected; reload the page.
|
|
|
|
|
location.reload();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 07:01:41 +00:00
|
|
|
if (result === true)
|
|
|
|
|
return;
|
2024-05-31 20:09:43 +00:00
|
|
|
} catch {
|
|
|
|
|
// Didn't reach the server; try again.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retried too many times; reload the page.
|
|
|
|
|
location.reload();
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
cancel: () => {
|
|
|
|
|
isCanceled = true;
|
2026-07-10 07:01:41 +00:00
|
|
|
hideReconnectModal();
|
|
|
|
|
},
|
|
|
|
|
triggerImmediateAttempt: () => {
|
|
|
|
|
forceAttempt = true;
|
2024-05-31 20:09:43 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-10 07:01:41 +00:00
|
|
|
const triggerReconnectAfterWake = () => {
|
|
|
|
|
if (isConnectionDown)
|
|
|
|
|
currentReconnectionProcess?.triggerImmediateAttempt();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('visibilitychange', () => {
|
|
|
|
|
if (document.visibilityState === 'visible')
|
|
|
|
|
triggerReconnectAfterWake();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
globalThis.addEventListener('pageshow', triggerReconnectAfterWake);
|
2024-05-31 20:09:43 +00:00
|
|
|
|
|
|
|
|
Blazor.start({
|
|
|
|
|
circuit: {
|
|
|
|
|
reconnectionHandler: {
|
2026-07-10 07:01:41 +00:00
|
|
|
onConnectionDown: () => {
|
|
|
|
|
isConnectionDown = true;
|
|
|
|
|
currentReconnectionProcess ??= startReconnectionProcess();
|
|
|
|
|
},
|
2024-05-31 20:09:43 +00:00
|
|
|
onConnectionUp: () => {
|
2026-07-10 07:01:41 +00:00
|
|
|
isConnectionDown = false;
|
2024-05-31 20:09:43 +00:00
|
|
|
currentReconnectionProcess?.cancel();
|
|
|
|
|
currentReconnectionProcess = null;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
configureSignalR: function (builder) {
|
2026-07-10 07:01:41 +00:00
|
|
|
builder.withServerTimeout(120_000);
|
2024-05-31 20:09:43 +00:00
|
|
|
builder.withKeepAliveInterval(30_000);
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})();
|