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
Conversation
Hey @gmajoulet, these files were changed:
Hey @newmuis, these files were changed:
|
183a96b
to
365b04b
Compare
*/ | ||
initializePageIds_() { | ||
const pageEls = this.element.querySelectorAll('amp-story-page'); | ||
const pageIds = Array.prototype.map.call(pageEls, el => el.id || 'default'); |
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: WDYT about 'page'
(or default-page
, or similar) as the default?
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
await story.layoutCallback(); | ||
|
||
expect(story.storeService_.get(StateProperty.PAGE_IDS)).to.deep.equal([ | ||
'cover', |
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 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
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.
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.
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.
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.
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.
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', |
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.
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.
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.
becomes