mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
|
export function waitUntilScrollingFinished(selector: string, start = -1) {
|
||
|
const pollSlow = 100
|
||
|
const pollFast = 10
|
||
|
const deadline =
|
||
|
performance.now() + Cypress.config('defaultCommandTimeout') - pollSlow * 2
|
||
|
return cy.get(selector).then(el => {
|
||
|
cy.log(
|
||
|
`waiting until scrolling finished for ${selector}, starting from ${start}`
|
||
|
)
|
||
|
return new Cypress.Promise<number>((resolve, reject) => {
|
||
|
const waitForStable = (prev: number, stableFor: number) => {
|
||
|
if (performance.now() > deadline) {
|
||
|
return reject(new Error('timeout waiting for scrolling to finish'))
|
||
|
}
|
||
|
const current = el.scrollTop()!
|
||
|
if (current !== prev) {
|
||
|
setTimeout(() => waitForStable(current, 0), pollFast)
|
||
|
} else if (stableFor < 3) {
|
||
|
setTimeout(() => waitForStable(current, stableFor + 1), pollFast)
|
||
|
} else {
|
||
|
resolve(current)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const waitForChange = () => {
|
||
|
if (performance.now() > deadline) {
|
||
|
return reject(new Error('timeout waiting for scrolling to start'))
|
||
|
}
|
||
|
const current = el.scrollTop()!
|
||
|
if (current === start) {
|
||
|
setTimeout(() => waitForChange(), pollSlow)
|
||
|
} else {
|
||
|
setTimeout(() => waitForStable(current, 0), pollFast)
|
||
|
}
|
||
|
}
|
||
|
waitForChange()
|
||
|
})
|
||
|
})
|
||
|
}
|