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-bind: Fix state/ready race condition #21033

Merged
merged 6 commits into from Feb 25, 2019

Conversation

dreamofabear
Copy link

@dreamofabear dreamofabear commented Feb 22, 2019

In some cases, bindReady viewer message can be sent before all <amp-state> elements are initialized (local data parsed). Fix this by:

  1. Allowing parsing local data (<script> child) in buildCallback() before viewer is visible.
  2. Don't send bindReady message until all <amp-state> are built.

@dreamofabear dreamofabear marked this pull request as ready for review February 24, 2019 18:43
@dreamofabear
Copy link
Author

/to @alabiaga

this.updateState_(json, isInit);
});
// Don't fetch in prerender mode.
const viewer = Services.viewerForDoc(this.getAmpDoc());
Copy link
Contributor

Choose a reason for hiding this comment

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

fetchAndUpdate_ is also called in the registered refresh action method call. We should set the viewer service as a private property on the buildCallback so that we don't need to get the service everything time this method is called.

Copy link
Author

Choose a reason for hiding this comment

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

I'd prefer not to do this optimization due to the readability and testing cost. I don't think it makes a perf difference in practice.

this.fetchAndUpdate_(/* isInit */ true);
}

this.registerAction('refresh', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I should have made this change when I added refresh support for amp-state but we should only register the action if the element has a src attribute. We should move this block in the if statement above.

if (this.element.hasAttribute('src')) {
  this.fetchAndUpdate_(/* isInit */ true);
  this.registerAction('refresh', ...
}

Copy link
Author

Choose a reason for hiding this comment

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

Good call. Though it's possible for the element to not have a src attribute at buildCallback but have one later (via amp-bind), so I added a userAssert here instead.

Copy link
Author

@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.

Thanks for the prompt review.

this.fetchAndUpdate_(/* isInit */ true);
}

this.registerAction('refresh', () => {
Copy link
Author

Choose a reason for hiding this comment

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

Good call. Though it's possible for the element to not have a src attribute at buildCallback but have one later (via amp-bind), so I added a userAssert here instead.

this.updateState_(json, isInit);
});
// Don't fetch in prerender mode.
const viewer = Services.viewerForDoc(this.getAmpDoc());
Copy link
Author

Choose a reason for hiding this comment

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

I'd prefer not to do this optimization due to the readability and testing cost. I don't think it makes a perf difference in practice.

@dreamofabear dreamofabear merged commit 4746f19 into ampproject:master Feb 25, 2019
@dreamofabear dreamofabear deleted the bind-ready-race branch February 25, 2019 18:10
noranazmy pushed a commit to noranazmy/amphtml that referenced this pull request Mar 22, 2019
* First pass on fixing race condition.

* Fix amp-state tests.

* Fix test-bind-impl.js.

* Check for unbuilt <amp-state> instead of N setState() calls.

* Tweak comments.

* Add userAssert for 'src' attribute in refresh action.
bramanudom pushed a commit to bramanudom/amphtml that referenced this pull request Mar 22, 2019
* First pass on fixing race condition.

* Fix amp-state tests.

* Fix test-bind-impl.js.

* Check for unbuilt <amp-state> instead of N setState() calls.

* Tweak comments.

* Add userAssert for 'src' attribute in refresh action.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants