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

Create url-replacement for embeds. #6133

Merged
merged 5 commits into from Nov 14, 2016
Merged

Conversation

avimehta
Copy link
Contributor

@avimehta avimehta commented Nov 10, 2016

For #5657.

@avimehta avimehta changed the title [WIP] Create url-replacement for embeds. Create url-replacement for embeds. Nov 11, 2016
export class A4AVariableSource extends VariableSource {
constructor(ampdoc, window) {
super();
this.globalVariableSource_ = urlReplacementsForDoc(ampdoc)
Copy link
Contributor

Choose a reason for hiding this comment

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

jsdoc

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.

super();
this.globalVariableSource_ = urlReplacementsForDoc(ampdoc)
.getVariableSource();
this.win_ = window;
Copy link
Contributor

Choose a reason for hiding this comment

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

jsdoc

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.


/** Provides A4A specific variable substitution. */
export class A4AVariableSource extends VariableSource {
constructor(ampdoc, window) {
Copy link
Contributor

Choose a reason for hiding this comment

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

jsdoc with @param

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 getNavigationData(this.win_, 'redirectCount');
});

for (let v = 0; v < WHITELISTED_VARIABLES.length; v++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. I'm generally more in preference with the runtime model, e.g. resolveVar that uses globalResolver.resolveVar. But let's see how this works out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is what I was initially doing.

Kind of difficult without doing a major refactoring. Currently, you need to call set() for each variable otherwise, getExpr() will not include the variable in regex and hence, the variable-source will never be called for that particular varName. As a result, substitutions never happen for variables for which set is not called.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. Let's keep going with this.

for (let v = 0; v < WHITELISTED_VARIABLES.length; v++) {
const varName = WHITELISTED_VARIABLES[v];
const resolvers = this.globalVariableSource_.getReplacement(varName);
this.set(varName, resolvers.sync).setAsync(varName, resolvers.async);
Copy link
Contributor

Choose a reason for hiding this comment

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

At list, maybe we can have a single setBoth?

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.


/** Provides A4A specific variable substitution. */
export class A4AVariableSource extends VariableSource {
constructor(ampdoc, window) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's rename window to embedWin

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 this.getTimingDataAsync_(startEvent, endEvent);
});
/** @private {boolean} */
this.initialized_ = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is this used and how?

@@ -745,7 +634,7 @@ export class UrlReplacements {
let binding;
if (opt_bindings && (name in opt_bindings)) {
binding = opt_bindings[name];
} else if ((binding = this.getReplacement_(name))) {
} else if ((binding = this.variableSource_.getReplacement(name))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call this method simply as get

* @param {!Window} embedWin
* @param {*} varSource
*/
export function installUrlReplacementsForEmbed(ampdoc, embedWin, varSource) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method should go to the url-replacements-impl. Why? To avoid any risk of all other extensions compiling the whole UrlReplacements in. Only whoever needs it will compile it by depending on url-replacements-impl directly. Does it make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is why I added the dep-check. Does that not fix it?

Copy link
Contributor

Choose a reason for hiding this comment

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

But there's no deep-check on url-replacements.js which is imported by many many extensions. Generally, all installs should go to -impl.js files, because there's no way to install the implementation unless you compile that file in anyway.

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 see. thanks for the answer.

@@ -0,0 +1,235 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should go to ./service/ dir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why so? This isn't a service now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. Maybe you are right. I was mostly reacting that it's only used by the service or service-depending code. But we can keep it here for now.

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 moved it :(. Should I keep it in src/service or move back to src/ ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No. Let's keep in ./service/ :)

@avimehta
Copy link
Contributor Author

@cramforce would you like to review this?

@dvoytenko any other tests you want me to add. Once case I can think of is for test-amp-a4a.js which ensures that new url-replacements.js is present. It's not really a unittest and I am trying to figure out how to best do it.

@dvoytenko
Copy link
Contributor

@avimehta For that unit test, we simply need to call urlReplacements(anElementInsideEmbed) and see that it's NOT the same as the global service.

@avimehta
Copy link
Contributor Author

done. I think I addressed all comments :)

installServiceInEmbedScope(embedWin, 'url-replace', {varSource});
export function installUrlReplacementsForEmbed(ampdoc, embedWin, varSource) {
installServiceInEmbedScope(embedWin, 'url-replace',
new UrlReplacements(ampdoc, varSource));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: +2sp

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. Merging the PR now.

@avimehta avimehta merged commit c45bb17 into ampproject:master Nov 14, 2016
@avimehta avimehta deleted the a4a2 branch November 14, 2016 20:26
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Nov 15, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
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

2 participants