Skip to content

Commit 83feb7a

Browse files
Waleed-KHantfu
andauthored
fix(useDevicePixelRatio): remove unnecessary cleanup (#2325)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent e6c5166 commit 83feb7a

File tree

1 file changed

+14
-24
lines changed
  • packages/core/useDevicePixelRatio

1 file changed

+14
-24
lines changed

packages/core/useDevicePixelRatio/index.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref } from 'vue-demi'
2-
import { type Fn, tryOnScopeDispose } from '@vueuse/shared'
2+
import { tryOnScopeDispose } from '@vueuse/shared'
33
import { type ConfigurableWindow, defaultWindow } from '../_configurable'
44

55
/**
@@ -11,34 +11,24 @@ import { type ConfigurableWindow, defaultWindow } from '../_configurable'
1111
export function useDevicePixelRatio({
1212
window = defaultWindow,
1313
}: ConfigurableWindow = {}) {
14-
if (!window) {
15-
return {
16-
pixelRatio: ref(1),
17-
}
18-
}
19-
2014
const pixelRatio = ref(1)
2115

22-
const cleanups: Fn[] = []
23-
24-
const cleanup = () => {
25-
cleanups.map(i => i())
26-
cleanups.length = 0
27-
}
16+
if (window) {
17+
let media: MediaQueryList
18+
function observe() {
19+
pixelRatio.value = window!.devicePixelRatio
20+
cleanup()
21+
media = window!.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
22+
media.addEventListener('change', observe, { once: true })
23+
}
24+
function cleanup() {
25+
media?.removeEventListener('change', observe)
26+
}
2827

29-
const observe = () => {
30-
pixelRatio.value = window.devicePixelRatio
31-
cleanup()
32-
const media = window.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
33-
media.addEventListener('change', observe, { once: true })
34-
cleanups.push(() => {
35-
media.removeEventListener('change', observe)
36-
})
28+
observe()
29+
tryOnScopeDispose(cleanup)
3730
}
3831

39-
observe()
40-
tryOnScopeDispose(cleanup)
41-
4232
return { pixelRatio }
4333
}
4434

0 commit comments

Comments
 (0)