Skip to content

Commit

Permalink
Merge pull request #81 from julianrubisch/retry-resolve
Browse files Browse the repository at this point in the history
Retry to resolve an element
  • Loading branch information
julianrubisch authored Jul 8, 2021
2 parents ac2169e + 602ee1e commit befee43
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions javascript/elements/futurism_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,27 @@ const dispatchAppearEvent = (entry, observer = null) => {
target.dispatchEvent(evt)
}

// from https://advancedweb.hu/how-to-implement-an-exponential-backoff-retry-strategy-in-javascript/#rejection-based-retrying
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))

const callWithRetry = async (fn, depth = 0) => {
try {
return await fn()
} catch (e) {
if (depth > 10) {
throw e
}
await wait(1.15 ** depth * 2000)

return callWithRetry(fn, depth + 1)
}
}

const observerCallback = (entries, observer) => {
entries.forEach(entry => {
entries.forEach(async entry => {
if (!entry.isIntersecting) return
dispatchAppearEvent(entry, observer)

await callWithRetry(dispatchAppearEvent(entry, observer))
})
}

Expand Down

0 comments on commit befee43

Please sign in to comment.