Skip to content

Commit

Permalink
fix: avoid race conditions in search loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jillro committed Dec 28, 2024
1 parent 5ef21ba commit 8d3ba8a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/components/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ async function updateSearchResults() {
let batch: Map<TFile, boolean> = 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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 8d3ba8a

Please sign in to comment.