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

Ensure one-time event listeners only run once #538

Merged
merged 4 commits into from
Oct 3, 2024
Merged

Ensure one-time event listeners only run once #538

merged 4 commits into from
Oct 3, 2024

Conversation

philipwalton
Copy link
Member

Fixes #534.

This PR removes the onHidden() utility in favor of manually using visibilitychange listeners, which gives more control over adding listeners that just need to run once.

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

Approving, but as discussed I would suggest making it clearer that LCP's visibilitychange change event is handled right away (either with a comment, renaming whenIdle to explain when it does better, or by moving it back to it's own, separate call).

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

Had another look with fresh eyes and think it looks good, but one comment looks dated.

src/attribution/onINP.ts Outdated Show resolved Hide resolved
@philipwalton philipwalton merged commit c828304 into v5 Oct 3, 2024
6 checks passed
@philipwalton philipwalton deleted the once branch October 3, 2024 16:28
handle = rIC(cb);
onHidden(cb);
rIC(cb);
document.addEventListener('visibilitychange', cb, {once: true});

Choose a reason for hiding this comment

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

【discuss】why the cb should be called two times?

  • when it's hidden:call it once,immediately
  • when it's visible:call it once with rIC and another time when change to hidden

before the change,it‘s wrapped with runOnce will only call once

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this! I've created to #541 to address this problem.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Very nice catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants