fix: brain polling — update individual items instead of full reload

Polls only pending/processing items by ID every 4s instead of
reloading the entire list every 3s. Prevents screen flashing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yusuf Suleman
2026-04-01 19:06:52 -05:00
parent 7a5c3382d3
commit b58313ec8e
2 changed files with 23 additions and 11 deletions

View File

@@ -217,25 +217,37 @@
function startPolling() {
stopPolling();
pollTimer = setInterval(async () => {
const hasPending = items.some(i => i.processing_status === 'pending' || i.processing_status === 'processing');
if (hasPending) {
await loadItems();
// Update selected item if it was pending
if (selectedItem) {
const updated = items.find(i => i.id === selectedItem!.id);
if (updated) selectedItem = updated;
}
} else {
const pendingIds = items
.filter(i => i.processing_status === 'pending' || i.processing_status === 'processing')
.map(i => i.id);
if (pendingIds.length === 0) {
stopPolling();
return;
}
}, 3000);
// Poll each pending item individually instead of reloading everything
for (const id of pendingIds) {
try {
const updated = await api(`/items/${id}`);
const idx = items.findIndex(i => i.id === id);
if (idx !== -1) {
items[idx] = updated;
items = items; // trigger reactivity
}
if (selectedItem?.id === id) {
selectedItem = updated;
}
} catch { /* item might be gone */ }
}
}, 4000);
}
function stopPolling() {
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
}
// Start polling after capture
// Start polling when pending items exist
$effect(() => {
const hasPending = items.some(i => i.processing_status === 'pending' || i.processing_status === 'processing');
if (hasPending && !pollTimer) startPolling();