All files / src/utils imageLoader.js

80.95% Statements 17/21
66.67% Branches 10/15
100% Functions 3/3
80.95% Lines 17/21
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                  4x 4x   4x 4x 1x 1x   3x   2x 2x       3x   3x         3x       2x   1x 1x         1x     2x      
// @flow
import filterImgSrc from '../logic/filterSrcset';
import fetchImage from './fetchImage';
import applyImage from './applyImage';
import logError from './logError';
import setImageHeight from '../logic/setImageHeight';
import updateSessionStorage from '../logic/updateSessionStorage';
 
export default function imageLoader(target: any, withObserver: boolean = true) {
  try {
    const image = new Image(); // eslint-disable-line no-undef
 
    Eif (withObserver) {
      if (this) {
        this.observer.unobserve(target);
        this.appendImgLoadingRef(image);
      } else {
        const { observer, imgLoadingRefs } = window.__REACT_SIMPLE_IMG__;
 
        observer.unobserve(target);
        imgLoadingRefs.set(target, image);
      }
    }
 
    const src = filterImgSrc(target);
 
    Iif (!src) {
      logError('Filter Image source returned empty image source', target);
      return;
    }
 
    Iif (target.parentNode && target.parentNode.style.height === '1px') {
      setImageHeight(image, target);
    }
 
    fetchImage(image, src)
      .then(() => {
        applyImage.apply(this, [target, image, src]);
        Iif (window.__REACT_SIMPLE_IMG__ && window.__REACT_SIMPLE_IMG__.disableAnimateCachedImg) {
          updateSessionStorage(src);
        }
      })
      .catch(e => {
        logError('Fetch image failed with target', target, e);
      });
  } catch (e) {
    logError('Image loader failed with target', target, e);
  }
}