show search results for the faster index first

+ refresh after the slower index is downloaded
+ use an existing `f` instead of `isUsw`
This commit is contained in:
tophf 2022-01-20 00:41:03 +03:00
parent 6a07ad0f56
commit 4c696fefbc

View File

@ -41,7 +41,6 @@
* @prop {string} sn - screenshotName * @prop {string} sn - screenshotName
* @prop {boolean} sa - screenshotArchived * @prop {boolean} sa - screenshotArchived
* --------------------- Stylus' internally added extras * --------------------- Stylus' internally added extras
* @prop {boolean} isUsw
* @prop {boolean} installed * @prop {boolean} installed
* @prop {number} installedStyleId * @prop {number} installedStyleId
* @prop {number} pingbackTimer * @prop {number} pingbackTimer
@ -294,21 +293,21 @@
an: author, an: author,
sa: shotArchived, sa: shotArchived,
sn: shot, sn: shot,
isUsw, f: fmt,
} = entry._result = result; } = entry._result = result;
entry.id = RESULT_ID_PREFIX + id; entry.id = RESULT_ID_PREFIX + id;
// title // title
Object.assign($('.search-result-title', entry), { Object.assign($('.search-result-title', entry), {
onclick: Events.openURLandHide, onclick: Events.openURLandHide,
href: `${isUsw ? URLS.usw : URLS.usoArchive}style/${id}`, href: `${fmt ? URLS.usoArchive : URLS.usw}style/${id}`,
}); });
if (isUsw) $('.search-result-title', entry).prepend(USW_ICON.cloneNode(true)); if (!fmt) $('.search-result-title', entry).prepend(USW_ICON.cloneNode(true));
$('.search-result-title span', entry).textContent = $('.search-result-title span', entry).textContent =
t.breakWord(name.length < 300 ? name : name.slice(0, 300) + '...'); t.breakWord(name.length < 300 ? name : name.slice(0, 300) + '...');
// screenshot // screenshot
const elShot = $('.search-result-screenshot', entry); const elShot = $('.search-result-screenshot', entry);
let shotSrc; let shotSrc;
if (isUsw) { if (!fmt) {
shotSrc = /^https?:/i.test(shot) && shot.replace(/\.jpg$/, imgType); shotSrc = /^https?:/i.test(shot) && shot.replace(/\.jpg$/, imgType);
} else { } else {
elShot._src = URLS.uso + `auto_style_screenshots/${id}${USO_AUTO_PIC_SUFFIX}`; elShot._src = URLS.uso + `auto_style_screenshots/${id}${USO_AUTO_PIC_SUFFIX}`;
@ -328,7 +327,7 @@
Object.assign($('[data-type="author"] a', entry), { Object.assign($('[data-type="author"] a', entry), {
textContent: author, textContent: author,
title: author, title: author,
href: isUsw ? `${URLS.usw}user/${encodeURIComponent(author)}` : href: !fmt ? `${URLS.usw}user/${encodeURIComponent(author)}` :
`${URLS.usoArchive}browse/styles?search=%40${authorId}`, `${URLS.usoArchive}browse/styles?search=%40${authorId}`,
onclick: Events.openURLandHide, onclick: Events.openURLandHide,
}); });
@ -433,7 +432,7 @@
async function install() { async function install() {
const {entry, result} = $resultEntry(this); const {entry, result} = $resultEntry(this);
const {i: id, isUsw} = result; const {i: id, f: fmt} = result;
const installButton = $('.search-result-install', entry); const installButton = $('.search-result-install', entry);
showSpinner(entry); showSpinner(entry);
@ -441,13 +440,13 @@
installButton.disabled = true; installButton.disabled = true;
entry.style.setProperty('pointer-events', 'none', 'important'); entry.style.setProperty('pointer-events', 'none', 'important');
delete entry.dataset.error; delete entry.dataset.error;
if (!isUsw) { if (fmt) {
// FIXME: move this to background page and create an API like installUSOStyle // FIXME: move this to background page and create an API like installUSOStyle
result.pingbackTimer = setTimeout(download, PINGBACK_DELAY, result.pingbackTimer = setTimeout(download, PINGBACK_DELAY,
`${URLS.uso}styles/install/${id}?source=stylish-ch`); `${URLS.uso}styles/install/${id}?source=stylish-ch`);
} }
const updateUrl = isUsw ? URLS.makeUswCodeUrl(id) : URLS.makeUsoArchiveCodeUrl(id); const updateUrl = fmt ? URLS.makeUsoArchiveCodeUrl(id) : URLS.makeUswCodeUrl(id);
try { try {
const sourceCode = await download(updateUrl); const sourceCode = await download(updateUrl);
@ -509,18 +508,15 @@
async function fetchIndex() { async function fetchIndex() {
const timer = setTimeout(showSpinner, BUSY_DELAY, dom.list); const timer = setTimeout(showSpinner, BUSY_DELAY, dom.list);
index = []; await Promise.race([
await Promise.all([ [INDEX_URL, json => json.filter(entry => entry.f === 'uso')],
download(INDEX_URL, {responseType: 'json'}).then(res => { [USW_INDEX_URL, json => json.data],
index = index.concat(res.filter(res => res.f === 'uso')); ].map(([url, transform]) =>
}).catch(() => {}), download(url, {responseType: 'json'}).then(res => {
download(USW_INDEX_URL, {responseType: 'json'}).then(res => { res = transform(res);
for (const style of res.data) { index = index ? index.concat(res) : res;
style.isUsw = true; if (index !== res) start();
index.push(style); })));
}
}).catch(() => {}),
]);
clearTimeout(timer); clearTimeout(timer);
$remove(':scope > .lds-spinner', dom.list); $remove(':scope > .lds-spinner', dom.list);
return index; return index;