Skip to content

Commit

Permalink
Merge pull request #1954 from mkholjuraev/paginated-requests
Browse files Browse the repository at this point in the history
create helper function to fetch data with concurrent requests
  • Loading branch information
mkholjuraev authored Mar 5, 2024
2 parents 2d2f273 + b38e178 commit 5f0c903
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": ">=7.0.0",
"react-router-dom": "^5.0.0 || ^6.0.0",
"cypress": ">=12.0.0 < 14.0.0"
"react-router-dom": "^5.0.0 || ^6.0.0"
},
"dependencies": {
"@redhat-cloud-services/rbac-client": "^1.0.100",
Expand All @@ -43,6 +42,7 @@
"axios": "^0.28.0",
"commander": "^2.20.3",
"mkdirp": "^1.0.4",
"p-all": "^5.0.0",
"react-content-loader": "^6.2.0"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export { useInventory } from './useInventory';
export * from './CypressUtils';
export * from './useInsightsNavigate';
export * from './useExportPDF';
export * from './usePromiseQueue';
export * from './useFetchBatched';
1 change: 1 addition & 0 deletions packages/utils/src/useFetchBatched/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './useFetchBatched';
30 changes: 30 additions & 0 deletions packages/utils/src/useFetchBatched/useFetchBatched.ts
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;
1 change: 1 addition & 0 deletions packages/utils/src/usePromiseQueue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './usePromiseQueue';
46 changes: 46 additions & 0 deletions packages/utils/src/usePromiseQueue/usePromiseQueue.ts
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;

0 comments on commit 5f0c903

Please sign in to comment.