Skip to content

Commit

Permalink
Tie page visibility api to on demand pinger (vercel#2818)
Browse files Browse the repository at this point in the history
Avoid making requests to the on demand reloader if the page is not visible.
  • Loading branch information
kpdecker authored and timneutkens committed Sep 8, 2017
1 parent c0d031d commit 808c662
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions client/on-demand-entries-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,31 @@ export default () => {
}
}

let pingerTimeout
async function runPinger () {
while (true) {
await new Promise((resolve) => setTimeout(resolve, 5000))
// Will restart on the visibilitychange API below. For older browsers, this
// will always be true and will always run, but support is fairly prevalent
// at this point.
while (!document.hidden) {
await ping()
await new Promise((resolve) => {
pingerTimeout = setTimeout(resolve, 5000)
})
}
}

runPinger()
.catch((err) => {
console.error(err)
})
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
runPinger()
} else {
clearTimeout(pingerTimeout)
}
}, false)

setTimeout(() => {
runPinger()
.catch((err) => {
console.error(err)
})
}, 10000)
}

0 comments on commit 808c662

Please sign in to comment.