All files / modules x-intersection-observer.ts

100% Statements 39/39
100% Branches 3/3
100% Functions 10/10
100% Lines 37/37

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135        94x                           94x                                                                                               52x 52x   52x 123x   123x 170x 99x 99x     71x     123x 37x       52x         52x 52x 1x   52x 1x     52x 106x 130x 130x       52x 153x     169x 69x     100x 100x       52x 61x 70x 70x       52x 1x 1x     52x      
/**
 * @module Modules/XIntersectionObserver
 */
 
import * as MH from "@lisn/globals/minification-helpers";
 
export type XIntersectionObserverCallback = (
  entries: IntersectionObserverEntry[],
  observer: XIntersectionObserver,
) => void;
 
/**
 * {@link XIntersectionObserver} is an extension of
 * {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver | IntersectionObserver}
 * with added capabilities:
 * - can skip the initial callback that happens shortly after setting up via
 *   {@link observeLater}
 */
export class XIntersectionObserver {
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root | IntersectionObserver:root}.
   */
  readonly root!: Element | Document | null;
 
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin | IntersectionObserver:rootMargin}.
   */
  readonly rootMargin!: string;
 
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds | IntersectionObserver:thresholds}.
   */
  readonly thresholds!: number[];
 
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/observe | IntersectionObserver:observe} except it accepts multiple
   * targets.
   */
  readonly observe: (...targets: Element[]) => void;
 
  /**
   * Like {@link observe} but it ignores the initial almost immediate callback
   * and only calls the callback on a subsequent intersection change.
   */
  readonly observeLater: (...targets: Element[]) => void;
 
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/unobserve | IntersectionObserver:unobserve} except it accepts multiple
   * targets.
   */
  readonly unobserve: (...targets: Element[]) => void;
 
  /**
   * Like {@link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/disconnect | IntersectionObserver:disconnect}.
   */
  readonly disconnect: () => void;
 
  /**
   * Like `IntersectionObserver.takeRecords`.
   */
  readonly takeRecords: () => void;
 
  constructor(
    callback: XIntersectionObserverCallback,
    observeOptions?: IntersectionObserverInit,
  ) {
    let observedTargets = MH.newWeakSet<Element>();
    const targetsToSkip = MH.newWeakSet<Element>();
 
    const intersectionHandler = (entries: IntersectionObserverEntry[]) => {
      const selectedEntries = [];
 
      for (const entry of entries) {
        if (targetsToSkip.has(MH.targetOf(entry))) {
          MH.deleteKey(targetsToSkip, MH.targetOf(entry));
          continue;
        }
 
        selectedEntries.push(entry);
      }
 
      if (MH.lengthOf(selectedEntries)) {
        callback(selectedEntries, this);
      }
    };
 
    const observer = MH.newIntersectionObserver(
      intersectionHandler,
      observeOptions,
    );
 
    MH.defineProperty(this, "root", { get: () => observer.root });
    MH.defineProperty(this, "rootMargin", {
      get: () => observer.rootMargin,
    });
    MH.defineProperty(this, "thresholds", {
      get: () => observer.thresholds,
    });
 
    this.observe = (...targets) => {
      for (const target of targets) {
        observedTargets.add(target);
        observer.observe(target);
      }
    };
 
    this.observeLater = (...targets) => {
      for (const target of targets) {
        // Only skip them if not already observed, otherwise the initial
        // (almost) immediate callback won't happen anyway.
        if (observedTargets.has(target)) {
          continue;
        }
 
        targetsToSkip.add(target);
        this.observe(target);
      }
    };
 
    this.unobserve = (...targets) => {
      for (const target of targets) {
        MH.deleteKey(observedTargets, target);
        observer.unobserve(target);
      }
    };
 
    this.disconnect = () => {
      observedTargets = MH.newWeakSet();
      observer.disconnect();
    };
 
    this.takeRecords = () => observer.takeRecords();
  }
}