From 0a7f56004fe9d820af8dfd38d08846d50598bdff Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Tue, 23 Jan 2018 21:02:51 -0800 Subject: [PATCH] guard against NPEs for IntersectionObserver --- routes/_utils/AsyncLayout.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/routes/_utils/AsyncLayout.js b/routes/_utils/AsyncLayout.js index a13cdf22..5dd3cf3c 100644 --- a/routes/_utils/AsyncLayout.js +++ b/routes/_utils/AsyncLayout.js @@ -14,24 +14,30 @@ class AsyncLayout { } observe(key, node, callback) { - this._onIntersectionCallbacks[key] = (entry) => { - callback(getRectFromEntry(entry)) - this.unobserve(key, node) + if (this._intersectionObserver) { + this._onIntersectionCallbacks[key] = (entry) => { + callback(getRectFromEntry(entry)) + this.unobserve(key, node) + } + this._intersectionObserver.observe(node) } - this._intersectionObserver.observe(node) } unobserve(key, node) { if (key in this._onIntersectionCallbacks) { return } - this._intersectionObserver.unobserve(node) + if (this._intersectionObserver) { + this._intersectionObserver.unobserve(node) + } delete this._onIntersectionCallbacks[key] } disconnect() { - this._intersectionObserver.disconnect() - this._intersectionObserver = null + if (this._intersectionObserver) { + this._intersectionObserver.disconnect() + this._intersectionObserver = null + } } }