re-enable observer for moved elements (#4486)

Signed-off-by: Danil Uzlov <danil.uzlov@xored.com>
This commit is contained in:
Danil Uzlov 2024-02-02 14:10:58 +07:00 committed by GitHub
parent 7f7242b9de
commit 075b362c1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,7 +7,6 @@ const delayedCaller = new DelayedCaller(5)
function makeObserver (rootMargin: string): IntersectionObserver {
const entriesPending = new Map<Element, { isIntersecting: boolean }>()
const notifyObservers = (observer: IntersectionObserver): void => {
console.log('notifyObservers', entriesPending.size)
for (const [target, entry] of entriesPending.entries()) {
const entryData = entryMap.get(target)
if (entryData == null) {
@ -69,12 +68,26 @@ export function lazyObserver (node: Element, onVisible: (value: boolean, unsubsc
return {}
}
const destroy = listen('20%', node, (isIntersecting) => {
visible = isIntersecting
onVisible(visible, destroy)
})
let needsUpdate = true
let destroy = (): void => {}
// we need this update function to re-trigger observer for moved elements
// moved elements are relevant because onVisible can have side effects
const update = (): void => {
if (!needsUpdate) {
return
}
needsUpdate = false
destroy()
destroy = listen('20%', node, (isIntersecting) => {
visible = isIntersecting
needsUpdate = visible
onVisible(visible, destroy)
})
}
update()
return {
destroy
destroy,
update
}
}