forked from sambernard/react-preload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageHelper.js
37 lines (31 loc) · 1.01 KB
/
ImageHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import ImageCache from './ImageCache';
const ImageHelper = {
loadImage(url) {
const image = ImageCache.get(url);
return new Promise((resolve, reject) => {
const handleSuccess = () => {
resolve(image);
};
const handleError = () => {
console.error('IMAGE FAIL', image);
reject(image);
};
if (image.naturalWidth && image.naturalHeight) {
// image is loaded, go ahead and change the state
handleSuccess();
} else {
image.addEventListener('load', handleSuccess, false);
image.addEventListener('error', handleError, false);
}
});
},
loadImages(urls) {
const promises = urls.map(this.loadImage.bind(this));
return Promise.all(promises);
},
// preload without caring about the result
stuffImages(urls) {
ImageCache.stuff(urls);
},
};
export default ImageHelper;