Skip to content

Commit 5ba660d

Browse files
committed
fix
1 parent 6f7d70f commit 5ba660d

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

web_src/js/features/repo-common.js

+29-31
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
import $ from 'jquery';
2-
import {hideElem, showElem} from '../utils/dom.js';
2+
import {hideElem, queryElems, showElem} from '../utils/dom.js';
33
import {POST} from '../modules/fetch.js';
4+
import {showErrorToast} from '../modules/toast.js';
45

5-
async function getArchive($target, url, first) {
6-
const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn');
7-
6+
async function downloadArchive(target, url, retryCount = 0) {
7+
// there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list
8+
const targetLoading = target.closest('.ui.dropdown') ?? target;
9+
let keepLoading = false;
810
try {
9-
dropdownBtn.classList.add('is-loading');
11+
targetLoading.classList.add('is-loading', 'loading-icon-2px');
1012
const response = await POST(url);
11-
if (response.status === 200) {
12-
const data = await response.json();
13-
if (!data) {
14-
// XXX Shouldn't happen?
15-
dropdownBtn.classList.remove('is-loading');
16-
return;
17-
}
18-
19-
if (!data.complete) {
20-
// Wait for only three quarters of a second initially, in case it's
21-
// quickly archived.
22-
setTimeout(() => {
23-
getArchive($target, url, false);
24-
}, first ? 750 : 2000);
25-
} else {
26-
// We don't need to continue checking.
27-
dropdownBtn.classList.remove('is-loading');
28-
window.location.href = url;
29-
}
13+
if (!response.ok) {
14+
throw new Error(`Invalid server response: ${response.status}`);
15+
}
16+
const data = await response.json();
17+
if (!data.complete) {
18+
keepLoading = true; // the archive is not ready yet, keep loading and then retry later
19+
const delay = Math.min((retryCount + 1) * 750, 2000);
20+
setTimeout(() => downloadArchive(target, url, retryCount + 1), delay);
21+
} else {
22+
window.location.href = url; // the archive is ready, start real downloading
3023
}
31-
} catch {
32-
dropdownBtn.classList.remove('is-loading');
24+
} catch (e) {
25+
console.error(e);
26+
showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
27+
} finally {
28+
if (!keepLoading) targetLoading.classList.remove('is-loading', 'loading-icon-2px');
3329
}
3430
}
3531

3632
export function initRepoArchiveLinks() {
37-
$('.archive-link').on('click', function (event) {
38-
event.preventDefault();
39-
const url = this.getAttribute('href');
40-
if (!url) return;
41-
getArchive($(event.target), url, true);
33+
queryElems('.archive-link', (el) => {
34+
el.addEventListener('click', (e) => {
35+
const target = e.target.closest('.archive-link');
36+
if (!target?.href) return;
37+
e.preventDefault();
38+
downloadArchive(target, target.href);
39+
});
4240
});
4341
}
4442

web_src/js/utils/dom.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ export function queryElemSiblings(el, selector = '*', fn) {
6565
}
6666

6767
// it works like jQuery.children: only the direct children are selected
68-
export function queryElemChildren(parent, selector = '*', fn) {
68+
export function queryElemChildren(parent, selector, fn) {
6969
return applyElemsCallback(parent.querySelectorAll(`:scope > ${selector}`), fn);
7070
}
7171

72+
export function queryElems(selector, fn) {
73+
return applyElemsCallback(document.querySelectorAll(selector), fn);
74+
}
75+
7276
export function onDomReady(cb) {
7377
if (document.readyState === 'loading') {
7478
document.addEventListener('DOMContentLoaded', cb);

0 commit comments

Comments
 (0)