initializeLazyLoading method

void initializeLazyLoading(
  1. Element slot
)

Enables lazy loading for the slot to be initialized on the page when it is queued.

Implementation

void initializeLazyLoading(Element slot) {
  Element? slotParent = slot.parent;
  if (slotParent == null) return;

  int slotPosition = slotParent.children.indexOf(slot);
  if (slotPosition < 0) return;

  Element trigger = slotParent;
  if (slotPosition != 0) {
    // If the parent has children other than a slot,
    // a child must be taken before the slot. Usually the parent is used,
    // because of the lack of difference in the absence of children.
    trigger = slotParent.children[slotPosition - 1];
  }

  IntersectionObserver observer = IntersectionObserver((entries, observer) {
    for (var entry in entries) {
      if (!entry.isIntersecting) return;

      initialize(slot, true);

      observer.unobserve(trigger);
    }
  });

  observer.observe(trigger);
}