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

Deduplicates amp-story-page ids. #24719

Merged
merged 3 commits into from Sep 26, 2019
Merged

Conversation

gmajoulet
Copy link
Contributor

Deduplicates amp-story-page ids and triggers a user error, so the story doesn't fail on load.
Having duplicated IDs completely fails navigation or the progress bar.

<amp-story-page id="cover">
<amp-story-page id="cover">

becomes

<amp-story-page id="cover">
<amp-story-page id="cover__1">

@amp-owners-bot
Copy link

Hey @gmajoulet, these files were changed:

  • extensions/amp-story/1.0/amp-story.js
  • extensions/amp-story/1.0/test/test-amp-story.js

Hey @newmuis, these files were changed:

  • extensions/amp-story/1.0/amp-story.js
  • extensions/amp-story/1.0/test/test-amp-story.js

*/
initializePageIds_() {
const pageEls = this.element.querySelectorAll('amp-story-page');
const pageIds = Array.prototype.map.call(pageEls, el => el.id || 'default');
Copy link
Contributor

@newmuis newmuis Sep 25, 2019

Choose a reason for hiding this comment

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

nit: WDYT about 'page' (or default-page, or similar) as the default?

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

await story.layoutCallback();

expect(story.storeService_.get(StateProperty.PAGE_IDS)).to.deep.equal([
'cover',
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like the right behavior to me, and I trust the tests more than myself, but I don't think I get how this is getting cover/cover__1 instead of cover__1/cover__2

Copy link
Contributor

Choose a reason for hiding this comment

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

It's due to the condition set in the while ((index = pageIds.indexOf(pageIds[i], i + 1)) !== -1) { of line 531 in amp-story.js.

the index will take the value of the first duplicate it finds after the current i element. Thus, the current element remains unchanged.

Perhaps there is a way to make that loop a bit easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second parameter of indexOf gives a start index. Goal was to reduce the O(n2) complexity since indexOf will loop over the array.
I was trying to do as little updates as possible, which is why it's keeping the first id as cover and not cover__0. I thought it made more product sense (ie: keep what the publisher wrote as much as possible), but it also makes the code easier and faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I rewrote the loop to make it easier to read and O(n), thanks Enrique.

await story.layoutCallback();

expect(story.storeService_.get(StateProperty.PAGE_IDS)).to.deep.equal([
'cover',
Copy link
Contributor

Choose a reason for hiding this comment

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

It's due to the condition set in the while ((index = pageIds.indexOf(pageIds[i], i + 1)) !== -1) { of line 531 in amp-story.js.

the index will take the value of the first duplicate it finds after the current i element. Thus, the current element remains unchanged.

Perhaps there is a way to make that loop a bit easier to read.

@gmajoulet gmajoulet merged commit 0fdfccd into ampproject:master Sep 26, 2019
@gmajoulet gmajoulet deleted the dedupe_page_ids branch September 26, 2019 17:10
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.

None yet

4 participants