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

feat(usePerformanceObserver): new function #2736

Merged
merged 10 commits into from
Mar 4, 2023
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export * from './useOffsetPagination'
export * from './useOnline'
export * from './usePageLeave'
export * from './useParallax'
export * from './usePerformanceObserver'
export * from './usePermission'
export * from './usePointer'
export * from './usePointerLock'
Expand Down
20 changes: 20 additions & 0 deletions packages/core/usePerformanceObserver/demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script setup lang="ts">
import { usePerformanceObserver } from '@vueuse/core'
import { ref } from 'vue'

const entrys = ref<PerformanceEntry[]>([])
usePerformanceObserver({
entryTypes: ['paint'],
}, (list) => {
entrys.value = list.getEntries()
})
const refresh = () => window.location.reload()
</script>

<template>
<button @click="refresh">
refresh
</button>

<pre lang="json">{{ entrys }}</pre>
</template>
20 changes: 20 additions & 0 deletions packages/core/usePerformanceObserver/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
category: Browser
---

# usePerformanceObserver

Observe performance metrics.

## Usage

```ts
import { usePerformanceObserver } from '@vueuse/core'

const entrys = ref<PerformanceEntry[]>([])
usePerformanceObserver({
entryTypes: ['paint'],
}, (list) => {
entrys.value = list.getEntries()
})
```
54 changes: 54 additions & 0 deletions packages/core/usePerformanceObserver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { tryOnScopeDispose } from '@vueuse/shared'
import { useSupported } from '../useSupported'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'

export type UsePerformanceObserverOptions = PerformanceObserverInit & ConfigurableWindow & {
/**
* Start the observer immediate.
*
* @default true
*/
immediate?: boolean
}

/**
* Observe performance metrics.
*
* @see https://vueuse.org/usePerformanceObserver
* @param options
*/
export function usePerformanceObserver(options: UsePerformanceObserverOptions, callback: PerformanceObserverCallback) {
const {
window = defaultWindow,
immediate = true,
...performanceOptions
} = options

const isSupported = useSupported(() => window && 'PerformanceObserver' in window)

let observer: PerformanceObserver | undefined

const stop = () => {
observer?.disconnect()
}

const start = () => {
if (isSupported.value) {
stop()
observer = new PerformanceObserver(callback)
observer.observe(performanceOptions)
}
}

tryOnScopeDispose(stop)

if (immediate)
start()

return {
isSupported,
start,
stop,
}
}