Skip to content

Commit

Permalink
feat(useWebObserver): support additional supported check for specif…
Browse files Browse the repository at this point in the history
…ic observer
  • Loading branch information
vikiboss committed Aug 14, 2024
1 parent 0ef523f commit d30a906
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/use-web-observer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface UseWebObserverOptions {
* @defaultValue true
*/
immediate?: boolean
/**
* Additional `isSupported` judgement
*/
supported?: () => boolean
}

export interface UseWebObserverReturns<Observer> extends Pausable {
Expand Down Expand Up @@ -104,9 +108,12 @@ export function useWebObserver(
): UseWebObserverReturns<ReportingObserver>
export function useWebObserver(...args: any[]): any {
const [observer, target, callback, options = {}, initOptions = {}, observerOptions = {}] = args
const { immediate = true } = options

const isSupported = useSupported(() => observer in window)
const { immediate = true, supported = () => true } = options

const latest = useLatest({ target, supported, callback, initOptions, observerOptions })

const isSupported = useSupported(() => observer in window && latest.current.supported())
const observerRef = useRef<any | null>(null)

const stopObserver = () => {
Expand Down Expand Up @@ -137,8 +144,6 @@ export function useWebObserver(...args: any[]): any {
}
})

const latest = useLatest({ target, callback, initOptions, observerOptions })

useEffect(() => {
immediate && pausable.resume()
return pausable.pause
Expand Down

0 comments on commit d30a906

Please sign in to comment.