Skip to content

Commit

Permalink
feat(usePerformanceObserver): new function (#2736)
Browse files Browse the repository at this point in the history
Co-authored-by: Admin <Admin@PF25TD71-PHW.inc.bytedance.com>
Co-authored-by: Jelf <okxiaoliang4@gmail.com>
Co-authored-by: Jelf <353742991@qq.com>
  • Loading branch information
4 people committed Mar 4, 2023
1 parent 4d6bc00 commit 21536a2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/index.ts
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
@@ -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
@@ -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
@@ -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,
}
}

0 comments on commit 21536a2

Please sign in to comment.