Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ describe('useDataLoader', () => {
expect(result.current.isPolling).toBe(true)
expect(result.current.isLoading).toBe(true)
expect(result.current.isSuccess).toBe(false)
expect(method2).toBeCalledTimes(1)
await waitForNextUpdate()
expect(method2).toBeCalledTimes(1)
expect(result.current.isPolling).toBe(true)
expect(result.current.isLoading).toBe(false)
expect(result.current.isSuccess).toBe(true)
Expand Down
84 changes: 50 additions & 34 deletions packages/use-dataloader/src/useDataLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { useEffect, useLayoutEffect, useMemo, useReducer, useRef } from 'react'
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useReducer,
useRef,
} from 'react'
import { useDataLoaderContext } from './DataLoaderProvider'
import { ActionEnum, StatusEnum } from './constants'
import reducer from './reducer'
Expand Down Expand Up @@ -75,10 +82,6 @@ const useDataLoader = (
}, [cacheKeyPrefix, fetchKey])

const previousDataRef = useRef()
const isMountedRef = useRef(enabled)
const methodRef = useRef(method)
const onSuccessRef = useRef(onSuccess)
const onErrorRef = useRef(onError)

const isLoading = useMemo(() => status === StatusEnum.LOADING, [status])
const isIdle = useMemo(() => status === StatusEnum.IDLE, [status])
Expand All @@ -90,53 +93,62 @@ const useDataLoader = (
[isSuccess, isLoading, enabled, pollingInterval],
)

const handleRequest = useRef(async cacheKey => {
try {
dispatch(Actions.createOnLoading())
const result = await methodRef.current?.()
const handleRequest = useCallback(
async cacheKey => {
try {
dispatch(Actions.createOnLoading())
const result = await method()

if (keepPreviousData) {
previousDataRef.current = getCachedData(cacheKey)
}
if (result !== undefined && result !== null && cacheKey)
addCachedData(cacheKey, result)
if (keepPreviousData) {
previousDataRef.current = getCachedData(cacheKey)
}
if (result !== undefined && result !== null && cacheKey)
addCachedData(cacheKey, result)

dispatch(Actions.createOnSuccess())
dispatch(Actions.createOnSuccess())

await onSuccessRef.current?.(result)
} catch (err) {
dispatch(Actions.createOnError(err))
await onErrorRef.current?.(err)
}
})
await onSuccess?.(result)
} catch (err) {
dispatch(Actions.createOnError(err))
await onError?.(err)
}
},
[
addCachedData,
getCachedData,
keepPreviousData,
method,
onError,
onSuccess,
],
)

const handleRequestRef = useRef(handleRequest)

useEffect(() => {
let handler
if (isMountedRef.current) {
if (enabled) {
if (isIdle) {
handleRequest.current(key)
handleRequestRef.current(key)
}
if (pollingInterval && isSuccess) {
handler = setTimeout(() => handleRequest.current(key), pollingInterval)
if (pollingInterval && (isSuccess || isError)) {
handler = setTimeout(
() => handleRequestRef.current(key),
pollingInterval,
)
}
}

return () => {
if (handler) clearTimeout(handler)
}
// Why can't put empty array for componentDidMount, componentWillUnmount ??? No array act like componentDidMount and componentDidUpdate
}, [key, pollingInterval, isIdle, isSuccess])

useLayoutEffect(() => {
methodRef.current = method
}, [method])
}, [key, pollingInterval, isIdle, isSuccess, isError, enabled])

useLayoutEffect(() => {
isMountedRef.current = enabled
dispatch(Actions.createReset())
if (key && typeof key === 'string') {
addReloadRef.current?.(key, reloadArgs =>
handleRequest.current(key, reloadArgs),
handleRequestRef.current(key, reloadArgs),
)
}

Expand All @@ -152,6 +164,10 @@ const useDataLoader = (
addReloadRef.current = addReload
}, [clearReload, addReload])

useLayoutEffect(() => {
handleRequestRef.current = handleRequest
}, [handleRequest])

return {
data: getCachedData(key) || initialData,
error,
Expand All @@ -161,7 +177,7 @@ const useDataLoader = (
isPolling,
isSuccess,
previousData: previousDataRef.current,
reload: args => handleRequest.current(key, args),
reload: args => handleRequestRef.current(key, args),
}
}

Expand Down