Skip to content

Commit

Permalink
feat: refactor retryable and refresh token retries
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Aug 30, 2023
1 parent 22923e7 commit 5aff2ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,12 +1530,20 @@ export default class GoTrueClient {
xform: _sessionResponse,
})
},
(attempt, _, result) =>
result &&
result.error &&
isAuthRetryableFetchError(result.error) &&
// retryable only if the request can be sent before the backoff overflows the tick duration
Date.now() + (attempt + 1) * 200 - startedAt < AUTO_REFRESH_TICK_DURATION
(attempt, error, result) => {
if (error && !isAuthRetryableFetchError(error)) {
// not retryable
return false
}

return (
result &&
result.error &&
isAuthRetryableFetchError(result.error) &&
// retryable only if the request can be sent before the backoff overflows the tick duration
Date.now() + (attempt + 1) * 200 - startedAt < AUTO_REFRESH_TICK_DURATION
)
}
)
} catch (error) {
this._debug(debugName, 'error', error)
Expand Down
13 changes: 11 additions & 2 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,17 @@ export function retryable<T>(
return
}
} catch (e: any) {
if (!isRetryable(attempt, e)) {
reject(e)
let shouldRetry = false

try {
shouldRetry = isRetryable(attempt, e)
} catch (innerError: any) {
reject(innerError)
return
}

if (!shouldRetry) {
reject(error)
return
}
}
Expand Down

0 comments on commit 5aff2ed

Please sign in to comment.