Skip to content

Commit

Permalink
feat(usePerformance): support entryTypes check
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Aug 14, 2024
1 parent d30a906 commit d9f39e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/use-performance-observer/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import { useWebObserver } from '../use-web-observer'
import { isDev } from '../utils/basic'

import type { UseWebObserverOptions, UseWebObserverReturns } from '../use-web-observer'

export interface UsePerformanceObserverOptions extends UseWebObserverOptions, PerformanceObserverInit {}
export interface UsePerformanceObserverOptions
extends UseWebObserverOptions,
Omit<PerformanceObserverInit, 'entryTypes'> {
entryTypes: string[]
}

export interface UsePerformanceObserverReturns extends UseWebObserverReturns<PerformanceObserver> {}

/**
* A React Hook that helps to use [PerformanceObserver API](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver) with ease.
*/
export function usePerformanceObserver(
callback: PerformanceObserverCallback = () => {},
options: UsePerformanceObserverOptions = {},
callback: PerformanceObserverCallback,
options: Omit<UsePerformanceObserverOptions, 'supported'>,
): UsePerformanceObserverReturns {
const { immediate = true, ...observerOptions } = options

return useWebObserver('PerformanceObserver', undefined, callback, { immediate }, undefined, observerOptions)
if (isDev) {
if (!options.entryTypes || !Array.isArray(options.entryTypes) || options.entryTypes.length === 0) {
throw new Error('usePerformanceObserver: entryTypes must be an array and not empty.')
}
}

return useWebObserver(
'PerformanceObserver',
undefined,
callback,
{
immediate,
supported() {
return options.entryTypes.every((e) => {
return PerformanceObserver.supportedEntryTypes.includes(e)
})
},
},
undefined,
observerOptions,
)
}
2 changes: 1 addition & 1 deletion src/use-request/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { HooksType, Since, Deprecated } from '@/components'

A hook that helps you manage the request status, including loading, refreshing, initializing, error, etc.

> A deprecated version of [useQuery](/reference/use-query), please use **useQuery** instead.
> A deprecated version of `useQuery`, please use [useQuery](/reference/use-query) instead.
## Demo

Expand Down

0 comments on commit d9f39e4

Please sign in to comment.