Skip to content

Simplify/optimize map-reduce in useLoadData #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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
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
Loading