diff options
author | Nolan Lawson <nolan@nolanlawson.com> | 2017-05-29 09:17:51 -0700 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-05-29 18:17:51 +0200 |
commit | 34a93ccf571426ffaac68c573f231bb679c0ff3b (patch) | |
tree | d4141134e914772377e969f21978f630f52b0eb4 /app/javascript/mastodon/features/ui/util | |
parent | 922fb74197ef65e6f6ebfba627f80eb483486c1c (diff) |
Add IntersectionObserverWrapper to cut down on re-renders (#3406)
Diffstat (limited to 'app/javascript/mastodon/features/ui/util')
-rw-r--r-- | app/javascript/mastodon/features/ui/util/intersection_observer_wrapper.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/app/javascript/mastodon/features/ui/util/intersection_observer_wrapper.js b/app/javascript/mastodon/features/ui/util/intersection_observer_wrapper.js new file mode 100644 index 000000000..0e959f9ae --- /dev/null +++ b/app/javascript/mastodon/features/ui/util/intersection_observer_wrapper.js @@ -0,0 +1,48 @@ +// Wrapper for IntersectionObserver in order to make working with it +// a bit easier. We also follow this performance advice: +// "If you need to observe multiple elements, it is both possible and +// advised to observe multiple elements using the same IntersectionObserver +// instance by calling observe() multiple times." +// https://developers.google.com/web/updates/2016/04/intersectionobserver + +class IntersectionObserverWrapper { + + callbacks = {}; + observerBacklog = []; + observer = null; + + connect (options) { + const onIntersection = (entries) => { + entries.forEach(entry => { + const id = entry.target.getAttribute('data-id'); + if (this.callbacks[id]) { + this.callbacks[id](entry); + } + }); + }; + + this.observer = new IntersectionObserver(onIntersection, options); + this.observerBacklog.forEach(([ id, node, callback ]) => { + this.observe(id, node, callback); + }); + this.observerBacklog = null; + } + + observe (id, node, callback) { + if (!this.observer) { + this.observerBacklog.push([ id, node, callback ]); + } else { + this.callbacks[id] = callback; + this.observer.observe(node); + } + } + + disconnect () { + if (this.observer) { + this.observer.disconnect(); + } + } + +} + +export default IntersectionObserverWrapper; |