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

Layers: Scoring Algorithm #12958

Merged
merged 12 commits into from Jan 30, 2018
Merged

Conversation

jridgewell
Copy link
Contributor

@jridgewell jridgewell commented Jan 23, 2018

This implements the scoring algorithm for Layers. It does not change how viewport distance is calculated yet, which prevents them from being scored far-out of viewport.

/cc @dvoytenko on the #ancestry iteration. I ended up waffling, and allow direct access to the LayoutElement during iteration, instead of forcing the caller to do the calculations (with a bunch of parameters to the iterator).

Fixes #12650.

Copy link

@dreamofabear dreamofabear left a comment

Choose a reason for hiding this comment

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

Just a bunch of minor comments.

* @return {boolean} The boolean value.
*/
assertBoolean(shouldBeBoolean, opt_message) {
this.assert(!!shouldBeBoolean === shouldBeBoolean,

Choose a reason for hiding this comment

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

This is faster than typeof?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷‍♂️

}
from.remeasure();

Choose a reason for hiding this comment

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

Feels like this should be inverted name-wise.

if (force)
  remeasure()
else
  requestRemeasure()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not in an else. The #requestRemeasure ensure that the layout thinks in needs to be measured (dirties), while the #remeasure does the actual measuring.

Choose a reason for hiding this comment

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

Ok, how about changing requestMeasure to setDirty or setNeedsRemeasure? 🚲 🏚

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

*
* @return {boolean}
*/
isActive() {

Choose a reason for hiding this comment

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

isActiveUnsafe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it throws?

Choose a reason for hiding this comment

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

Because you shouldn't call it unless you really know what you're doing. Similar to "unsafe" suffixes in resources-impl.js.

const win = /** @type {!Window } */ (dev().assert(
element.ownerDocument.defaultView));
// If it remains fixed, it will still be a layer.
if (computedStyle(win, element).position === 'fixed') {

Choose a reason for hiding this comment

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

How can we ensure this is called in a measure cycle (without interleaved mutates)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

/**
* Gets the minimal horizontal distance of this element from it's parent's

Choose a reason for hiding this comment

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

Nit: its

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know the difference.... 😳

This is literally (as in literally, not figuratively) the one contraction I always screw up.

Choose a reason for hiding this comment

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

I had this wrong for years.

@@ -682,6 +693,8 @@ export class Resource {
let scrollPenalty = 1;
let distance = 0;

// TODO

Choose a reason for hiding this comment

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

Elaborate or remove.

this.schedulePass();
});

/** @const */

Choose a reason for hiding this comment

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

@private and add type.

}

/**
* Calculates the ancestry score... todo description trololololol.

Choose a reason for hiding this comment

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

:trollface: Please fix.

* @return {T}
* @template T
*/
ancestry(element, iterator, state) {

Choose a reason for hiding this comment

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

This name is really too vague. iterateAncestry?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. 🚲🏚

layer = layer.getParentLayer();
}

let value;

Choose a reason for hiding this comment

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

accumulator since this is reduce-like, and explicitly initialize to undefined.

Copy link

@dreamofabear dreamofabear left a comment

Choose a reason for hiding this comment

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

Following up with tests after merge I assume?

@@ -74,6 +79,19 @@ function positionLt(left, top) {
};
}

/**
* Stores an LayoutElement's parent layer ancestry (temporarily).

Choose a reason for hiding this comment

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

Mention that this an optimization is to avoid array creation in a tight inner loop for interateAncestry.

// Gather, and update whether the layers are descendants of the active
// layer.
let isActive = activeLayer === this || activeLayer.contains(this);

Choose a reason for hiding this comment

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

Maybe dev().assert that cache is empty before use.

@@ -413,6 +482,9 @@ export class LayoutElement {
*/
this.scrollTop_ = 0;

/**
*

Choose a reason for hiding this comment

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

What is this?

@@ -46,6 +46,11 @@ export let SizeDef;
*/
export let PositionDef;

/**
* @typedef {Object<string, *>}

Choose a reason for hiding this comment

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

"Anonymous state/cache passed from ancestry iterator."

When I first read this I thought it was "state of ancestry".

@jridgewell jridgewell added this to TODO in Layers via automation Jan 30, 2018
@jridgewell jridgewell moved this from TODO to In Progress in Layers Jan 30, 2018
@jridgewell jridgewell merged commit 578f708 into ampproject:master Jan 30, 2018
Layers automation moved this from In Progress to Done Jan 30, 2018
@jridgewell jridgewell deleted the amp-layers-3 branch January 30, 2018 23:07
RanAbram pushed a commit to RanAbram/amphtml that referenced this pull request Mar 12, 2018
* Add comments for future todos

* Ensure size/position is always re-measured

* scoring

* Fix fixed-position undeclare layer

* Ensure a remeasure happens

* Fix cache and score

* Fix width/height calc

* Add force remeasurement

More information is necessary from the caller to determine whether it
should be forced.

* Lint

* Review comments

* Review comments

* Fix renames
protonate pushed a commit to protonate/amphtml that referenced this pull request Mar 15, 2018
* Add comments for future todos

* Ensure size/position is always re-measured

* scoring

* Fix fixed-position undeclare layer

* Ensure a remeasure happens

* Fix cache and score

* Fix width/height calc

* Add force remeasurement

More information is necessary from the caller to determine whether it
should be forced.

* Lint

* Review comments

* Review comments

* Fix renames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Layers
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants