All files / src instances.ts

100% Statements 19/19
100% Branches 1/1
100% Functions 6/6
100% Lines 18/18

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    1x 1x             1x 4x 4x             1x 4x 4x             2x           1x 2x   2x   1x 1x       2x   2x 1x     2x    
import { OverlayScrollbars } from 'overlayscrollbars';
 
const targets: Set<Element> = new Set();
const targetInstanceMap: WeakMap<Element, OverlayScrollbars> = new WeakMap();
 
/**
 * Adds the given OverlayScrollbars instance to the given element.
 * @param target The element which is the target of the OverlayScrollbars instance.
 * @param osInstance The OverlayScrollbars instance.
 */
export const addInstance = (target: Element, osInstance: OverlayScrollbars): void => {
  targetInstanceMap.set(target, osInstance);
  targets.add(target);
};
 
/**
 * Removes a OverlayScrollbars instance from the given element.
 * @param target The element from which its OverlayScrollbars instance shall be removed.
 */
export const removeInstance = (target: Element): void => {
  targetInstanceMap.delete(target);
  targets.delete(target);
};
 
/**
 * Gets the OverlayScrollbars from the given element or undefined if it doesn't have one.
 * @param target The element of which its OverlayScrollbars instance shall be get.
 */
export const getInstance = (target: Element): OverlayScrollbars | undefined => targetInstanceMap.get(target);
 
/**
 * Gets a Map which represents all active OverayScrollbars instances.
 * The Key is the ekement and the value is the instance.
 */
export const allInstances = (): ReadonlyMap<Element, OverlayScrollbars> => {
  const validTargetInstanceMap: Map<Element, OverlayScrollbars> = new Map();
 
  targets.forEach((target: Element) => {
    /* istanbul ignore else */
    if (targetInstanceMap.has(target)) {
      validTargetInstanceMap.set(target, targetInstanceMap.get(target)!);
    }
  });
 
  targets.clear();
 
  validTargetInstanceMap.forEach((instance: OverlayScrollbars, validTarget: Element) => {
    targets.add(validTarget);
  });
 
  return validTargetInstanceMap;
};