Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usePerformanceObserver): new function (#2736)
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
1 parent
4d6bc00
commit 21536a2
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |