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() { function startPolling() {
stopPolling(); stopPolling();
pollTimer = setInterval(async () => { pollTimer = setInterval(async () => {
const hasPending = items.some(i => i.processing_status === 'pending' || i.processing_status === 'processing'); const pendingIds = items
if (hasPending) { .filter(i => i.processing_status === 'pending' || i.processing_status === 'processing')
await loadItems(); .map(i => i.id);
// Update selected item if it was pending
if (selectedItem) { if (pendingIds.length === 0) {
const updated = items.find(i => i.id === selectedItem!.id);
if (updated) selectedItem = updated;
}
} else {
stopPolling(); 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() { function stopPolling() {
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; } if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
} }
// Start polling after capture // Start polling when pending items exist
$effect(() => { $effect(() => {
const hasPending = items.some(i => i.processing_status === 'pending' || i.processing_status === 'processing'); const hasPending = items.some(i => i.processing_status === 'pending' || i.processing_status === 'processing');
if (hasPending && !pollTimer) startPolling(); if (hasPending && !pollTimer) startPolling();