@@ -2,42 +2,34 @@ import $ from 'jquery';
2
2
import { hideElem , queryElems , showElem } from '../utils/dom.js' ;
3
3
import { POST } from '../modules/fetch.js' ;
4
4
import { showErrorToast } from '../modules/toast.js' ;
5
+ import { sleep } from '../utils.js' ;
5
6
6
- async function downloadArchive ( target , url , retryCount = 0 ) {
7
+ async function onDownloadArchive ( e ) {
8
+ e . preventDefault ( ) ;
7
9
// 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' ) ;
10
13
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 ) ) ;
23
21
}
22
+ window . location . href = el . href ; // the archive is ready, start real downloading
24
23
} catch ( e ) {
25
24
console . error ( e ) ;
26
25
showErrorToast ( `Failed to download the archive: ${ e } ` , { duration : 2500 } ) ;
27
26
} finally {
28
- if ( ! keepLoading ) targetLoading . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
27
+ targetLoading . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
29
28
}
30
29
}
31
30
32
31
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 ) ) ;
41
33
}
42
34
43
35
export function initRepoCloneLink ( ) {
0 commit comments