-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create useFetchBatched fook to fetch data with concurrent requests
- Loading branch information
1 parent
47cd91c
commit 6054ba2
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './useFetchBatched'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import usePromiseQueue from '../usePromiseQueue'; | ||
|
||
type FetchFunctionType = (filter: object | Array<string>, options?: object) => void; | ||
|
||
//hook to enable fetching a lot of data from the API in concurrent API calls | ||
const useFetchBatched = () => { | ||
const { isResolving: isLoading, resolve } = usePromiseQueue(); | ||
|
||
return { | ||
isLoading, | ||
fetchBatched: (fetchFunction: FetchFunctionType, total: number, filter: object, batchSize = 50) => { | ||
const pages = Math.ceil(total / batchSize) || 1; | ||
|
||
const results = resolve([...new Array(pages)].map((_, pageIdx) => () => fetchFunction(filter, { page: pageIdx + 1, per_page: batchSize }))); | ||
|
||
return results; | ||
}, | ||
fetchBatchedInline: (fetchFunction: FetchFunctionType, list: Array<string>, batchSize = 20) => { | ||
const pages = Math.ceil(list.length / batchSize) || 1; | ||
|
||
const results = resolve( | ||
[...new Array(pages)].map((_, pageIdx) => () => fetchFunction(list.slice(batchSize * pageIdx, batchSize * (pageIdx + 1)))) | ||
); | ||
|
||
return results; | ||
}, | ||
}; | ||
}; | ||
|
||
export default useFetchBatched; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './usePromiseQueue'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useCallback, useState } from 'react'; | ||
import pAll from 'p-all'; | ||
|
||
const DEFAULT_CONCURRENT_PROMISES = 2; | ||
|
||
type PromiseQueueStateType = { | ||
isResolving: boolean; | ||
//undeterministic result type by p-all package | ||
promiseResults: any; | ||
}; | ||
|
||
// hook that provides queued promises with state | ||
const usePromiseQueue = (concurrency = DEFAULT_CONCURRENT_PROMISES) => { | ||
const defaultState: PromiseQueueStateType = { | ||
isResolving: false, | ||
promiseResults: undefined, | ||
}; | ||
const [results, setResults] = useState(defaultState); | ||
|
||
const resolve = useCallback( | ||
async (fns: any) => { | ||
setResults((state) => ({ | ||
...state, | ||
isResolving: true, | ||
})); | ||
const results: any = await pAll(fns, { | ||
concurrency, | ||
}); | ||
setResults({ | ||
isResolving: false, | ||
promiseResults: results, | ||
}); | ||
|
||
return results; | ||
}, | ||
[concurrency] | ||
); | ||
|
||
return { | ||
isResolving: results.isResolving, | ||
results: results.promiseResults, | ||
resolve, | ||
}; | ||
}; | ||
|
||
export default usePromiseQueue; |