Skip to content

Commit 4a7c6d4

Browse files
committed
fix: Correct event handler ordering for dynamic printer connections
CRITICAL BUG FIX #2: The previous commit (3910927) fixed the handler logic but introduced a new initialization order bug that broke STARTUP connections. PROBLEM: Event handlers were registered AFTER connecting to printers, causing: - connectPrinters() runs → backends emit 'backend-initialized' - NO HANDLER REGISTERED YET → events lost - Handlers registered after connection complete - Result: NO MONITORS for startup connections (--last-used, --all-saved) SOLUTION: Moved event handler registration BEFORE connecting to printers: CORRECT ORDER: 1. Initialize Spoolman tracker coordinator 2. Register event handlers (backend-initialized, connected) ← BEFORE connection 3. Connect to printers (events fire → handlers receive them) 4. Start WebUI This ensures handlers are ready when backend-initialized events fire during BOTH startup connections AND dynamic connections. VERIFIED FLOWS: ✅ Startup: --last-used → handler receives events during connectPrinters() ✅ Startup: --all-saved → handler receives events during connectPrinters() ✅ Dynamic: API /api/printers/connect → handler already registered ✅ Dynamic: API /api/printers/reconnect → handler already registered Step numbering updated to reflect new order (10, 11, 12, 13...).
1 parent 3910927 commit 4a7c6d4

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

src/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -377,39 +377,18 @@ async function main(): Promise<void> {
377377
multiContextSpoolmanTracker.initialize();
378378
console.log('[Init] Spoolman tracker initialized');
379379

380-
// 10. Connect to printers
381-
console.log('[Init] Connecting to printers...');
382-
connectedContexts = await connectPrinters(config);
383-
384-
if (connectedContexts.length === 0 && config.mode !== 'no-printers') {
385-
console.warn('[Warning] No printers connected, but WebUI will still start');
386-
} else if (connectedContexts.length > 0) {
387-
console.log(`[Init] Connected to ${connectedContexts.length} printer(s)`);
388-
389-
// Log connection summary
390-
for (const contextId of connectedContexts) {
391-
const context = contextManager.getContext(contextId);
392-
if (context) {
393-
console.log(` - ${context.printerDetails.Name} (${context.printerDetails.IPAddress})`);
394-
}
395-
}
396-
}
397-
398-
// 11. Start WebUI server
399-
await startWebUI();
400-
401-
// 12. Setup event forwarding BEFORE starting polling
402-
// This ensures listeners are ready when polling data starts flowing
380+
// 10. Setup event handlers BEFORE connecting to printers
381+
// This ensures handlers are ready when backend-initialized events fire during connection
403382
setupEventForwarding();
404383

405-
// 12b. Setup post-connection hook for logging
384+
// 10b. Setup post-connection hook for logging
406385
connectionManager.on('connected', (printerDetails) => {
407386
console.log(`[Events] Printer connected: ${printerDetails.Name}`);
408387
// Polling and monitors are initialized by backend-initialized handler
409388
});
410389
console.log('[Events] Post-connection hook configured');
411390

412-
// 12c. Setup backend-initialized hook to start polling and create monitors
391+
// 10c. Setup backend-initialized hook to start polling and create monitors
413392
// This is critical for Spoolman deduction and print state monitoring
414393
// IMPORTANT: This handles both startup connections AND dynamic connections (API reconnect/discovery)
415394
connectionManager.on('backend-initialized', (event: unknown) => {
@@ -456,6 +435,27 @@ async function main(): Promise<void> {
456435
});
457436
console.log('[Events] Backend-initialized hook configured');
458437

438+
// 11. Connect to printers (handlers are now ready to receive backend-initialized events)
439+
console.log('[Init] Connecting to printers...');
440+
connectedContexts = await connectPrinters(config);
441+
442+
if (connectedContexts.length === 0 && config.mode !== 'no-printers') {
443+
console.warn('[Warning] No printers connected, but WebUI will still start');
444+
} else if (connectedContexts.length > 0) {
445+
console.log(`[Init] Connected to ${connectedContexts.length} printer(s)`);
446+
447+
// Log connection summary
448+
for (const contextId of connectedContexts) {
449+
const context = contextManager.getContext(contextId);
450+
if (context) {
451+
console.log(` - ${context.printerDetails.Name} (${context.printerDetails.IPAddress})`);
452+
}
453+
}
454+
}
455+
456+
// 12. Start WebUI server
457+
await startWebUI();
458+
459459
// 13. Note: Polling and monitors are initialized by backend-initialized handler
460460
// This handler fires for both startup connections AND dynamic connections
461461
if (connectedContexts.length > 0) {

0 commit comments

Comments
 (0)