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

🐛 [amp-carousel 0.1] Fix snapping and closing race #32695

Merged
merged 6 commits into from
Feb 23, 2021

Conversation

micajuine-ho
Copy link
Contributor

@micajuine-ho micajuine-ho commented Feb 16, 2021

Closes #28050

For amp-carousel's in a lightbox image gallery, a race occurs when the user drags the slide slightly (but not enough to move to the next slide), and then clicks the image, which closes the carousel.

Dragging the slide causes the snapping feature to kick in: customSnap_() which eventually calls updateOnScroll_() => getNextSlideIndex_().

At this point, we NaN due to 0/0, since the this.slideWidth_ has been updated to be 0, b/c we're closing the carousel.

Copy link
Contributor

@caroqliu caroqliu left a comment

Choose a reason for hiding this comment

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

LGTM with a couple suggestions.

const impl = await ampSlideScroll.getImpl();
const devErrorSpy = env.sandbox.spy(dev(), 'error');

impl.showSlideAndTriggerAction_(NaN)
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be extra safe to add another unit test that also passes Infinity or similar.

extensions/amp-carousel/0.1/slidescroll.js Outdated Show resolved Hide resolved
Micajuine Ho added 2 commits February 22, 2021 09:35
@@ -686,7 +686,10 @@ export class AmpSlideScroll extends BaseSlides {
*/
showSlide_(newIndex) {
const {noOfSlides_} = this;
newIndex = dev().assertNumber(newIndex);
if (isNaN(newIndex)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

From the stack trace, this looks like we're fixing the issue in the wrong place. We're getting a NaN from getNextSlideIndex_ (called from updateOnScroll_).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch. I'll add the check there as well.

@micajuine-ho micajuine-ho changed the title 🐛 [amp-carousel 0.1] Dev error when moveSlide is NaN 🐛 [amp-carousel 0.1] Fix snapping and closing race Feb 23, 2021
@@ -521,6 +521,11 @@ export class AmpSlideScroll extends BaseSlides {
* @return {number} a number representing the next slide index.
*/
getNextSlideIndex_(currentScrollLeft) {
// Addresses race where slideWidth is 0, due to being closed
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand what it means for the carousel to be closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Within the lightbox gallery sense, you open the carousel by clicking on the lightbox image and then close it by clicking the image in the carousel.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see. In that case perhaps we can update this to hidden?

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.

🚨 Error: Attempting to access a non-existant slide %s / %s NaN 7
4 participants