guard against NPEs for IntersectionObserver

This commit is contained in:
Nolan Lawson 2018-01-23 21:02:51 -08:00
parent 2a215480e2
commit 0a7f56004f

View file

@ -14,24 +14,30 @@ class AsyncLayout {
} }
observe(key, node, callback) { observe(key, node, callback) {
this._onIntersectionCallbacks[key] = (entry) => { if (this._intersectionObserver) {
callback(getRectFromEntry(entry)) this._onIntersectionCallbacks[key] = (entry) => {
this.unobserve(key, node) callback(getRectFromEntry(entry))
this.unobserve(key, node)
}
this._intersectionObserver.observe(node)
} }
this._intersectionObserver.observe(node)
} }
unobserve(key, node) { unobserve(key, node) {
if (key in this._onIntersectionCallbacks) { if (key in this._onIntersectionCallbacks) {
return return
} }
this._intersectionObserver.unobserve(node) if (this._intersectionObserver) {
this._intersectionObserver.unobserve(node)
}
delete this._onIntersectionCallbacks[key] delete this._onIntersectionCallbacks[key]
} }
disconnect() { disconnect() {
this._intersectionObserver.disconnect() if (this._intersectionObserver) {
this._intersectionObserver = null this._intersectionObserver.disconnect()
this._intersectionObserver = null
}
} }
} }