Add event loop yields so SSE events flush during search

Without yields, the async operations block the event loop and
SSE events pile up unsent until the entire search completes.
Adding setTimeout(0) yields after start and store_complete events
lets Node.js flush the write buffer to the client.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
mariosemes
2026-03-26 22:28:35 +01:00
parent 75b8759805
commit 0e6ec21e81

View File

@@ -161,6 +161,9 @@ export async function searchStreaming(
stores: stores.map((s) => ({ id: s.id, name: s.name, renderJs: !!s.render_js })), stores: stores.map((s) => ({ id: s.id, name: s.name, renderJs: !!s.render_js })),
}); });
// Yield to event loop so the SSE start event flushes to client
await new Promise((r) => setTimeout(r, 0));
const limit = pLimit(MAX_CONCURRENCY); const limit = pLimit(MAX_CONCURRENCY);
const errors: SearchResult['meta']['errors'] = []; const errors: SearchResult['meta']['errors'] = [];
let totalResults = 0; let totalResults = 0;
@@ -193,6 +196,9 @@ export async function searchStreaming(
resultCount: products.length, resultCount: products.length,
duration, duration,
}); });
// Yield so SSE event flushes to client
await new Promise((r) => setTimeout(r, 0));
} catch (err) { } catch (err) {
const duration = Date.now() - storeStart; const duration = Date.now() - storeStart;
const errorMessage = err instanceof Error ? err.message : String(err); const errorMessage = err instanceof Error ? err.message : String(err);