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

AMP Fast Fetch: Attempt to revert resize changes within unlayoutCallback #9066

Merged
merged 5 commits into from May 2, 2017
Merged

AMP Fast Fetch: Attempt to revert resize changes within unlayoutCallback #9066

merged 5 commits into from May 2, 2017

Conversation

keithwrightbos
Copy link
Contributor

When users swipe between pages within the viewer, unlayoutCallback is executed which resets state. This reset should include attempting to revert the slot size back to its original as it could have been modified via resizes or collapse.

cc/ @ampproject/a4a

Copy link
Contributor

@glevitzky glevitzky left a comment

Choose a reason for hiding this comment

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

LGTM; just have one question.

@@ -279,6 +279,9 @@ export class AmpA4A extends AMP.BaseElement {
height: this.element.getAttribute('height'),
};

/** @private {?../../../src/layout-rect.LayoutRectDef} */
this.originalSlotSize_ = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not initialize with the correct value here, once and for good?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seemed sensical to wait for onLayoutMeasure to ensure the values derived were correct (and its not required before then).

// Store original size of slot in order to allow re-expansion on
// unlayoutCallback so that it is reverted to original size in case
// of resumeCallback.
this.originalSlotSize_ = this.getIntersectionElementLayoutBox();
Copy link
Contributor

Choose a reason for hiding this comment

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

#getLayoutBox. #getIntersectionElementLayoutBox is meant to be overridable for getting the iframe's layout box.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// Store original size of slot in order to allow re-expansion on
// unlayoutCallback so that it is reverted to original size in case
// of resumeCallback.
this.originalSlotSize_ = this.getIntersectionElementLayoutBox();
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can the ad never request another resize? Shouldn't this be only set once?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, check for this.originalSlotSize_ already been set.

@@ -958,6 +984,9 @@ export class AmpA4A extends AMP.BaseElement {
*/
forceCollapse() {
dev().assert(this.uiHandler);
// Store original size to allow for reverting on unlayoutCallback so that
// subsequent pageview allows for ad request.
this.originalSlotSize_ = this.getIntersectionElementLayoutBox();
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto only setting once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

if (this.originalSlotSize_) {
// Attempt to revert any size change, this could fail but is unlikely as
// we can assume the document is no longer visible.
protectFunctionWrapper(
Copy link
Contributor

Choose a reason for hiding this comment

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

This crazy thing is useless. #attemptChangeSize will never throw an error, but it might return a rejected promise which you're not handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@keithwrightbos
Copy link
Contributor Author

@jridgewell PTAL Thanks

super.attemptChangeSize(
this.originalSlotSize_.height, this.originalSlotSize_.width)
.then(() => {
this.originalSlotSize_ = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Left so that if resize fails, it could reattempt on next unlayoutCallback attempt (user swipes back and forth). Perhaps that won't happen in practice but can't hurt :)

Copy link
Contributor

Choose a reason for hiding this comment

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

But it would attempt to change back to an already changed size, not the original.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I follow. Given we don't override the value if set, it would
attempt to go back to the first size, no?

Sorry, missed that this was in the onResolve block.

/** @override */
unlayoutCallback() {
// Increment promiseId to cause any pending promise to cancel.
this.promiseId_++;
this.protectedEmitLifecycleEvent_('adSlotCleared');
this.uiHandler.setDisplayState(AdDisplayState.NOT_LAID_OUT);
if (this.originalSlotSize_) {
super.attemptChangeSize(
Copy link
Contributor

Choose a reason for hiding this comment

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

@dvoytenko: Since this is in #unlayoutCallback, would a call to #changeSize be ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

That'd be dangerous since unlayout is not necessary due to whole document being backgrounded. I think it'd be better to use "attempt", but ensure that in Resources "attempt" is equivalent to "certain change" when viewer.isVisible() == false. I think it should be the case now, but always good to check.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yah, attempt will always succeed if the page isn't visible. Thanks for the clarification.

@keithwrightbos
Copy link
Contributor Author

keithwrightbos commented May 2, 2017 via email

@tdrl tdrl merged commit 609c61b into ampproject:master May 2, 2017
@tdrl tdrl deleted the a4a_unlayout_resize branch May 2, 2017 20:13
KenneyE pushed a commit to spotxchange/amphtml that referenced this pull request May 3, 2017
…ack (ampproject#9066)

* Attempt to revert resize changes within unlayoutcallback

* review feedback

* fix lint error

* fix type error

* fix test failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants