Refactor waitForWasm to use event listener instead of polling

Replaces the busy-wait polling loop in `waitForWasm` with a `borgstmf:ready` event listener.
This eliminates unnecessary CPU usage during initialization and potentially improves latency.

Also updates the `stmf.wasm` binary to ensure it contains the event dispatch logic present in `pkg/wasm/stmf/main.go`.
This commit is contained in:
Snider 2026-02-02 02:47:19 +00:00
parent cf2af53ed3
commit 269b3d6ac0
3 changed files with 19 additions and 6 deletions

Binary file not shown.

View file

@ -306,13 +306,26 @@ export class BorgSTMF {
}
private async waitForWasm(timeout = 5000): Promise<void> {
const start = Date.now();
while (!window.BorgSTMF?.ready) {
if (Date.now() - start > timeout) {
throw new Error('Timeout waiting for WASM module to initialize');
}
await new Promise((resolve) => setTimeout(resolve, 50));
if (window.BorgSTMF?.ready) {
return;
}
return new Promise((resolve, reject) => {
let timeoutId: number;
const onReady = () => {
window.clearTimeout(timeoutId);
document.removeEventListener('borgstmf:ready', onReady);
resolve();
};
timeoutId = window.setTimeout(() => {
document.removeEventListener('borgstmf:ready', onReady);
reject(new Error('Timeout waiting for WASM module to initialize'));
}, timeout);
document.addEventListener('borgstmf:ready', onReady, { once: true });
});
}
private async loadScript(src: string): Promise<void> {

Binary file not shown.