Skip to content

Conversation

bramanudom
Copy link
Contributor

#20083

This PR adds additional logic make preloading consider next pages that are reachable within stories through goToPage action, as well as previous pages that were reached through either advance-to or goToPage.

For next pages we search through the page element to find the next pages. For previous pages, we make use of the story level stack (#20182).

* or [] if there aren't any.
*/
getNextPagesByAction() {
const actionElements =
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the name makes it seem like the type of this would be {!Array<!Element>}, where it's really !{Array<string>} (where each string is an attribute). Maybe name it actionAttrs? Or just actions?

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

el => el.getAttribute('on'));

return actionElements.reduce((res, el) => {
if (el.includes('goToPage')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Same as above; the name el makes it seem like this is an {!Element}, when it's really a {string}. Maybe action?

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


/**
* Finds any elements in the page that has a goToPage action.
* @return {Array<string>} The IDs of the potential next pages in the story
Copy link
Contributor

Choose a reason for hiding this comment

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

Type should be non-nullable ({!Array<string>})

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

}
// Do not overwrite, branching distance always takes precedence.
if (pageId !== maybePrev) {
pagesByDistance[distance].push(pageId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be combined with the statement below?

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 think we'd still like to go through the step of setting the distance attribute when the branching experiment is turned off!

return actionElements.reduce((res, el) => {
if (el.includes('goToPage')) {
// The pageId is in between the equals sign & closing parenthesis.
res.push(el.slice(el.search('\=(.*)') + 1,-1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this still work if the element has two actions with parameters? e.g.

on="tap:foo.barAction(baz=1), story.goToPage(id=page-id)"

Copy link
Contributor Author

@bramanudom bramanudom Jan 31, 2019

Choose a reason for hiding this comment

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

Thank you for pointing this out! You're right, this won't work for that case. It also won't work for handling multiple events on="tap:story.goToPage(id=pageID);submit-error:lightbox2".

Made some changes to account for this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should also add unit tests for those cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I just added some tests to test out the method I wrote.

*/
actions() {
const actionElements =
Array.prototype.slice.call(this.element.querySelectorAll('[on]'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: You could only query the actions that contain goToPage by doing something like querySelectorAll('[on*=goToPage]') (documentation).
Not a big deal but these selectors are pretty powerful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for linking the documentation also, very good to know. Went ahead and did the change. I believe the inside check in the reduce for 'goToPage' still needs to be there to account for the cases outlined above (multiple events/actions).

* @return {!Array<string>} The IDs of the potential next pages in the story
* or null if there aren't any.
*/
actions() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should be private

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.


story.element.querySelector('#cover').appendChild(actionButton);

const distanceGraph = story.getPagesByDistance_();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can we set the goToPage action in the page before the buildCallback, so we don't have to manually call this private method?

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 had some issues retrieving the distance attribute from the pages for some reason (it doesn't seem to exist during the tests on the page elements during the tests, though it's definitely being set). If it's okay with you, I think we should keep the as they are.

@newmuis
Copy link
Contributor

newmuis commented Feb 8, 2019

@bramanudom is this ready to be merged?

@bramanudom
Copy link
Contributor Author

@newmuis Yes! Thanks, Jon.

@newmuis newmuis merged commit 4b1dbfb into ampproject:master Feb 8, 2019
nbeloglazov pushed a commit to nbeloglazov/amphtml that referenced this pull request Feb 12, 2019
* implemented changes still need to fix tests

* fixed tests

* removed console.logs

* Add comment

* addressed PR comments

* fixed lint issues

* Oops

* revert changes

* added more tests, reverted accidental change in validator.js

* removed excess space that linter didnt catch

* addressed nits
noranazmy pushed a commit to noranazmy/amphtml that referenced this pull request Mar 22, 2019
* implemented changes still need to fix tests

* fixed tests

* removed console.logs

* Add comment

* addressed PR comments

* fixed lint issues

* Oops

* revert changes

* added more tests, reverted accidental change in validator.js

* removed excess space that linter didnt catch

* addressed nits
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.

5 participants