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

Measure document height when necessary and send to viewer #7327

Merged
merged 4 commits into from
Feb 17, 2017

Conversation

muxin
Copy link
Contributor

@muxin muxin commented Feb 3, 2017

Implements #6530

  • Measure document height when necessary
    • deferMutate
    • mutateElement
    • resize
  • Manual tested in three types of viewport.

@@ -859,6 +871,19 @@ export class Resources {
this.visibilityStateMachine_.setState(
this.viewer_.getVisibilityState()
);

this.vsync_.measure(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This might be expensive to do every time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I should move the if statement outside.

@muxin muxin requested a review from dvoytenko February 3, 2017 22:14
@muxin muxin force-pushed the document-height branch 2 times, most recently from 0003cf1 to db457b3 Compare February 3, 2017 22:33
@muxin
Copy link
Contributor Author

muxin commented Feb 3, 2017

Tests coming, but PTAL @dvoytenko

@muxin muxin mentioned this pull request Feb 6, 2017
@muxin muxin force-pushed the document-height branch 3 times, most recently from 696b5eb to 2fbf4ec Compare February 7, 2017 01:08
@muxin
Copy link
Contributor Author

muxin commented Feb 13, 2017

@dvoytenko I marked the maybeChangeHeight_ inside mutateWork_ and checked it in the doPass_, is this fine?

this.scrollHeight_ = measuredScrollHeight;
dev().fine(TAG_, 'document height changed: ' + this.scrollHeight_);
}
this.maybeChangeHeight_ = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Set it to false before vsync.measure(). In other words as a first line inside the if. This will avoid running several measures unnecessary.

@@ -859,6 +871,19 @@ export class Resources {
this.visibilityStateMachine_.setState(
this.viewer_.getVisibilityState()
);

if (this.maybeChangeHeight_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add unit-tests for this?

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'm thinking about assigning true to maybeChangeHeight_ and call doPass_ directly in the test, is it fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

@muxin muxin force-pushed the document-height branch 2 times, most recently from 611447e to 961dd56 Compare February 16, 2017 21:22
if (measuredScrollHeight != this.scrollHeight_) {
this.viewer_.sendMessage('documentHeight',
{height: measuredScrollHeight}, /* cancelUnsent */true);
this.scrollHeight_ = measuredScrollHeight;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the test, the code inside vsync_.meanure is happening after the expect(resources.scrollHeight_).to.equal... which makes the test to fail. But later resources.scrollHeight is changed. How could I run expect after vsync_.measure? @dvoytenko

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to make #measure execute immediately.


if (this.maybeChangeHeight_) {
this.maybeChangeHeight_ = false;
this.vsync_.measure(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We can still queue up multiple measures here. We can add a second boolean hasMeasuredDocHeight, setting to false when we queue, and setting to true when it's measured. Then, when we see that maybeChangeHeight_ && !hasMeasuredDocHeight, we know we can skip since it's already queued.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think doPass_ happens less frequently than vsync.measure, maybe there's no need to dedupe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jridgewell I would rather not do this optimization now, if in the future this become a performance problem we can recheck.

if (measuredScrollHeight != this.scrollHeight_) {
this.viewer_.sendMessage('documentHeight',
{height: measuredScrollHeight}, /* cancelUnsent */true);
this.scrollHeight_ = measuredScrollHeight;
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to make #measure execute immediately.

@muxin
Copy link
Contributor Author

muxin commented Feb 17, 2017

Tests updated, PTAL

Copy link
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

LGTM on my side.

@muxin muxin merged commit 7071ff1 into ampproject:master Feb 17, 2017
@muxin muxin deleted the document-height branch February 17, 2017 22:17
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants