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

Retry to resolve an element #81

Merged
merged 4 commits into from
Jul 8, 2021
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
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