Skip to content

Commit

Permalink
fix(useDevicePixelRatio): remove unnecessary cleanup (#2325)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
Waleed-KH and antfu committed Feb 18, 2023
1 parent e6c5166 commit 83feb7a
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions packages/core/useDevicePixelRatio/index.ts
@@ -1,5 +1,5 @@
import { ref } from 'vue-demi'
import { type Fn, tryOnScopeDispose } from '@vueuse/shared'
import { tryOnScopeDispose } from '@vueuse/shared'
import { type ConfigurableWindow, defaultWindow } from '../_configurable'

/**
Expand All @@ -11,34 +11,24 @@ import { type ConfigurableWindow, defaultWindow } from '../_configurable'
export function useDevicePixelRatio({
window = defaultWindow,
}: ConfigurableWindow = {}) {
if (!window) {
return {
pixelRatio: ref(1),
}
}

const pixelRatio = ref(1)

const cleanups: Fn[] = []

const cleanup = () => {
cleanups.map(i => i())
cleanups.length = 0
}
if (window) {
let media: MediaQueryList
function observe() {
pixelRatio.value = window!.devicePixelRatio
cleanup()
media = window!.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
media.addEventListener('change', observe, { once: true })
}
function cleanup() {
media?.removeEventListener('change', observe)
}

const observe = () => {
pixelRatio.value = window.devicePixelRatio
cleanup()
const media = window.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
media.addEventListener('change', observe, { once: true })
cleanups.push(() => {
media.removeEventListener('change', observe)
})
observe()
tryOnScopeDispose(cleanup)
}

observe()
tryOnScopeDispose(cleanup)

return { pixelRatio }
}

Expand Down

0 comments on commit 83feb7a

Please sign in to comment.