Fix WebSocket never connecting due to init race condition

Neutralino.init() establishes an internal WebSocket asynchronously.
Calling storage.getData() before the "ready" event fires could stall
silently, meaning init() never reached connect(). Also, "ready" can
fire before DOMContentLoaded, so DOM refs would be null.

Fix: wait for both "ready" AND DOMContentLoaded before calling init(),
guaranteeing native APIs and DOM are both available.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
nonna
2026-03-12 00:26:41 +00:00
parent 6c4731eac0
commit 92d79a7d45

View File

@@ -329,6 +329,17 @@ async function init() {
connect(); connect();
} }
// Boot after Neutralino is ready // Boot: wait for BOTH the DOM and the Neutralino runtime to be ready.
// "ready" guarantees native APIs (storage, tray, window) are live.
// DOMContentLoaded guarantees DOM refs exist.
// We race neither — resolve both before calling init().
let domReady = false;
let neuReady = false;
function maybeInit() {
if (domReady && neuReady) init();
}
Neutralino.init(); Neutralino.init();
document.addEventListener('DOMContentLoaded', init); Neutralino.events.on('ready', () => { neuReady = true; maybeInit(); });
document.addEventListener('DOMContentLoaded', () => { domReady = true; maybeInit(); });