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
Conversation
7c833a4
to
f31b399
Compare
export class A4AVariableSource extends VariableSource { | ||
constructor(ampdoc, window) { | ||
super(); | ||
this.globalVariableSource_ = urlReplacementsForDoc(ampdoc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsdoc
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsdoc
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsdoc with @param
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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))) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 @@ | |||
/** |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/ ?
There was a problem hiding this comment.
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/
:)
Tests are pending. For ampproject#5657.
@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. |
@avimehta For that unit test, we simply need to call |
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: +2sp
There was a problem hiding this comment.
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.
* Create url-replacement for embeds. For ampproject#5657.
* Create url-replacement for embeds. For ampproject#5657.
* Create url-replacement for embeds. For ampproject#5657.
For #5657.