All files / src/logic observerStart.js

84.62% Statements 11/13
66.67% Branches 4/6
66.67% Functions 2/3
100% Lines 9/9

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      3x           2x 2x 2x 1x                           1x   1x   1x               1x    
// @flow
import imageLoader from './imageLoader';
 
export const defaultConfig = {
  rootMargin: '0px 0px',
  threshold: [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
};
 
export function onIntersection(entries: Array<{ intersectionRatio: number, target: any }>) {
  for (let i = 0, len = entries.length; i < len; i++) {
    const { intersectionRatio, target } = entries[i];
    if (intersectionRatio > 0) {
      imageLoader(target);
    }
  }
}
 
export default function observerStart(
  config: {
    root?: HTMLElement,
    rootMargin?: string,
    threshold?: number | Array<number>,
  } = defaultConfig,
  disableAnimateCachedImg: boolean = false,
  logConsoleError: boolean,
) {
  Iif (!window.IntersectionObserver) require('intersection-observer');
  // $FlowIgnoreLine:
  const observer = new IntersectionObserver(entries => onIntersection(entries), config);
 
  window.__REACT_SIMPLE_IMG__ = {
    observer,
    imgLoadingRefs: new Map(),
    callBackRefs: new Map(),
    disableAnimateCachedImg,
    logConsoleError,
  };
 
  return undefined;
}