Skip to content

Commit

Permalink
Simplify map-reduce to some and every in useLoadData
Browse files Browse the repository at this point in the history
  • Loading branch information
baker-ling committed Jan 10, 2025
1 parent aeb827c commit 3684623
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions hooks/useLoadData/useLoadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ function correctOptionalDependencies<Deps extends any[]>(args?: readonly [...Dep
}

function checkArgsAreLoaded<Deps extends any[]>(args?: readonly [...Deps]) {
return (args || [])
.map((arg: unknown) => {
if (isApiResponseBase(arg)) {
return !(arg.isInProgress || arg.isError);
}
return true;
})
.reduce((prev, curr) => prev && curr, true);
return (args || []).every((arg: unknown) => {
if (isApiResponseBase(arg)) {
return !(arg.isInProgress || arg.isError);
}
return true;
});
}

function normalizeArgumentOverloads<T extends NotUndefined, Deps extends any[]>(
Expand Down Expand Up @@ -283,14 +281,12 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
const correctedArgs = correctOptionalDependencies(fetchDataArgs);
const argsAreLoaded = checkArgsAreLoaded(correctedArgs);

const argsHaveErrors = (correctedArgs || [])
.map((arg: unknown) => {
if (isApiResponseBase(arg)) {
return arg.isError;
}
return false;
})
.reduce((prev, curr) => prev || curr, false);
const argsHaveErrors = (correctedArgs || []).some((arg: unknown) => {
if (isApiResponseBase(arg)) {
return arg.isError;
}
return false;
});

if (argsHaveErrors) {
setPendingData({
Expand Down

0 comments on commit 3684623

Please sign in to comment.