Skip to content

Commit 516a280

Browse files
committed
fix
1 parent ffc9879 commit 516a280

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

web_src/js/features/repo-common.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
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';
5+
import {sleep} from '../utils.js';
46

5-
async function getArchive($target, url, first) {
6-
const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn');
7-
7+
async function onDownloadArchive(e) {
8+
e.preventDefault();
9+
// there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list
10+
const el = e.target.closest('a.archive-link[href]');
11+
const targetLoading = el.closest('.ui.dropdown') ?? el;
12+
targetLoading.classList.add('is-loading', 'loading-icon-2px');
813
try {
9-
dropdownBtn.classList.add('is-loading');
10-
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-
}
14+
for (let tryCount = 0; ;tryCount++) {
15+
const response = await POST(el.href);
16+
if (!response.ok) throw new Error(`Invalid server response: ${response.status}`);
1817

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-
}
18+
const data = await response.json();
19+
if (data.complete) break;
20+
await sleep(Math.min((tryCount + 1) * 750, 2000));
3021
}
31-
} catch {
32-
dropdownBtn.classList.remove('is-loading');
22+
window.location.href = el.href; // the archive is ready, start real downloading
23+
} catch (e) {
24+
console.error(e);
25+
showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
26+
} finally {
27+
targetLoading.classList.remove('is-loading', 'loading-icon-2px');
3328
}
3429
}
3530

3631
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);
42-
});
32+
queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive));
4333
}
4434

4535
export function initRepoCloneLink() {

web_src/js/utils/dom.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ 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)