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

const touchCache should be var or let, not const #33665

Open
gayeagle opened this issue May 19, 2024 · 1 comment
Open

const touchCache should be var or let, not const #33665

gayeagle opened this issue May 19, 2024 · 1 comment
Labels
Content:WebAPI Web API docs good first issue A good issue for newcomers to get started with.

Comments

@gayeagle
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Touch_events/Multi-touch_interaction

What specific section or headline is this issue about?

Section "Global State"

What information was incorrect, unhelpful, or incomplete?

The included JS sample appears as such:

const logEvents = false;

// Touch Point cache
const tpCache = [];

which conflicts with the code example as written for the Move/Pinch/Zoom handler see

  if (ev.targetTouches.length === 2 && ev.changedTouches.length === 2) {
    // Check if the two target touches are the same ones that started
    // the 2-touch
    const point1 = tpCache.findLastIndex(
      (tp) => tp.identifier === ev.targetTouches[0].identifier,
    );
    const point2 = tpCache.findLastIndex(
      (tp) => tp.identifier === ev.targetTouches[1].identifier,
    );

    if (point1 >= 0 && point2 >= 0) {
      // ... omitted for brevity ...
    } else {
      // empty tpCache
      tpCache = []; // <--- issue here
    }

What did you expect to see?

The issue is pretty simple, during some quality update to the demos and tutorials, someone updated the variable declaration from var to const without realizing that the reassignment to an empty array is not permitted.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@gayeagle gayeagle added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 19, 2024
@github-actions github-actions bot added the Content:WebAPI Web API docs label May 19, 2024
@minimalsm
Copy link
Contributor

Hey @gayeagle, nice catch 💪!

I think it would be better to clear the array without reassignment. That way, we prevent bugs where it might accidentally be reassigned elsewhere.

e.g.

  function clearTouchCache() {
    tpCache.length = 0
  }

  ...

  if (point1 >= 0 && point2 >= 0) {
    // ... 
  } else {
    clearTouchCache()
  }

Happy to open a PR if you don't want to :)

@Josh-Cena Josh-Cena added good first issue A good issue for newcomers to get started with. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs good first issue A good issue for newcomers to get started with.
Projects
None yet
Development

No branches or pull requests

3 participants