Skip to content

Commit

Permalink
Delay getElement timeout until after rAF
Browse files Browse the repository at this point in the history
Under normal circumstances, this should be a negligible increase in the
timeout, but if this tab is in the background, this rAF will delay until
it has focus.

Fixes insin#198.
  • Loading branch information
mcpower committed Jan 21, 2023
1 parent 62625cd commit e8bcfc4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tweak-new-twitter.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ function getElement(selector, {
let startTime = Date.now()
let rafId
let timeoutId
let stopped = false

function stop($element, reason) {
if ($element == null) {
Expand All @@ -886,11 +887,21 @@ function getElement(selector, {
if (timeoutId) {
clearTimeout(timeoutId)
}
stopped = true
resolve($element)
}

if (timeout !== Infinity) {
timeoutId = setTimeout(stop, timeout, null, `${timeout}ms timeout`)
// Delay the timeout until after a rAF to avoid
// https://github.com/insin/tweak-new-twitter/issues/198.
// Under normal circumstances, this should be a negligible increase in the
// timeout, but if this tab is in the background, this rAF will delay
// until it has focus.
requestAnimationFrame(() => {
if (!stopped) {
timeoutId = setTimeout(stop, timeout, null, `${timeout}ms timeout`)
}
})
}

function queryElement() {
Expand Down Expand Up @@ -3370,4 +3381,4 @@ function configChanged(changes) {
main()
//#endregion

}()
}()

0 comments on commit e8bcfc4

Please sign in to comment.