Fix live updates: debounce DB writes, broadcast before saving, include smeltable/craftable in updates

This commit is contained in:
MayaTheShy
2026-03-21 18:15:50 -04:00
parent aed7d1f735
commit 80338d1973
3 changed files with 104 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ import { createServer } from 'http';
import {
loadFullState, saveFullState, recordItemHistory,
saveItems, saveFurnaces, saveAlerts, saveState,
getHistory, closeDb,
getHistory, closeDb, flushPendingSave,
} from './db.js';
const app = express();
@@ -378,27 +378,7 @@ function updateStateFromBridge(data) {
lastUpdate = Date.now();
// Persist to SQLite
try {
saveFullState({
inventoryState,
activityState,
alertsState,
smeltingPaused,
disabledRecipes,
smeltableRecipes,
craftableRecipes,
craftTurtleOk,
});
// Record history snapshot (throttled to every 5 min internally)
if (inventoryState.itemList?.length) {
recordItemHistory(inventoryState.itemList);
}
} catch (err) {
console.error('❌ DB save error:', err.message);
}
// Broadcast to all web clients
// Broadcast to all web clients FIRST (non-blocking, instant)
broadcastToClients({
type: 'state_update',
inventory: inventoryState,
@@ -406,9 +386,28 @@ function updateStateFromBridge(data) {
alerts: alertsState,
smeltingPaused,
disabledRecipes,
smeltable: smeltableRecipes,
craftable: craftableRecipes,
craftTurtleOk,
lastUpdate,
});
// Persist to SQLite (debounced — won't block the broadcast)
saveFullState({
inventoryState,
activityState,
alertsState,
smeltingPaused,
disabledRecipes,
smeltableRecipes,
craftableRecipes,
craftTurtleOk,
});
// Record history snapshot (throttled to every 5 min internally)
if (inventoryState.itemList?.length) {
recordItemHistory(inventoryState.itemList);
}
}
// ========== WebSocket Server ==========