fix: prevent IPC listener leak in preload, fix hud-show race on cold start

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
boksbc 2026-05-11 22:01:28 +02:00
parent 03e4fd68d8
commit 71f7341974
2 changed files with 42 additions and 7 deletions

View File

@ -3,6 +3,8 @@ const path = require('path');
const HUD_URL = process.env.FOX_HUD_URL || 'https://fox.pxo.at/hud';
let hudWindow = null;
let hudLoaded = false;
let pendingShow = false;
function createHud() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
@ -29,8 +31,22 @@ function createHud() {
hudWindow.setAlwaysOnTop(true, 'screen-saver');
hudWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
hudWindow.webContents.once('did-finish-load', () => {
hudLoaded = true;
if (pendingShow) {
pendingShow = false;
hudWindow.webContents.send('hud-show');
}
});
hudWindow.loadURL(HUD_URL);
hudWindow.on('closed', () => { hudWindow = null; });
hudWindow.on('closed', () => {
hudWindow = null;
hudLoaded = false;
pendingShow = false;
});
return hudWindow;
}
@ -39,7 +55,11 @@ function showHud() {
if (!hudWindow) createHud();
hudWindow.show();
hudWindow.focus();
hudWindow.webContents.send('hud-show');
if (hudLoaded) {
hudWindow.webContents.send('hud-show');
} else {
pendingShow = true;
}
}
function hideHud() {

View File

@ -4,9 +4,24 @@ contextBridge.exposeInMainWorld('foxBridge', {
sendMessage: (text) => ipcRenderer.send('send-message', text),
hideHud: () => ipcRenderer.send('hide-hud'),
macCommand: (cmd) => ipcRenderer.invoke('mac-command', cmd),
onFoxAction: (cb) => ipcRenderer.on('fox-action', (_, data) => cb(data)),
onHudShow: (cb) => ipcRenderer.on('hud-show', () => cb()),
onHudHide: (cb) => ipcRenderer.on('hud-hide', () => cb()),
onStartListening: (cb) => ipcRenderer.on('start-listening', () => cb()),
onPlayAudio: (cb) => ipcRenderer.on('play-audio', (_, url) => cb(url)),
onFoxAction: (cb) => {
ipcRenderer.removeAllListeners('fox-action');
ipcRenderer.on('fox-action', (_, data) => cb(data));
},
onHudShow: (cb) => {
ipcRenderer.removeAllListeners('hud-show');
ipcRenderer.on('hud-show', () => cb());
},
onHudHide: (cb) => {
ipcRenderer.removeAllListeners('hud-hide');
ipcRenderer.on('hud-hide', () => cb());
},
onStartListening: (cb) => {
ipcRenderer.removeAllListeners('start-listening');
ipcRenderer.on('start-listening', () => cb());
},
onPlayAudio: (cb) => {
ipcRenderer.removeAllListeners('play-audio');
ipcRenderer.on('play-audio', (_, url) => cb(url));
},
});