Skip to content

Commit d10dd3c

Browse files
committed
Eliminate timeout for disconneccting ResizeObsever
1 parent b8e86d6 commit d10dd3c

File tree

1 file changed

+1
-22
lines changed

1 file changed

+1
-22
lines changed

plugins/embed-optimizer/detect.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ function log( ...message ) {
1515
console.log( consoleLogPrefix, ...message );
1616
}
1717

18-
/*
19-
* Observe the loading of embeds on the page. We need to run this now before the resources on the page have fully
20-
* loaded because we need to start observing the embed wrappers before the embeds have loaded. When we detect
21-
* subtree modifications in an embed wrapper, we then need to measure the new height of the wrapper element.
22-
* However, since there may be multiple subtree modifications performed as an embed is loaded, we need to wait until
23-
* what is likely the last mutation.
24-
* TODO: This is a magic number. Ideally we wouldn't need this.
25-
*/
26-
const EMBED_LOAD_WAIT_MS = 5000;
27-
2818
/**
2919
* Embed element heights.
3020
*
@@ -69,7 +59,7 @@ export async function finalize( { urlMetric, isDebug } ) {
6959
if ( loadedElementContentRects.has( element.xpath ) ) {
7060
if ( isDebug ) {
7161
log(
72-
'Overriding:',
62+
`Overriding boundingClientRect for ${ element.xpath }:`,
7363
element.boundingClientRect,
7464
'=>',
7565
loadedElementContentRects.get( element.xpath )
@@ -93,20 +83,9 @@ function monitorEmbedWrapperForResizes( embedWrapper ) {
9383
throw new Error( 'Embed wrapper missing data-od-xpath attribute.' );
9484
}
9585
const xpath = embedWrapper.dataset.odXpath;
96-
let timeoutId = 0;
9786
const observer = new ResizeObserver( ( entries ) => {
9887
const [ entry ] = entries;
99-
if ( timeoutId > 0 ) {
100-
clearTimeout( timeoutId );
101-
}
102-
log(
103-
`Pending embed height of ${ entry.contentRect.height }px for ${ xpath }`
104-
);
10588
loadedElementContentRects.set( xpath, entry.contentRect );
106-
// TODO: Is the timeout really needed? We can just keep updating the height of the element until the URL metrics are sent when the page closes.
107-
timeoutId = setTimeout( () => {
108-
observer.disconnect();
109-
}, EMBED_LOAD_WAIT_MS );
11089
} );
11190
observer.observe( embedWrapper, { box: 'content-box' } );
11291
}

0 commit comments

Comments
 (0)