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

Add AMP loader to <amp-facebook-*> #14545

Merged
merged 8 commits into from Apr 13, 2018
Merged

Conversation

nainar
Copy link
Contributor

@nainar nainar commented Apr 11, 2018

This fixes #13573

We send a message from the 3p.js file over to the <amp-facebook-*>.js API to disable the loader when the SDK is loaded and ready to paint.

@nainar nainar requested a review from cvializ April 11, 2018 13:59
@nainar
Copy link
Contributor Author

nainar commented Apr 11, 2018

@cvializ could you take a look? Thanks!

if (this.iframe_ && event.source != this.iframe_.contentWindow) {
return;
}
if (!getData(event) || !(isObject(getData(event))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would cache the value of getData(event) in a local variable so you don't have to keep calling getData.

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.

}

/** @const {?JsonObject} */
const eventData = /** @type {?JsonObject} */ (isObject(getData(event))
Copy link
Contributor

Choose a reason for hiding this comment

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

When would event.data be a JSON string and when would it be a JSON object? Does the Facebook API return both?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No this is because the iframe is also sending some ill formed JSON messages which I need to gracefully handle as well. Had to do the same for amp-bodymovin-animation.js - https://github.com/ampproject/amphtml/blob/master/extensions/amp-bodymovin-animation/0.1/amp-bodymovin-animation.js#L145.

return;
}
if (!getData(event) || !(isObject(getData(event))
|| startsWith(/** @type {string} */ (getData(event)), '{'))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Attempting to parseJson might be a better check of whether it is valid JSON rather than checking the first character. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Definitely. Didn't know a function like tryParseJSon existed. Changing the code to use that instead.

Copy link
Contributor Author

@nainar nainar left a comment

Choose a reason for hiding this comment

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

@cvializ made the changes you asked for.

}

/** @const {?JsonObject} */
const eventData = /** @type {?JsonObject} */ (isObject(getData(event))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No this is because the iframe is also sending some ill formed JSON messages which I need to gracefully handle as well. Had to do the same for amp-bodymovin-animation.js - https://github.com/ampproject/amphtml/blob/master/extensions/amp-bodymovin-animation/0.1/amp-bodymovin-animation.js#L145.

return;
}
if (!getData(event) || !(isObject(getData(event))
|| startsWith(/** @type {string} */ (getData(event)), '{'))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Definitely. Didn't know a function like tryParseJSon existed. Changing the code to use that instead.

if (this.iframe_ && event.source != this.iframe_.contentWindow) {
return;
}
if (!getData(event) || !(isObject(getData(event))
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.

return;
}
let eventData = getData(event);
if (!eventData || !(isObject(eventData) || tryParseJson(eventData))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still having a little trouble quickly reading these if statements. Would something like this be equivalent?

const eventData = getData(event);
if (!eventData) {
  return;
}

const parsedEventData = isObject(eventData) ? eventData : tryParseJson(eventData);
if (!parsedEventData) {
  return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made the changes you asked for. Thanks for the suggestion!

Copy link
Contributor

Choose a reason for hiding this comment

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

You're welcome! And if you ever feel like my suggestions are too nit-picky feel free to say you'll keep it how you prefer.

@cvializ
Copy link
Contributor

cvializ commented Apr 13, 2018

Consider adding a test for this feature as a follow up. I see a test for resize messages in test-amp-facebook.js

it('resizes facebook posts', () => {

@cvializ cvializ merged commit 8b80a82 into ampproject:master Apr 13, 2018
glevitzky pushed a commit that referenced this pull request Apr 27, 2018
* Add loader to <amp-facebook-*>

* Travis checks

* Undo unintentional changes

* Refactor code to reduce function calls. Use tryParseJson

* Travis fixes

* refaactor handleFacebookMessages

* travis fixes
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.

amp-facebook-* do not show the AMP loader
3 participants