Skip to content
Closed
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
29 changes: 15 additions & 14 deletions library/src/plugins/actions/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,18 @@ const fetchEventSource = (

let retries = 0
let baseRetryInterval = retryInterval
const scheduleRetry = (interval: number = retryInterval): boolean => {
clearTimeout(retryTimer)
retryTimer = setTimeout(create, interval)
retryInterval = Math.min(retryInterval * retryScaler, retryMaxWait)
if (++retries >= retryMaxCount) {
dispatchFetch(RETRIES_FAILED, el, {})
dispose()
reject('Max retries reached.')
return false
}
return true
}
const create = async () => {
curRequestController = new AbortController()
const curRequestSignal = curRequestController.signal
Expand Down Expand Up @@ -569,8 +581,8 @@ const fetchEventSource = (
!isRedirectStatus &&
(retry === 'always' || (retry === 'error' && isErrorStatus))
) {
clearTimeout(retryTimer)
retryTimer = setTimeout(create, retryInterval)
dispatchFetch(RETRYING, el, { status: status.toString() })
scheduleRetry()
return
}
dispose()
Expand Down Expand Up @@ -662,18 +674,7 @@ const fetchEventSource = (
try {
// check if we need to retry:
const interval: any = onerror?.(err) || retryInterval
clearTimeout(retryTimer)
retryTimer = setTimeout(create, interval)
retryInterval = Math.min(
retryInterval * retryScaler,
retryMaxWait,
) // exponential backoff
if (++retries >= retryMaxCount) {
dispatchFetch(RETRIES_FAILED, el, {})
// we should not retry anymore:
dispose()
reject('Max retries reached.') // Max retries reached, check your server or network connection
} else {
if (scheduleRetry(interval)) {
console.error(
`Datastar failed to reach ${input.toString()} retrying in ${interval}ms.`,
)
Expand Down