All files / src/logic imageLoader.js

92.31% Statements 24/26
78.57% Branches 11/14
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                  8x 8x   8x 6x   6x 6x   6x 1x 1x     5x 1x     5x   3x 3x   3x 3x   3x 2x     3x 1x   1x 1x       3x     3x      
// @flow
import filterImgSrc from '../utils/filterSrcset';
import fetchImage from './fetchImage';
import applyImage from './applyImage';
import logError from '../utils/logError';
import setImageHeight from '../utils/setImageHeight';
import updateSessionStorage from './updateSessionStorage';
 
export default function imageLoader(target: any) {
  try {
    const image = new Image(); // eslint-disable-line no-undef
 
    const { observer, imgLoadingRefs } = window.__REACT_SIMPLE_IMG__;
    const src = filterImgSrc(target);
 
    observer.unobserve(target);
    imgLoadingRefs.set(target, image);
 
    if (!src) {
      logError('Filter Image source returned empty image source', target);
      return;
    }
 
    if (target.parentNode && target.parentNode.style.height === '1px') {
      setImageHeight(image, target);
    }
 
    fetchImage(image, src)
      .then(() => {
        Eif (target) {
          applyImage(target, image, src);
 
          Iif (!window.__REACT_SIMPLE_IMG__) return;
          const { disableAnimateCachedImg, callBackRefs } = window.__REACT_SIMPLE_IMG__;
 
          if (disableAnimateCachedImg) {
            updateSessionStorage(src);
          }
 
          const callback = callBackRefs.get(target);
          Iif (!callback) return;
 
          callback();
          callBackRefs.delete(target);
        }
      })
      .catch(e => {
        logError('Fetch image failed with target', target, e);
      });
  } catch (e) {
    logError('Image loader failed with target', target, e);
  }
}