🐛 Fix Initializing the connection to the local database hang (#4375)

* fix initializing to the local db

* release notes
This commit is contained in:
Michael Clark
2025-02-13 22:43:33 +00:00
committed by GitHub
parent 454f019f7a
commit 7e1c0a8682
3 changed files with 30 additions and 2 deletions

View File

@@ -96,11 +96,15 @@ function connectWorker(worker, onOpen, onError) {
// ready to handle messages.
if (msg.type === 'connect') {
// Send any messages that were queued while closed
if (messageQueue.length > 0) {
if (messageQueue?.length > 0) {
messageQueue.forEach(msg => worker.postMessage(msg));
messageQueue = null;
}
// signal to the backend that we're connected to it
globalWorker.postMessage({
name: 'client-connected-to-backend',
});
onOpen();
} else if (msg.type === 'app-init-failure') {
onError(msg);

View File

@@ -44,6 +44,12 @@ export const init: T.Init = function (serverChn, handlers) {
return;
}
if (msg.name === 'client-connected-to-backend') {
// the client is indicating that it is connected to this backend. Stop attempting to connect
clearInterval(reconnectToClientInterval);
return;
}
const { id, name, args, undoTag, catchErrors } = msg;
if (handlers[name]) {
@@ -98,7 +104,19 @@ export const init: T.Init = function (serverChn, handlers) {
false,
);
serverChannel.postMessage({ type: 'connect' });
const RECONNECT_INTERVAL_MS = 200;
const MAX_RECONNECT_ATTEMPTS = 500;
let reconnectAttempts = 0;
const reconnectToClientInterval = setInterval(() => {
serverChannel.postMessage({ type: 'connect' });
reconnectAttempts++;
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
// Failed to connect to client - signal server error
send('server-error');
clearInterval(reconnectToClientInterval);
}
}, RECONNECT_INTERVAL_MS);
};
export const send: T.Send = function (name, args) {

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [MikesGlitch]
---
Prevent the app getting stuck on the "Initializing the connection to the local database..." screen