Splitting reordering into separate loop

We now have on loop that checks which results we want to re-order, and then a second loop that actually does the re-ordering.
pull/662/head
Kevin Payravi 2024-05-01 18:59:28 -05:00
parent a8c740480c
commit cf838788b2
1 changed files with 15 additions and 2 deletions

View File

@ -548,6 +548,7 @@ async function reorderSearchResults(searchResults, searchEngine, storage) {
if (!resultsFirstChild) return;
let crossLanguageSetting = storage.crossLanguage || 'off';
let resultsToSort = [];
for (const searchResult of searchResults) {
try {
@ -569,8 +570,7 @@ async function reorderSearchResults(searchResults, searchEngine, storage) {
console.debug('Indie Wiki Buddy is not re-ordering results, as an indie wiki is already the first result.');
break;
} else {
await reorderDestinationSearchResult(resultsFirstChild, searchResult);
reorderedHrefs.push(searchResultLink);
resultsToSort.push(searchResult);
}
}
}
@ -578,6 +578,19 @@ async function reorderSearchResults(searchResults, searchEngine, storage) {
console.log('Indie Wiki Buddy failed to properly re-order search results with error: ' + e);
}
}
// Reverse order of resultsToSort,
// to restore top-down order.
resultsToSort = resultsToSort.reverse();
for (const searchResult of resultsToSort) {
try {
await reorderDestinationSearchResult(resultsFirstChild, searchResult);
reorderedHrefs.push(searchResultLink);
} catch (e) {
console.log('Indie Wiki Buddy failed to properly re-order search results with error: ' + e);
}
}
}
return reorderedHrefs;