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

fix(useDevicePixelRatio): more efficient mechanism #2044

Merged
merged 3 commits into from Sep 5, 2022

Conversation

Waleed-KH
Copy link
Contributor

Description

I implemented a more efficient mechanism for the useDevicePixelRatio function that inspired by this example

Additional context

Currently the useDevicePixelRatio function create 12*2 watchers/eventListeners (+1 window resize) to achieve the same goal, this mechanism use only 1 event listener to check if the current resolution changed (once) then update the value and recursively check the new value


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

wheatjs
wheatjs previously approved these changes Aug 5, 2022
Comment on lines 23 to 30
tryOnScopeDispose((function MqResolutionObserve(): Fn {
pixelRatio.value = window.devicePixelRatio
}

useEventListener(window, 'resize', handleDevicePixelRatio, { passive: true })

DEVICE_PIXEL_RATIO_SCALES.forEach((dppx) => {
// listen mql events in both sides
const mqlMin = useMediaQuery(`screen and (min-resolution: ${dppx}dppx)`)
const mqlMax = useMediaQuery(`screen and (max-resolution: ${dppx}dppx)`)

watch([mqlMin, mqlMax], handleDevicePixelRatio)
})
mqResolution = window.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
mqResolution.addEventListener('change', MqResolutionObserve, { once: true })
return () => {
mqResolution.removeEventListener('change', MqResolutionObserve)
}
})())
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for wraping the function like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a short way to immediately invoke the function and passing removeEventListener to tryOnScopeDispose

@antfu antfu enabled auto-merge (squash) September 5, 2022 13:56
@antfu antfu merged commit eb05403 into vueuse:main Sep 5, 2022
@Waleed-KH
Copy link
Contributor Author

Hi @antfu
can you please explain the changes you did to the code , like what is the need for an array of cleanups?

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.

None yet

3 participants