Skip to content

Commit

Permalink
Merge pull request #209 from BonnierNews/fix/reduce-scroll-listeners
Browse files Browse the repository at this point in the history
Fix for scroll listener warnings
  • Loading branch information
jonaswalden authored Aug 9, 2024
2 parents bf98ee7 + cd56c79 commit 3fbbb10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- version -->
# 16.0.0 API Reference
# 16.0.1 API Reference
<!-- versionstop -->

<!-- toc -->
Expand Down
13 changes: 12 additions & 1 deletion lib/IntersectionObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ module.exports = function fakeIntersectionObserver(browser) {
const IntersectionObserverEntry = fakeIntersectionObserverEntry(browser);
browser.window.IntersectionObserverEntry = IntersectionObserverEntry;

const eventHandlers = [];

function addEventHandler(handler) {
if (eventHandlers.length === 0) {
browser.window.addEventListener("scroll", () => {
eventHandlers.forEach((fn) => fn());
});
}
eventHandlers.push(handler);
}

class IntersectionObserver {
constructor(viewportUpdate, options) {
this[kToObserve] = [];
Expand All @@ -23,7 +34,7 @@ module.exports = function fakeIntersectionObserver(browser) {
this[kUpdate] = viewportUpdate;
this[kRootMargin] = getRootMargin(options);

browser.window.addEventListener("scroll", () => {
addEventHandler(() => {
const entries = this[kToObserve].map((el) => new IntersectionObserverEntry(el, this[kRootMargin]));
const changedEntries = entries.filter((entry) => {
const previous = this[kPrevEntries].find((x) => x.target === entry.target);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@expressen/tallahassee",
"version": "16.0.0",
"version": "16.0.1",
"description": "Lightweight client testing framework",
"main": "index.js",
"license": "BSD-3-Clause",
Expand Down

0 comments on commit 3fbbb10

Please sign in to comment.