@@ -4,8 +4,9 @@ import { url, folder_data } from '../constants/Data';
4
4
import { loadCurrentWikiInfo , loadWiki , } from './loaders' ;
5
5
import { model_wiki_info } from '../constants/Models' ;
6
6
import { CacheManager } from 'react-native-expo-image-cache' ;
7
+ import PromisePool from 'es6-promise-pool' ;
8
+
7
9
8
- // to-do cleanup image downloading
9
10
export const downloadImages = async ( wiki , progress_callback , cancel ) => {
10
11
// Logger.debug('downloading images');
11
12
@@ -22,27 +23,29 @@ export const downloadImages = async (wiki, progress_callback, cancel) => {
22
23
} ) ;
23
24
images . items . push ( url . images . items ( 'recipe' ) ) ;
24
25
25
-
26
- let cProgress = 0 ;
27
26
const keys = Object . keys ( images ) ;
28
- const imagesTotal = keys . reduce ( ( res , arr ) => res += images [ arr ] . length , 0 ) ;
29
- const step = 1 / imagesTotal ;
30
-
31
- let _images = [ ] ;
27
+
28
+ let flatImages = [ ] ;
32
29
keys . forEach ( key => {
33
- _images = _images . concat ( images [ key ] )
30
+ flatImages = flatImages . concat ( images [ key ] )
34
31
} ) ;
35
- // console.log(_images)
36
32
37
- return Promise . all (
38
- _images . map ( async image => {
39
- // if(cancel()) return;
33
+ let _processedImages = 0 ;
34
+ const imagesTotal = flatImages . length ;
35
+ let cProgress = 0 ;
36
+ const step = 1 / imagesTotal ;
40
37
41
- await CacheManager . get ( image ) . getPath ( ) ;
42
- progress_callback ( cProgress += step ) ;
43
- // Logger.silly(`downloaded -${image}-`)
38
+ const promiseProducer = ( ) => {
39
+ if ( _processedImages < imagesTotal ) {
40
+ _processedImages ++ ;
41
+ return CacheManager . get ( flatImages [ _processedImages - 1 ] ) . getPath ( )
42
+ . then ( progress_callback ( cProgress += step ) ) ;
43
+ } else {
44
+ return null ;
44
45
}
45
- ) ) ;
46
+ }
47
+
48
+ return new PromisePool ( promiseProducer , 5 ) . start ( ) ;
46
49
}
47
50
48
51
0 commit comments