fix: huddles not launching reliably

Closes #160
Race condition preventing proper initialization.
This commit is contained in:
talksik
2026-04-14 07:58:45 -07:00
parent 91a973b7fb
commit fe2a003e94
4 changed files with 29 additions and 34 deletions
-1
View File
@@ -19,7 +19,6 @@ declare global {
closeHuddle: () => void;
};
electronHuddle: {
onConnect: (callback: (data: { token: string; serverUrl: string }) => void) => () => void;
getScreenSources: () => Promise<ScreenSource[]>;
};
electronAutoplay: {
+9 -8
View File
@@ -24,14 +24,15 @@ import { RoomEvent, Track } from 'livekit-client';
import { useState, useEffect, useRef, useCallback } from 'react';
import { ScreenSourcePicker } from '@/components/screen-source-picker';
export function HuddleApp() {
const [connection, setConnection] = useState<{ token: string; serverUrl: string } | null>(null);
function readConnectionFromHash(): { token: string; serverUrl: string } | null {
const params = new URLSearchParams(window.location.hash.slice(1));
const token = params.get('token');
const serverUrl = params.get('serverUrl');
return token && serverUrl ? { token, serverUrl } : null;
}
useEffect(() => {
return window.electronHuddle.onConnect((data) => {
setConnection(data);
});
}, []);
export function HuddleApp() {
const [connection] = useState(readConnectionFromHash);
const handleDisconnected = () => {
window.electronWindow.closeHuddle();
@@ -40,7 +41,7 @@ export function HuddleApp() {
if (!connection) {
return (
<div className="flex h-screen items-center justify-center bg-background text-foreground">
<p className="text-muted-foreground text-sm">Connecting to huddle...</p>
<p className="text-muted-foreground text-sm">Missing huddle connection data.</p>
</div>
);
}
+20 -20
View File
@@ -162,10 +162,7 @@ const createAutoplayWindow = () => {
};
const createHuddleWindow = () => {
if (huddleWindow) {
huddleWindow.focus();
return;
}
if (huddleWindow) return;
huddleWindow = new BrowserWindow({
width: 1024,
@@ -180,19 +177,28 @@ const createHuddleWindow = () => {
});
hardenWindow(huddleWindow);
if (HUDDLE_WINDOW_VITE_DEV_SERVER_URL) {
huddleWindow.loadURL(HUDDLE_WINDOW_VITE_DEV_SERVER_URL);
} else {
huddleWindow.loadFile(
path.join(__dirname, `../renderer/${HUDDLE_WINDOW_VITE_NAME}/index.html`),
);
}
huddleWindow.on('closed', () => {
huddleWindow = null;
});
};
// Connection data is passed via URL hash so it's available synchronously on
// renderer mount — avoids the IPC race where `huddle:connect` could be sent
// before React attached its listener.
const loadHuddleWindow = (data: { token: string; serverUrl: string }) => {
if (!huddleWindow) return;
const params = new URLSearchParams({ token: data.token, serverUrl: data.serverUrl });
const hash = params.toString();
if (HUDDLE_WINDOW_VITE_DEV_SERVER_URL) {
huddleWindow.loadURL(`${HUDDLE_WINDOW_VITE_DEV_SERVER_URL}#${hash}`);
} else {
huddleWindow.loadFile(
path.join(__dirname, `../renderer/${HUDDLE_WINDOW_VITE_NAME}/index.html`),
{ hash },
);
}
};
function positionAutoplayWindow() {
if (!autoplayWindow) return;
const { width } = screen.getPrimaryDisplay().workAreaSize;
@@ -261,14 +267,8 @@ ipcMain.on('window:fullscreen', (event) => {
// Secondary window IPC handlers
ipcMain.on('window:open-huddle', (_event, data: { token: string; serverUrl: string }) => {
createHuddleWindow();
// Send connection data once the huddle window is ready
huddleWindow?.webContents.once('did-finish-load', () => {
huddleWindow?.webContents.send('huddle:connect', data);
});
// If already loaded, send immediately
if (!huddleWindow?.webContents.isLoading()) {
huddleWindow?.webContents.send('huddle:connect', data);
}
loadHuddleWindow(data);
huddleWindow?.focus();
});
ipcMain.on('window:close-huddle', () => {
huddleWindow?.close();
-5
View File
@@ -12,11 +12,6 @@ contextBridge.exposeInMainWorld('electronWindow', {
});
contextBridge.exposeInMainWorld('electronHuddle', {
onConnect: (callback: (data: { token: string; serverUrl: string }) => void) => {
const handler = (_event: Electron.IpcRendererEvent, data: { token: string; serverUrl: string }) => callback(data);
ipcRenderer.on('huddle:connect', handler);
return () => { ipcRenderer.removeListener('huddle:connect', handler); };
},
getScreenSources: () => ipcRenderer.invoke('screen:get-sources'),
});