From c5e2eeee2d6feb1a0f993430f4d0ae029ad9f8b9 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 11 Aug 2019 11:09:43 -0700 Subject: [PATCH] perf: fix potential memory leak in IntersectionObserver (#1383) --- .../compose/ComposeStickyButton.html | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/routes/_components/compose/ComposeStickyButton.html b/src/routes/_components/compose/ComposeStickyButton.html index ba393d14..c4d19656 100644 --- a/src/routes/_components/compose/ComposeStickyButton.html +++ b/src/routes/_components/compose/ComposeStickyButton.html @@ -58,11 +58,11 @@ export default { oncreate () { - this.setupStickyObserver() + this.setupIntersectionObservers() this.setupIOSWorkaround() }, ondestroy () { - this.teardownStickyObserver() + this.teardownIntersectionObservers() }, store: () => store, data: () => ({ @@ -140,7 +140,7 @@ }) this.on('destroy', () => cleanup()) }, - setupStickyObserver () { + setupIntersectionObservers () { const sentinel = this.refs.sentinel this.__stickyObserver = new IntersectionObserver(entries => this.onObserve(entries)) @@ -150,21 +150,27 @@ // due to a bug in Firefox where when the scrollTop is set // manually, the other observer doesn't necessarily fire this.observe('timelineInitialized', timelineInitialized => { - if (timelineInitialized) { - const observer = new IntersectionObserver(entries => { + if (timelineInitialized && !this.__oneShotObserver) { + this.__oneShotObserver = new IntersectionObserver(entries => { this.onObserve(entries) - observer.disconnect() + this.__oneShotObserver.disconnect() + this.__oneShotObserver = null }) - observer.observe(sentinel) + this.__oneShotObserver.observe(sentinel) } }, { init: false }) }, onObserve (entries) { this.set({ sticky: !entries[0].isIntersecting }) }, - teardownStickyObserver () { + teardownIntersectionObservers () { if (this.__stickyObserver) { this.__stickyObserver.disconnect() + this.__stickyObserver = null + } + if (this.__oneShotObserver) { + this.__oneShotObserver.disconnect() + this.__oneShotObserver = null } } },