From 8d3ba8ab2cd9f69b709207be4364435674867743 Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Sat, 28 Dec 2024 12:58:06 +0100 Subject: [PATCH] fix: avoid race conditions in search loop --- src/components/store.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/store.ts b/src/components/store.ts index 322078c..d5a4dc2 100644 --- a/src/components/store.ts +++ b/src/components/store.ts @@ -88,8 +88,6 @@ async function updateSearchResults() { let batch: Map = new Map(); let lastBatch = { date: new Date(), index: 0 }; for (let i = 0; i < $sortedFiles.length; i++) { - if ($searchQuery !== get(searchQuery)) return; - const file = $sortedFiles[i]; const cachedResult = get(searchResultCache).get(cacheKey($sortedFiles[i])); if (cachedResult !== undefined) { @@ -111,10 +109,14 @@ async function updateSearchResults() { frontmatter, caseSensitive: $searchCaseSensitive, }); + + if ($searchQuery !== get(searchQuery)) return; batch.set(file, match); searchResultCache.update((cache) => cache.set(cacheKey(file), match)); } + if ($searchQuery !== get(searchQuery)) return; + if (i % 10 === 0) { searchResultLoadingState.set(i / $sortedFiles.length); }