Skip to content

Commit

Permalink
fix(useQuery): fix useQuery run method return value when throttle
Browse files Browse the repository at this point in the history
… and `debounce` are not set
  • Loading branch information
vikiboss committed Sep 9, 2024
1 parent 362e06f commit 2f25b3e
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/react-use/src/use-query/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ export function useQuery<T extends AnyFunc, D = Awaited<ReturnType<T>>, E = any>
): UseQueryReturns<T, D, E> {
const [cache, cacheActions] = useQueryCache<T, D>(options)

const latest = useLatest({ fetcher, cache, ...options })
const debounceOptions = isNumber(options.debounce) ? { wait: options.debounce } : options.debounce
const throttleOptions = isNumber(options.throttle) ? { wait: options.throttle } : options.throttle

const enableRateControl = Boolean(debounceOptions || throttleOptions)

const latest = useLatest({ fetcher, cache, enableRateControl, ...options })

const service = useLoadingSlowFn<T, D, E>(
useRetryFn<T, E>(
((...args) => {
Expand Down Expand Up @@ -297,17 +300,17 @@ export function useQuery<T extends AnyFunc, D = Awaited<ReturnType<T>>, E = any>
return service.mutate(nextData, nextParams)
})

const refreshWithCacheAndRateControl = useStableFn(async (params?: Parameters<T> | []) => {
const refreshWithCache = useStableFn(async (params?: Parameters<T> | []) => {
const outerParams = cacheActions.isCacheEnabled ? latest.current.cache.params : service.params
const actualParams = params ?? (outerParams || [])
return refreshWithRateControl(actualParams)
return latest.current.enableRateControl ? refreshWithRateControl(actualParams) : service.refresh(actualParams)
})

return {
...pausable,
mutate: mutateWithCache,
refresh: refreshWithCacheAndRateControl,
run: serviceWithRateControl,
refresh: refreshWithCache,
run: enableRateControl ? serviceWithRateControl : service.run,
cancel: service.cancel,
get params() {
return cacheActions.isCacheEnabled ? cache.params : service.params
Expand Down

0 comments on commit 2f25b3e

Please sign in to comment.