Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intersect-resources: Fix incorrect pre-layout assumption #27684

Merged
merged 8 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/service/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,16 @@ export class Resource {
/**
* Whether the resource is displayed, i.e. if it has non-zero width and
* height.
* @param {!ClientRect=} opt_premeasuredRect If provided, use this
* premeasured ClientRect instead of using the cached layout box.
* @param {boolean} usePremeasuredRect If true and a premeasured rect is
* available, use it. Otherwise, use the cached layout box.
* @return {boolean}
*/
isDisplayed(opt_premeasuredRect) {
isDisplayed(usePremeasuredRect = false) {
devAssert(!usePremeasuredRect || !this.intersect_);
const isFluid = this.element.getLayout() == Layout.FLUID;
const box = opt_premeasuredRect || this.getLayoutBox();
const box = usePremeasuredRect
? devAssert(this.premeasuredRect_)
: this.getLayoutBox();
const hasNonZeroSize = box.height > 0 && box.width > 0;
return (
(isFluid || hasNonZeroSize) &&
Expand Down
27 changes: 20 additions & 7 deletions src/service/resources-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,14 +1420,28 @@ export class ResourcesImpl {
const reschedule = this.reschedule_.bind(this, task);
executing.promise.then(reschedule, reschedule);
} else {
// With IntersectionObserver, the element's client rect measurement
// is recent so immediate remeasuring shouldn't be necessary.
if (!this.intersectionObserver_) {
task.resource.measure();
const {resource} = task;

let stillDisplayed = true;
if (this.intersectionObserver_) {
// With IntersectionObserver, peek at the premeasured rect to see
// if the resource is still displayed (has a non-zero size).
// The premeasured rect is most analogous to an immediate measure.
if (resource.hasBeenPremeasured()) {
Copy link
Member

@samouri samouri Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in thinking this is to catch the case where the IO has fired and inserted the premeasuredRect but the discoverWork block of the pass hasn't happened yet? Therefore we are trying to catch cases that have hidden themselves in the meantime

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisely!

stillDisplayed = resource.isDisplayed(
/* usePremeasuredRect */ true
);
}
} else {
// Remeasure can only update isDisplayed(), not in-viewport state.
resource.measure();
}
// Check if the element has exited the viewport or the page has changed
// visibility since the layout was scheduled.
if (this.isLayoutAllowed_(task.resource, task.forceOutsideViewport)) {
if (
stillDisplayed &&
this.isLayoutAllowed_(resource, task.forceOutsideViewport)
) {
task.promise = task.callback();
task.startTime = now;
dev().fine(TAG_, 'exec:', task.id, 'at', task.startTime);
Expand All @@ -1439,9 +1453,8 @@ export class ResourcesImpl {
)
.catch(/** @type {function (*)} */ (reportError));
} else {
devAssert(!this.intersectionObserver_);
dev().fine(TAG_, 'cancelled', task.id);
task.resource.layoutCanceled();
resource.layoutCanceled();
}
}

Expand Down
Loading