Skip to content

Commit

Permalink
feat: Wrap updates-for-element in an IntersectionObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Sep 14, 2023
1 parent 58a9799 commit 4e1db1f
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion javascript/elements/updates_for_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ export default class UpdatesForElement extends SubscribingElement {

this.triggerElementLog = new BoundedQueue(10)
this.targetElementLog = new BoundedQueue(10)

this.intersecting = false
this.intersectionObserver = new IntersectionObserver(
this.intersectionCallback.bind(this),
{}
)

this.intersectionObserver.observe(this)
}

intersectionCallback (entries, observe) {
entries.forEach(entry => {
if (entry.target === this) {
if (entry.isIntersecting) {
// transition from non-intersecting to intersecting forces update
if (!this.intersecting) {
this.update({})
}
this.intersecting = true
} else {
this.intersecting = false
}
}
})
}

async connectedCallback () {
Expand Down Expand Up @@ -237,7 +261,11 @@ class Block {

shouldUpdate (data) {
// if everything that could prevent an update is false, update this block
return !this.ignoresInnerUpdates && this.hasChangesSelectedForUpdate(data)
return (
!this.ignoresInnerUpdates &&
this.hasChangesSelectedForUpdate(data) &&
this.intersecting
)
}

hasChangesSelectedForUpdate (data) {
Expand Down Expand Up @@ -272,4 +300,8 @@ class Block {
get query () {
return this.element.query
}

get intersecting () {
return this.element.intersecting
}
}

0 comments on commit 4e1db1f

Please sign in to comment.