Skip to content

Split url-replacement to var-substitution to allow non-url subs#6257

Closed
mkhatib wants to merge 3 commits intoampproject:masterfrom
mkhatib:var-subs
Closed

Split url-replacement to var-substitution to allow non-url subs#6257
mkhatib wants to merge 3 commits intoampproject:masterfrom
mkhatib:var-subs

Conversation

@mkhatib
Copy link
Copy Markdown
Contributor

@mkhatib mkhatib commented Nov 20, 2016

As per this discussion moving var-sub to its own service.

Copy link
Copy Markdown
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

LGTM, but I'm just assuming this is a copy-paste, right?

installVarSubstitutionForEmbed(ampdoc, embedWin, varSource);
installServiceInEmbedScope(embedWin, 'url-replace',
new UrlReplacements(ampdoc, varSource));
new UrlReplacements(ampdoc));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to install installVarSubstitutionServiceForDoc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am installing it for the embed scope, I think that should be enough no?

cc/ @avimehta

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe it should be enough because service for doc is installed elsewhere. otherwise, will it be okay to call it here again? it should be a noop if the service is already installed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do you mean service for doc is installed elsewhere, you mean runtime.js call?

It would be a noop if we call it again here, but I don't understand why would we call it here since we haven't been installing it here before this change?

} from './var-substitution-impl';
import {varSubstitutionForDoc} from '../var-substitution';
import {
VariableSource,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What are these being imported for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The gulp check-types throw errors because it doesn't know the Defs and the VariableSource type. I've noticed that we do add them to externs sometimes, should I do that instead?

This still breaks linting for example giving me VariableSource is defined but never used problem. So not sure how to best fix this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

They types should be something like {../path/to/variable-source.VariableSource}?

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 22, 2016

Yes, this is mostly cut-paste with minimal changes to url-replacement-impl, I kept the tests there on purpose but also copied them to also cover var-substitution-impl separately.

@avimehta
Copy link
Copy Markdown
Contributor

/cc @dvoytenko Initially when I created VariableSource, Dima asked me to not make VariableSource as a service(it was a service initially). Just want to make sure he is aware of this change.

@avimehta
Copy link
Copy Markdown
Contributor

Also, A4AVariableSource currently "gets" the global variable source from url-replacements service. We can probably change it do directly request the service instead if we decide to go ahead with this PR.

VariableSource,
ResolverReturnDef,
SyncResolverDef,
} from './variable-source';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto for these imports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried this, didn't work because maybe defs are not exported, will export and try again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@dvoytenko
Copy link
Copy Markdown
Contributor

If we have good use cases, I don't mind, but is there a real value to have it as a external service?

@jridgewell
Copy link
Copy Markdown
Contributor

Both Analytics and Forms (is it the forms installed with runtime or the forms extension?) will depend on now. Code size win?

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 23, 2016

Forms extension. We also, wanted to make sure to separate general variable substitution from URL-specific replacements for specific extra protection for url-replacements (like the protocol mismatching). Forms will use var-subs inside hidden fields directly rather than the full URL.

@dvoytenko
Copy link
Copy Markdown
Contributor

@jridgewell The url replacer is a core service so probably no code size effect.
@mkhatib That seems like a smallish nuance of API.

There's on issue with service proliferation: they are kind of hard to maintain when they need to be customized for embeds. Where we had a single service customized, now there are two that depend on each other in a bit confusing way.

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 23, 2016

Do we just not do this and expose a URL-replacement API that is expandStringSync and expandStringAsync instead? It just becomes a confusing API specially the naming, so maybe we should refactor url-replacement to be var-substitution and have APIs that are expandUrlSync and expandUrlAsync.

We really need a better way to handle embeds though. In my other PRs, it's confusing to know which context is this going to run in and how to handle the different contexts.

I don't have a preference of which path we take in this PR, happy to close it and send a new one with new APIs to url-replacement, I'll leave the say to @jridgewell

@jridgewell
Copy link
Copy Markdown
Contributor

I'll leave the say to @jridgewell

Defering to @dvoytenko. 😛

If we go the other route, I'd prefer the URL replacements to be the specialized method names.

@dvoytenko
Copy link
Copy Markdown
Contributor

I'd keep one service and come up with the good new method names and deprecate the old ones and cleanup in a couple of weeks.

@mkhatib mkhatib closed this Nov 23, 2016
@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 23, 2016

So I am assuming naming is still going to be url-replacement for the service?

Suggestions for good API names?

  • Old expandSync -> deprecate (alias to expandUrlSync until removed)
  • Old expandAsync -> deprecate (alias to expandUrlAsync until removed)
  • expandUrlSync -> Does extra URL protocol checks
  • expandUrlAsync -> Does extra URL protocol checks
  • expandStringSync -> No URL protocols checks - but usage MUST be reviewed - enforced through presubmits.
  • expandStringAsync -> No URL protocols checks - but usage MUST be reviewed - enforced through presubmits.

@dvoytenko
Copy link
Copy Markdown
Contributor

This sounds good. To confirm though, do forms need string interpolation or just variable resolution?

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 23, 2016

Sent a new PR #6323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants