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

Gracefully recover from observer exceptions 🧸 #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 15 additions & 13 deletions src/position-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ PositionObserver.prototype.check = function(viewportState) {

let untriggered = false

if (onBottom && !_wasBottom && atBottom) onBottom.call(this, container, viewportState)
else if (onTop && !_wasTop && atTop) onTop.call(this, container, viewportState)
else if (onRight && !_wasRight && atRight) onRight.call(this, container, viewportState)
else if (onLeft && !_wasLeft && atLeft) onLeft.call(this, container, viewportState)
else if (onFit && !_wasFit && fits) onFit.call(this, container, viewportState)
else untriggered = true
try {
if (onBottom && !_wasBottom && atBottom) onBottom.call(this, container, viewportState)
else if (onTop && !_wasTop && atTop) onTop.call(this, container, viewportState)
else if (onRight && !_wasRight && atRight) onRight.call(this, container, viewportState)
else if (onLeft && !_wasLeft && atLeft) onLeft.call(this, container, viewportState)
else if (onFit && !_wasFit && fits) onFit.call(this, container, viewportState)
else untriggered = true

if (once && !untriggered) this.destroy()

this._wasTop = atTop
this._wasBottom = atBottom
this._wasLeft = atLeft
this._wasRight = atRight
this._wasFit = fits
if (once && !untriggered) this.destroy()
} finally {
this._wasTop = atTop
this._wasBottom = atBottom
this._wasLeft = atLeft
this._wasRight = atRight
this._wasFit = fits
}
}
12 changes: 8 additions & 4 deletions src/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ export function Viewport(container, observerCollection) {
requestAnimationFrame(() => {
const { observers } = this
const state = this.getState()
for (let i = observers.length; i--; ) observers[i].check(state)
this.lastX = state.positionX
this.lastY = state.positionY
scheduled = false

try {
for (let i = observers.length; i--; ) observers[i].check(state)
} finally {
this.lastX = state.positionX
this.lastY = state.positionY
scheduled = false
}
})
}
}
Expand Down
55 changes: 54 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,60 @@ describe('viewprt', () => {
await testObserverType('ElementObserver')
})

it('destroys after trigging with once option', async () => {
it('recovers from observer exceptions', async () => {
assert.ok(
await page.evaluate(pageHeight => {
const content = document.createElement('div')
const contentHeight = pageHeight * 2
content.style.height = contentHeight + 'px'
document.body.appendChild(content)

return new Promise(resolve => {
PositionObserver({
onTop() {
setTimeout(() => resolve(true))
},
onBottom() {
throw new Error()
}
})
window.scrollTo(0, contentHeight)
setTimeout(() => window.scrollTo(0, 0), 65)
})
}, pageHeight)
)

await closePage()
await createPage()

assert.ok(
await page.evaluate(pageHeight => {
const content = document.createElement('div')
const contentHeight = pageHeight * 2
content.style.height = contentHeight + 'px'
document.body.appendChild(content)

const element = document.createElement('div')
element.style.height = '10px'
document.body.appendChild(element)

return new Promise(resolve => {
ElementObserver(element, {
onEnter() {
throw new Error()
},
onExit() {
setTimeout(() => resolve(true))
}
})
window.scrollTo(0, contentHeight)
setTimeout(() => window.scrollTo(0, 0), 65)
})
}, pageHeight)
)
})

it('destroys after triggering with once option', async () => {
assert.ok(
await page.evaluate(pageHeight => {
const content = document.createElement('div')
Expand Down