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

Delay getElement timeout until after rAF #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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

}()
}()