From e406e2b5dcd38f148fe9c79067517b5d1e0f22d3 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 7 Jul 2022 16:49:43 +0300 Subject: [PATCH] fetch all indexes before showing "not found" --- popup/search.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/popup/search.js b/popup/search.js index 5e1ce380..48e897c9 100644 --- a/popup/search.js +++ b/popup/search.js @@ -198,7 +198,7 @@ render(); dom.list.hidden = !results.length; if (!results.length && !$('#search-query').value) { - error(t('searchResultNoneFound')); + if (index._ready) error(t('searchResultNoneFound')); } else { resetUI(); } @@ -505,15 +505,19 @@ async function fetchIndex() { const timer = setTimeout(showSpinner, BUSY_DELAY, dom.list); - await Promise.race([ + const jobs = [ [INDEX_URL, json => json.filter(entry => entry.f === 'uso')], [USW_INDEX_URL, json => json.data], - ].map(([url, transform]) => - download(url, {responseType: 'json'}).then(res => { - res = transform(res); - index = index ? index.concat(res) : res; - if (index !== res) start(); - }))); + ].map(async ([url, transform]) => { + const res = transform(await download(url, {responseType: 'json'})); + index = index ? index.concat(res) : res; + if (index !== res) ready = ready.then(start); + }); + // TODO: use Promise.allSettled when "minimum_chrome_version" >= 76 and "strict_min_version" >= 71 + Promise.all(jobs.map(j => j.catch(e => e))).then(() => { + index._ready = true; + }); + await Promise.race(jobs); clearTimeout(timer); $remove(':scope > .lds-spinner', dom.list); return index;