Skip to content

Implement protocol adapters#29329

Merged
samouri merged 18 commits intoampproject:masterfrom
samouri:adapters
Jul 28, 2020
Merged

Implement protocol adapters#29329
samouri merged 18 commits intoampproject:masterfrom
samouri:adapters

Conversation

@samouri
Copy link
Copy Markdown
Member

@samouri samouri commented Jul 17, 2020

summary
Implements Protocol adapters: #25719.

Introduces the concept of the amp-script src for amp-list components.
I picked this to be more in line with how initializing from amp-state works, but if anyone feels strongly I'd be
ok with switching back to the more terse fn:.

Note that amp-bind is not required for this to work.

future work

cc @morsssss

@samouri samouri self-assigned this Jul 17, 2020
@google-cla google-cla bot added the cla: yes label Jul 17, 2020
@samouri samouri marked this pull request as ready for review July 17, 2020 23:10
@amp-owners-bot amp-owners-bot bot requested a review from caroqliu July 17, 2020 23:10
);
userAssert(
!this.ssrTemplateHelper_.isEnabled(),
'[amp-list]: "amp-script" URIs cannot be used in SSR mode.'
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.

Can you install amp-script (move past the error before this one) in a situation where SSR is enabled?

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 20, 2020

Choose a reason for hiding this comment

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

Thats a good point. While this check exists for bind, <amp-script> is not valid within amp4email so it is likely unnecessary.

Although technically one could opt-in to SSR mode (and self-host the runtime to make their origin a whitelisted ssr-er) and then get past the earlier error.

🤷

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.

In the latter case, is there a reason they should not be able to opt into the amp-script protocol?

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 20, 2020

Choose a reason for hiding this comment

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

Yep! Because in the SSR case, it is the viewer that actually does the fetch. We can't expect a viewer to know how to handle amp-script: scriptId.fnId

expectAsyncConsoleError(errorMsg);
expect(list.layoutCallback()).to.eventually.throw(errorMsg);

element.setAttribute('src', 'amp-script:too.many.dots');
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.

For my learning: Is multiple dot call left out to limit scope?

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 20, 2020

Choose a reason for hiding this comment

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

As written, the dot is meant to be a separator for ampscriptId and fnIdentifier. It isn't actually reaching into an object. For that reason only a single dot is valid.

Note: we could pick essentially any separator as long as the separator isn't a valid part of a js ident


it('Waits for initialization to complete before returning', async () => {
element.setAttribute('script', 'local-script');
const result = service.callFunction('local-script', 'fetchData');
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.

Is testing for bad inputs handled in worker dom? Also can that call an identifier with dots?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think I answer that in a response to an earlier question, let me know if I missed something!
#29329 (comment)

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 may have missed if there are test cases for this in worker dom, but I mean strings that are empty or otherwise not valid js variable names, i.e. 123 or anything containing odd symbols.

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 21, 2020

Choose a reason for hiding this comment

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

We don't run the function name they pass directly into an eval() or anything like that. worker-dom exposes a function called exportFunction(string, handle) so that devs can specifically mark functions for export. As long as the string they pass in matches a string that was exported it'll work.

That is an interesting point that technically invalid identifiers can pass through this unharmed, I'm not convinced thats a bad thing.

Copy link
Copy Markdown

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

Can you add an e2e test in amp-script/test-e2e/ for this?

* @param {string} fnIdentifier
* @return {Promise<*>}
*/
callFunction(ampScriptId, fnIdentifier) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why have this intermediary function on AmpScriptService at all?

There's only a single caller but if you want to avoid the boilerplate you could also hook into the internal actions API e.g. registerAction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This would actually solve a lot of interesting problems around args parsing so I'd love to make it work.
I'm not seeing a straightforward way to pipe through the return value though

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ah yea, I guess executeAction could work but not sure it's a net win actually.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah. Calling executeAction would mean I'd still need to locate the correct element, so wouldn't help much here. A nice aspect is that if I registered the action, we'd get part (1) of this issue implemented for free: #24763

Also, I could imagine us moving in a direction eventually where devs pass a mixture of literal arguments/ amp-state arguments into the function. I skirt the issue by banning it entirely, but if we utilized the actions parser it could be easier to implement. This would become especially important if we wanted to make amp-script functions accessible from bind expressions.

Interop with bind expressions would let devs write a bound src with amp-state vars and have it automatically updated when the state variable is updated.

<amp-list [src]="amp-script:fns.fetchListData(filterArgs)">

The current workaround requires chaining a list.refresh() action after the setState action.

@samouri
Copy link
Copy Markdown
Member Author

samouri commented Jul 23, 2020

Can you add an e2e test in amp-script/test-e2e/ for this?

Do you mind if I defer that to the next PR?
It won't actually work e2e until I release a new version of worker-dom.

@samouri
Copy link
Copy Markdown
Member Author

samouri commented Jul 27, 2020

cc @ampproject/wg-caching for the validator change

Copy link
Copy Markdown
Contributor

@honeybadgerdontcare honeybadgerdontcare left a comment

Choose a reason for hiding this comment

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

It's a nit, and I've been told to add a test for this, but...alphabetical....

In file /validator-amp-list.protoascii please keep the attributes alphabetical.

@samouri
Copy link
Copy Markdown
Member Author

samouri commented Jul 28, 2020

It's a nit, and I've been told to add a test for this, but...alphabetical....

Sounds reasonable! I'll spin up a new PR to address this now.

@samouri samouri merged commit 0290a90 into ampproject:master Jul 28, 2020
@samouri samouri deleted the adapters branch July 28, 2020 18:27
twifkak pushed a commit to twifkak/amphtml that referenced this pull request Aug 5, 2020
@twifkak twifkak mentioned this pull request Aug 5, 2020
twifkak added a commit that referenced this pull request Aug 5, 2020
* cl/323700659 Revision bump for #29329
* cl/323797874 Revision bump for #29542
* cl/323858009 Revision bump for #29409
* cl/323889989 n/a
* cl/324641097 Implements document level byte count limitation.
* cl/324911894 Remove AMP Actions from Validator
* cl/324915915 Revision bump for #29642

Co-authored-by: Greg Grothaus <greggrothaus@google.com>
Co-authored-by: Amaltas Bohra <amaltas@google.com>
Co-authored-by: honeybadgerdontcare <sedano@google.com>
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* Implement protocol adapters

* clean a bit

* lint fixes

* typefix

* make example a promise

* add amp-list tests

* amp-script tests

* goodbye nested ternaries you've served me well

* round1 caro fixes

* helpful message for devs

* add unit test for single-item

* fixed incorrect header

* rebase to fix missing import

* choumx feedback updates

* make the tests pass + listen to Caros wisdom

* lint fixes

* rely on amp-script id, as well as getImpl throwing if extension absent

* remove unused import
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* cl/323700659 Revision bump for ampproject#29329
* cl/323797874 Revision bump for ampproject#29542
* cl/323858009 Revision bump for ampproject#29409
* cl/323889989 n/a
* cl/324641097 Implements document level byte count limitation.
* cl/324911894 Remove AMP Actions from Validator
* cl/324915915 Revision bump for ampproject#29642

Co-authored-by: Greg Grothaus <greggrothaus@google.com>
Co-authored-by: Amaltas Bohra <amaltas@google.com>
Co-authored-by: honeybadgerdontcare <sedano@google.com>
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.

5 participants