1
1
import { ref } from 'vue-demi'
2
- import { type Fn , tryOnScopeDispose } from '@vueuse/shared'
2
+ import { tryOnScopeDispose } from '@vueuse/shared'
3
3
import { type ConfigurableWindow , defaultWindow } from '../_configurable'
4
4
5
5
/**
@@ -11,34 +11,24 @@ import { type ConfigurableWindow, defaultWindow } from '../_configurable'
11
11
export function useDevicePixelRatio ( {
12
12
window = defaultWindow ,
13
13
} : ConfigurableWindow = { } ) {
14
- if ( ! window ) {
15
- return {
16
- pixelRatio : ref ( 1 ) ,
17
- }
18
- }
19
-
20
14
const pixelRatio = ref ( 1 )
21
15
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
+ }
28
27
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 )
37
30
}
38
31
39
- observe ( )
40
- tryOnScopeDispose ( cleanup )
41
-
42
32
return { pixelRatio }
43
33
}
44
34
0 commit comments