Skip to content
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

Features/attempts fn #187

Merged
merged 2 commits into from
Jun 29, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-react",
"version": "3.5.4",
"version": "3.5.6",
"description": "React hooks for data fetching",
"main": "dist/index.js",
"scripts": {
Expand Down
61 changes: 51 additions & 10 deletions src/hooks/use-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fetcherDefaults,
hasErrors,
isPending,
gettingAttempts,
lastResponses,
previousConfig,
previousProps,
Expand Down Expand Up @@ -126,7 +127,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
onSubmit,
onAbort,
refresh = isDefined(ctx.refresh) ? ctx.refresh : 0,
attempts = ctx.attempts,
attempts: $attempts = ctx.attempts,
attemptInterval = ctx.attemptInterval,
revalidateOnFocus = ctx.revalidateOnFocus,
suspense: $suspense,
Expand Down Expand Up @@ -205,6 +206,10 @@ export function useFetch<FetchDataType = any, BodyType = any>(

const resolvedKey = serialize({ idString })

if (!statusCodes.has(resolvedKey)) {
statusCodes.set(resolvedKey, null)
}

const resolvedDataKey = serialize({ idString, reqQuery, reqParams })

const ageKey = ['max-age', resolvedDataKey].join('-')
Expand Down Expand Up @@ -645,6 +650,8 @@ export function useFetch<FetchDataType = any, BodyType = any>(
: incoming

if (code >= 200 && code < 400) {
gettingAttempts.set(resolvedKey, true)

hasData.set(resolvedDataKey, true)
hasData.set(resolvedKey, true)

Expand Down Expand Up @@ -859,6 +866,9 @@ export function useFetch<FetchDataType = any, BodyType = any>(
}
}
} finally {
if (rpc.error) {
gettingAttempts.set(resolvedKey, false)
}
temporaryFormData.delete(resolvedKey)
runningRequests.set(resolvedKey, false)
suspenseInitialized.set(resolvedKey, true)
Expand Down Expand Up @@ -1156,11 +1166,33 @@ export function useFetch<FetchDataType = any, BodyType = any>(
}
}, [requestCallId, resolvedKey, revalidateOnMount, suspense])

if (!gettingAttempts.has(resolvedKey)) {
gettingAttempts.set(resolvedKey, false)
}

useEffect(() => {
// Attempts will be made after a request fails
if ((attempts as number) > 0) {
const tm = setTimeout(() => {
if (error) {

// if ((attempts as number) > 0) {
const tm = setTimeout(() => {
if (!gettingAttempts.get(resolvedKey)) {
gettingAttempts.set(resolvedKey, true)
const attempts =
typeof $attempts === 'function'
? $attempts({
status:
statusCodes.get(resolvedKey) ||
statusCodes.get(resolvedDataKey),
res: lastResponses.get(resolvedKey),
error:
hasErrors.get(resolvedKey) ||
hasErrors.get(resolvedDataKey) ||
(error as any),
completedAttempts
})
: $attempts

if ((attempts as number) > 0) {
if (completedAttempts < (attempts as number)) {
reValidate()
setCompletedAttempts((previousAttempts: number) => {
Expand All @@ -1182,14 +1214,23 @@ export function useFetch<FetchDataType = any, BodyType = any>(
if (inDeps('online')) setOnline(false)
}
}
}, getMiliseconds(attemptInterval as TimeSpan))

return () => {
clearTimeout(tm)
}
}, getMiliseconds(attemptInterval as TimeSpan))
// }
return () => {
clearTimeout(tm)
}
return () => {}
}, [error, attempts, rawJSON, attemptInterval, completedAttempts])
}, [
error,
$attempts,
reValidate,
// loading,
// rawJSON,
fetchState,
attemptInterval,
resolvedKey,
completedAttempts
])

useEffect(() => {
const refreshAmount = getMiliseconds(refresh as TimeSpan)
Expand Down
5 changes: 5 additions & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export const maxPaginationAges = new Map()
*/
export const hasErrors = new Map()

/**
* Keep track of initialized attempts
*/
export const gettingAttempts = new Map()

/**
* Suspense calls that resolved
*/
Expand Down
9 changes: 8 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,14 @@ export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
* The ammount of attempts if request fails
* @default 1
*/
attempts?: number
attempts?:
| number
| ((q: {
status: number
res: Response
error: Error
completedAttempts: number
}) => number | undefined | void)
/**
* The interval at which to run attempts on request fail
* @default 0
Expand Down
Loading