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

fix: don’t throw context error when retry is disabled #60

Merged
merged 1 commit into from
Nov 18, 2024
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
30 changes: 25 additions & 5 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,24 @@ export async function retry<T>(
let lastErr: unknown
const startTime = Date.now()

if (options.disable) {
return fn()
let tries = 0

const shouldContinue = () => {
// always run once
if (tries < 1) return true
// if retries are disabled, don't run a second time
if (options.disable) return false
// if timeout is not reached, keep trying
if (Date.now() - startTime < timeout) {
return true
}
return false
}

while (Date.now() - startTime < timeout) {
while (shouldContinue()) {
tries++
try {
// Do it!
return await fn()
} catch (err) {
lastErr = err
Expand All @@ -159,15 +171,23 @@ export async function retry<T>(
errString.toLowerCase().includes(match.toLowerCase())
))
) {
// it's not a matching error, throw immediately
throw err
}
if (Date.now() - startTime >= timeout) {
continue
if (!shouldContinue()) {
if (options.disable) {
// if matching error was thrown, but retries are disabled
// just return undefined
return
}
break
}
if (poll === 'raf') {
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
// we're in a renderer environment and can use requestAnimationFrame
await new Promise((resolve) => requestAnimationFrame(resolve))
} else {
// we're in Node.js or another environment without requestAnimationFrame
await new Promise((resolve) => setTimeout(resolve, 20))
}
} else {
Expand Down
Loading