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
5 changes: 5 additions & 0 deletions .changeset/spotty-words-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/use-dataloader": patch
---

Fix bug on infinity data loader
2 changes: 1 addition & 1 deletion packages/fuzzy-search/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest'
import { isFuzzyMatch, levenshteinDistance, normalizeString } from ".."
import { isFuzzyMatch, levenshteinDistance, normalizeString } from '..'

describe('fuzzySearch', () => {
describe('normalizeString', () => {
Expand Down
31 changes: 14 additions & 17 deletions packages/use-dataloader/src/useInfiniteDataLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ export const useInfiniteDataLoader = <
getNextPage ? getNextPage(...params) : undefined,
)

const paramsRef = useRef({
const paramsArgs = {
...baseParams,
[pageParamKey]: page,
})

const getMethodRef = useRef(() => method(paramsRef.current))
}

const getMethodRef = useRef(() => method(paramsArgs))
const getOnSuccessRef = useRef(
(...params: Parameters<NonNullable<typeof onSuccess>>) =>
onSuccess?.(...params),
Expand Down Expand Up @@ -99,23 +98,28 @@ export const useInfiniteDataLoader = <
'infinite',
page as string | number,
])

// Clean bad requests in the array
requestRefs.current = requestRefs.current.filter(request => {
if (request.key.startsWith(computeKey(baseQueryKey))) {
return true
}

request.removeObserver(forceRerender.current)

return false
})

const requestInRef = requestRefs.current.find(request =>
request.key.endsWith(currentQueryKey),
)

if (!requestInRef) {
const request = getOrAddRequest<ResultType, ErrorType>(currentQueryKey, {
enabled,
method: getMethodRef.current,
})

if (!request.observers.includes(forceRerender.current)) {
request.addObserver(forceRerender.current)
}
Expand Down Expand Up @@ -176,16 +180,13 @@ export const useInfiniteDataLoader = <

const loadMoreRef = useRef(() => {
if (nextPageRef.current) {
paramsRef.current = {
...baseParams,
[pageParamKey]: nextPageRef.current,
}
setPage(curr => nextPageRef.current ?? curr)
}
})

useEffect(() => {
request.method = () => method(paramsRef.current)
request.method = () => method(paramsArgs)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [method, request])

useEffect(() => {
Expand All @@ -210,28 +211,24 @@ export const useInfiniteDataLoader = <
.then(async result => {
nextPageRef.current = getNextPageFnRef.current(
result,
paramsRef.current,
paramsArgs,
) as typeof page
await onSuccessLoad(result)
})
.catch(onFailedLoad)
}
optimisticIsLoadingRef.current = false
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [needLoad, request])

useEffect(() => {
paramsRef.current = {
...baseParams,
[pageParamKey]: page,
}
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [baseParams, pageParamKey])
useEffect(() => {
getOnSuccessRef.current = (...params) => onSuccess?.(...params)
}, [onSuccess])

useEffect(() => {
getOnErrorRef.current = err => onError?.(err) ?? onGlobalError?.(err)
}, [onError, onGlobalError])

useEffect(() => {
getNextPageFnRef.current = (...params) =>
getNextPage ? getNextPage(...params) : undefined
Expand Down
Loading