Skip to content

Commit 2071f28

Browse files
committed
rewrite
1 parent 5cfd387 commit 2071f28

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

web_src/js/features/repo-common.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,34 @@ import $ from 'jquery';
22
import {hideElem, queryElems, showElem} from '../utils/dom.js';
33
import {POST} from '../modules/fetch.js';
44
import {showErrorToast} from '../modules/toast.js';
5+
import {sleep} from '../utils.js';
56

6-
async function downloadArchive(target, url, retryCount = 0) {
7+
async function onDownloadArchive(e) {
8+
e.preventDefault();
79
// 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;
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');
1013
try {
11-
targetLoading.classList.add('is-loading', 'loading-icon-2px');
12-
const response = await POST(url);
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
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}`);
17+
18+
const data = await response.json();
19+
if (data.complete) break;
20+
await sleep(Math.min((tryCount + 1) * 750, 2000));
2321
}
22+
window.location.href = el.href; // the archive is ready, start real downloading
2423
} catch (e) {
2524
console.error(e);
2625
showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
2726
} finally {
28-
if (!keepLoading) targetLoading.classList.remove('is-loading', 'loading-icon-2px');
27+
targetLoading.classList.remove('is-loading', 'loading-icon-2px');
2928
}
3029
}
3130

3231
export function initRepoArchiveLinks() {
33-
queryElems('.archive-link', (el) => {
34-
el.addEventListener('click', async (e) => {
35-
const target = e.target.closest('.archive-link');
36-
if (!target?.href) return;
37-
e.preventDefault();
38-
await downloadArchive(target, target.href);
39-
});
40-
});
32+
queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive));
4133
}
4234

4335
export function initRepoCloneLink() {

0 commit comments

Comments
 (0)