Skip to content

Commit ad0cb23

Browse files
committed
downloaders — added es-6-promise-pool as a wourkaround React Native concurrent fetches bug (react-native-netinfo/react-native-netinfo#10)
1 parent cb5de80 commit ad0cb23

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
]
1919
},
2020
"dependencies": {
21+
"es6-promise-pool": "^2.5.0",
2122
"expo": "^32.0.0",
2223
"react": "16.5.0",
2324
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",

src/utils/downloaders.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { url, folder_data } from '../constants/Data';
44
import { loadCurrentWikiInfo, loadWiki, } from './loaders';
55
import { model_wiki_info } from '../constants/Models';
66
import { CacheManager } from 'react-native-expo-image-cache';
7+
import PromisePool from 'es6-promise-pool';
8+
79

8-
// to-do cleanup image downloading
910
export const downloadImages = async (wiki, progress_callback, cancel) => {
1011
// Logger.debug('downloading images');
1112

@@ -22,27 +23,29 @@ export const downloadImages = async (wiki, progress_callback, cancel) => {
2223
});
2324
images.items.push(url.images.items('recipe'));
2425

25-
26-
let cProgress = 0;
2726
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 = [];
3229
keys.forEach(key => {
33-
_images = _images.concat(images[key])
30+
flatImages = flatImages.concat(images[key])
3431
});
35-
// console.log(_images)
3632

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;
4037

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;
4445
}
45-
));
46+
}
47+
48+
return new PromisePool(promiseProducer, 5).start();
4649
}
4750

4851

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,11 @@ es-to-primitive@^1.1.1:
26242624
is-date-object "^1.0.1"
26252625
is-symbol "^1.0.1"
26262626

2627+
es6-promise-pool@^2.5.0:
2628+
version "2.5.0"
2629+
resolved "https://registry.yarnpkg.com/es6-promise-pool/-/es6-promise-pool-2.5.0.tgz#147c612b36b47f105027f9d2bf54a598a99d9ccb"
2630+
integrity sha1-FHxhKza0fxBQJ/nSv1SlmKmdnMs=
2631+
26272632
escape-html@~1.0.3:
26282633
version "1.0.3"
26292634
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"

0 commit comments

Comments
 (0)