Skip to content

Commit c6c6ede

Browse files
authored
feat: use useEventListener where it was not being used (#4479)
1 parent d035035 commit c6c6ede

File tree

11 files changed

+47
-66
lines changed

11 files changed

+47
-66
lines changed

packages/core/onClickOutside/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
5151
// How it works: https://stackoverflow.com/a/39712411
5252
if (isIOS && !_iOSWorkaround) {
5353
_iOSWorkaround = true
54+
const listenerOptions = { passive: true }
5455
Array.from(window.document.body.children)
55-
.forEach(el => el.addEventListener('click', noop))
56-
window.document.documentElement.addEventListener('click', noop)
56+
.forEach(el => useEventListener(el, 'click', noop, listenerOptions))
57+
useEventListener(window.document.documentElement, 'click', noop, listenerOptions)
5758
}
5859

5960
let shouldListen = true

packages/core/useBluetooth/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ This sample illustrates the use of the Web Bluetooth API to read battery level a
4747
Here, we use the characteristicvaluechanged event listener to handle reading battery level characteristic value. This event listener will optionally handle upcoming notifications as well.
4848

4949
```ts
50-
import { pausableWatch, useBluetooth } from '@vueuse/core'
50+
import { pausableWatch, useBluetooth, useEventListener } from '@vueuse/core'
5151

5252
const {
5353
isSupported,
@@ -78,9 +78,9 @@ async function getBatteryLevels() {
7878
)
7979

8080
// Listen to when characteristic value changes on `characteristicvaluechanged` event:
81-
batteryLevelCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
81+
useEventListener(batteryLevelCharacteristic, 'characteristicvaluechanged', (event) => {
8282
batteryPercent.value = event.target.value.getUint8(0)
83-
})
83+
}, { passive: true })
8484

8585
// Convert received buffer to number:
8686
const batteryLevel = await batteryLevelCharacteristic.readValue()

packages/core/useBluetooth/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { tryOnMounted, tryOnScopeDispose } from '@vueuse/shared'
44
import { readonly, shallowRef, watch } from 'vue'
55

66
import { defaultNavigator } from '../_configurable'
7+
import { useEventListener } from '../useEventListener'
78
import { useSupported } from '../useSupported'
89

910
export interface UseBluetoothRequestDeviceOptions {
@@ -102,7 +103,7 @@ export function useBluetooth(options?: UseBluetoothOptions): UseBluetoothReturn
102103

103104
if (device.value && device.value.gatt) {
104105
// Add reset fn to gattserverdisconnected event:
105-
device.value.addEventListener('gattserverdisconnected', reset)
106+
useEventListener(device, 'gattserverdisconnected', reset, { passive: true })
106107

107108
try {
108109
// Connect to the device:

packages/core/useBroadcastChannel/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ConfigurableWindow } from '../_configurable'
33
import { tryOnMounted, tryOnScopeDispose } from '@vueuse/shared'
44
import { ref, shallowRef } from 'vue'
55
import { defaultWindow } from '../_configurable'
6+
import { useEventListener } from '../useEventListener'
67
import { useSupported } from '../useSupported'
78

89
export interface UseBroadcastChannelOptions extends ConfigurableWindow {
@@ -49,17 +50,21 @@ export function useBroadcastChannel<D, P>(options: UseBroadcastChannelOptions):
4950
error.value = null
5051
channel.value = new BroadcastChannel(name)
5152

52-
channel.value.addEventListener('message', (e: MessageEvent) => {
53+
const listenerOptions = {
54+
passive: true,
55+
}
56+
57+
useEventListener(channel, 'message', (e: MessageEvent) => {
5358
data.value = e.data
54-
}, { passive: true })
59+
}, listenerOptions)
5560

56-
channel.value.addEventListener('messageerror', (e: MessageEvent) => {
61+
useEventListener(channel, 'messageerror', (e: MessageEvent) => {
5762
error.value = e
58-
}, { passive: true })
63+
}, listenerOptions)
5964

60-
channel.value.addEventListener('close', () => {
65+
useEventListener(channel, 'close', () => {
6166
isClosed.value = true
62-
})
67+
}, listenerOptions)
6368
})
6469
}
6570

packages/core/useDisplayMedia/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Ref } from 'vue'
33
import type { ConfigurableNavigator } from '../_configurable'
44
import { ref, shallowRef, watch } from 'vue'
55
import { defaultNavigator } from '../_configurable'
6+
import { useEventListener } from '../useEventListener'
67
import { useSupported } from '../useSupported'
78

89
export interface UseDisplayMediaOptions extends ConfigurableNavigator {
@@ -43,7 +44,7 @@ export function useDisplayMedia(options: UseDisplayMediaOptions = {}) {
4344
if (!isSupported.value || stream.value)
4445
return
4546
stream.value = await navigator!.mediaDevices.getDisplayMedia(constraint)
46-
stream.value?.getTracks().forEach(t => t.addEventListener('ended', stop))
47+
stream.value?.getTracks().forEach(t => useEventListener(t, 'ended', stop, { passive: true }))
4748
return stream.value
4849
}
4950

packages/core/useMediaControls/index.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ export function useMediaControls(target: MaybeRef<HTMLMediaElement | null | unde
159159
document = defaultDocument,
160160
} = options
161161

162+
const listenerOptions = { passive: true }
163+
162164
const currentTime = ref(0)
163165
const duration = ref(0)
164166
const seeking = ref(false)
@@ -265,7 +267,6 @@ export function useMediaControls(target: MaybeRef<HTMLMediaElement | null | unde
265267

266268
// Clear the sources
267269
el.querySelectorAll('source').forEach((e) => {
268-
e.removeEventListener('error', sourceErrorEvent.trigger)
269270
e.remove()
270271
})
271272

@@ -277,7 +278,7 @@ export function useMediaControls(target: MaybeRef<HTMLMediaElement | null | unde
277278
source.setAttribute('type', type || '')
278279
source.setAttribute('media', media || '')
279280

280-
source.addEventListener('error', sourceErrorEvent.trigger)
281+
useEventListener(source, 'error', sourceErrorEvent.trigger, listenerOptions)
281282

282283
el.appendChild(source)
283284
})
@@ -286,15 +287,6 @@ export function useMediaControls(target: MaybeRef<HTMLMediaElement | null | unde
286287
el.load()
287288
})
288289

289-
// Remove source error listeners
290-
tryOnScopeDispose(() => {
291-
const el = toValue(target)
292-
if (!el)
293-
return
294-
295-
el.querySelectorAll('source').forEach(e => e.removeEventListener('error', sourceErrorEvent.trigger))
296-
})
297-
298290
/**
299291
* Apply composable state to the element, also when element is changed
300292
*/
@@ -394,8 +386,6 @@ export function useMediaControls(target: MaybeRef<HTMLMediaElement | null | unde
394386
}
395387
})
396388

397-
const listenerOptions = { passive: true }
398-
399389
useEventListener(
400390
target,
401391
'timeupdate',

packages/core/useMediaQuery/index.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import type { MaybeRefOrGetter } from '@vueuse/shared'
44
import type { ConfigurableWindow } from '../_configurable'
5-
import { pxValue, tryOnScopeDispose } from '@vueuse/shared'
6-
import { computed, ref, toValue, watchEffect } from 'vue'
5+
import { pxValue } from '@vueuse/shared'
6+
import { computed, ref, shallowRef, toValue, watchEffect } from 'vue'
77
import { defaultWindow } from '../_configurable'
8+
import { useEventListener } from '../useEventListener'
89
import { useSSRWidth } from '../useSSRWidth'
910
import { useSupported } from '../useSupported'
1011

@@ -21,24 +22,14 @@ export function useMediaQuery(query: MaybeRefOrGetter<string>, options: Configur
2122

2223
const ssrSupport = ref(typeof ssrWidth === 'number')
2324

24-
let mediaQuery: MediaQueryList | undefined
25+
const mediaQuery = shallowRef<MediaQueryList>()
2526
const matches = ref(false)
2627

2728
const handler = (event: MediaQueryListEvent) => {
2829
matches.value = event.matches
2930
}
3031

31-
const cleanup = () => {
32-
if (!mediaQuery)
33-
return
34-
if ('removeEventListener' in mediaQuery)
35-
mediaQuery.removeEventListener('change', handler)
36-
else
37-
// @ts-expect-error deprecated API
38-
mediaQuery.removeListener(handler)
39-
}
40-
41-
const stopWatch = watchEffect(() => {
32+
watchEffect(() => {
4233
if (ssrSupport.value) {
4334
// Exit SSR support on mounted if window available
4435
ssrSupport.value = !isSupported.value
@@ -62,24 +53,11 @@ export function useMediaQuery(query: MaybeRefOrGetter<string>, options: Configur
6253
if (!isSupported.value)
6354
return
6455

65-
cleanup()
66-
67-
mediaQuery = window!.matchMedia(toValue(query))
68-
69-
if ('addEventListener' in mediaQuery)
70-
mediaQuery.addEventListener('change', handler)
71-
else
72-
// @ts-expect-error deprecated API
73-
mediaQuery.addListener(handler)
74-
75-
matches.value = mediaQuery.matches
56+
mediaQuery.value = window!.matchMedia(toValue(query))
57+
matches.value = mediaQuery.value.matches
7658
})
7759

78-
tryOnScopeDispose(() => {
79-
stopWatch()
80-
cleanup()
81-
mediaQuery = undefined
82-
})
60+
useEventListener(mediaQuery, 'change', handler, { passive: true })
8361

8462
return computed(() => matches.value)
8563
}

packages/core/useScriptTag/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ConfigurableDocument } from '../_configurable'
33
import { noop, tryOnMounted, tryOnUnmounted } from '@vueuse/shared'
44
import { ref, toValue } from 'vue'
55
import { defaultDocument } from '../_configurable'
6+
import { useEventListener } from '../useEventListener'
67

78
export interface UseScriptTagOptions extends ConfigurableDocument {
89
/**
@@ -128,14 +129,17 @@ export function useScriptTag(
128129
}
129130

130131
// Event listeners
131-
el.addEventListener('error', event => reject(event))
132-
el.addEventListener('abort', event => reject(event))
133-
el.addEventListener('load', () => {
132+
const listenerOptions = {
133+
passive: true,
134+
}
135+
useEventListener(el, 'error', event => reject(event), listenerOptions)
136+
useEventListener(el, 'abort', event => reject(event), listenerOptions)
137+
useEventListener(el, 'load', () => {
134138
el!.setAttribute('data-loaded', 'true')
135139

136140
onLoaded(el!)
137141
resolveWithElement(el!)
138-
})
142+
}, listenerOptions)
139143

140144
// Append the <script> tag to head.
141145
if (shouldAppend)

packages/guidelines.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Learn more about the implementation: [`_configurable.ts`](https://github.com/vue
5050
```ts
5151
import type { ConfigurableWindow } from '../_configurable'
5252
import { defaultWindow } from '../_configurable'
53+
import { useEventListener } from '../useEventListener'
5354

5455
export function useActiveElement<T extends HTMLElement>(
5556
options: ConfigurableWindow = {},
@@ -63,7 +64,7 @@ export function useActiveElement<T extends HTMLElement>(
6364

6465
// skip when in Node.js environment (SSR)
6566
if (window) {
66-
window.addEventListener('blur', () => {
67+
useEventListener(window, 'blur', () => {
6768
el = window?.document.activeElement
6869
}, true)
6970
}

packages/shared/useDebounceFn/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ Debounce execution of a function.
1212
## Usage
1313

1414
```js
15-
import { useDebounceFn } from '@vueuse/core'
15+
import { useDebounceFn, useEventListener } from '@vueuse/core'
1616

1717
const debouncedFn = useDebounceFn(() => {
1818
// do something
1919
}, 1000)
2020

21-
window.addEventListener('resize', debouncedFn)
21+
useEventListener(window, 'resize', debouncedFn)
2222
```
2323

2424
You can also pass a 3rd parameter to this, with a maximum wait time, similar to [lodash debounce](https://lodash.com/docs/4.17.15#debounce)
2525

2626
```js
27-
import { useDebounceFn } from '@vueuse/core'
27+
import { useDebounceFn, useEventListener } from '@vueuse/core'
2828

2929
// If no invokation after 5000ms due to repeated input,
3030
// the function will be called anyway.
3131
const debouncedFn = useDebounceFn(() => {
3232
// do something
3333
}, 1000, { maxWait: 5000 })
3434

35-
window.addEventListener('resize', debouncedFn)
35+
useEventListener(window, 'resize', debouncedFn)
3636
```
3737

3838
Optionally, you can get the return value of the function using promise operations.

0 commit comments

Comments
 (0)