MCPcopy
hub / github.com/ampproject/amphtml / whenContentIniLoadInOb

Function whenContentIniLoadInOb

src/ini-load.js:88–142  ·  view source on GitHub ↗
(elementOrAmpDoc, opt_prerenderableOnly)

Source from the content-addressed store, hash-verified

86 * TODO(#31915): remove visibility
87 */
88export function whenContentIniLoadInOb(elementOrAmpDoc, opt_prerenderableOnly) {
89 const ampdoc = Services.ampdoc(elementOrAmpDoc);
90 // First, wait for the `ready-scan` signal. Waiting for each element
91 // individually is too expensive and `ready-scan` will cover most of
92 // the initially parsed elements.
93 const whenReady = ampdoc.signals().whenSignal(READY_SCAN_SIGNAL);
94 return whenReady.then(() => {
95 // Filter elements.
96 const resources = Services.resourcesForDoc(ampdoc);
97 const elements = resources
98 .get()
99 .filter((r) => {
100 if (opt_prerenderableOnly && !r.prerenderAllowed()) {
101 return false;
102 }
103 return !EXCLUDE_INI_LOAD.includes(r.element.tagName);
104 })
105 .map((r) => r.element);
106
107 if (elements.length === 0) {
108 return Promise.resolve([]);
109 }
110
111 // Find intersecting elements.
112 return new Promise((resolve) => {
113 const {win} = ampdoc;
114 const io = new win.IntersectionObserver(
115 (entries) => {
116 io.disconnect();
117 const intersecting = [];
118 for (let i = 0; i < entries.length; i++) {
119 const {isIntersecting, target} = entries[i];
120 if (isIntersecting) {
121 intersecting.push(target);
122 }
123 }
124 resolve(intersecting);
125 },
126 {
127 // We generally always want `root: document` here. However, in
128 // many browsers this is still polyfilled and `{root: null}` is
129 // a lot faster.
130 root: isIframed(win) ? /** @type {?} */ (win.document) : null,
131 threshold: 0.01,
132 }
133 );
134 // Limit check to the first 100 elements.
135 for (let i = 0; i < Math.min(elements.length, 100); i++) {
136 io.observe(elements[i]);
137 }
138 }).then((elements) => {
139 return Promise.all(elements.map((element) => element.whenLoaded()));
140 });
141 });
142}
143
144/**
145 * Returns a subset of resources which are (1) belong to the specified host

Callers 2

test-ini-load.jsFile · 0.90
whenContentIniLoadFunction · 0.85

Calls 13

isIframedFunction · 0.90
whenSignalMethod · 0.80
resolveMethod · 0.80
whenLoadedMethod · 0.80
resolveFunction · 0.50
signalsMethod · 0.45
thenMethod · 0.45
filterMethod · 0.45
getMethod · 0.45
prerenderAllowedMethod · 0.45
disconnectMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected