Skip to content

Commit

Permalink
Revert "[Bug]: dont do anything if already have match (#28)"
Browse files Browse the repository at this point in the history
This reverts commit d72a8bb.
  • Loading branch information
scott-newcomer committed May 3, 2021
1 parent 8c9d870 commit bc20a6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Why use an administrator to manage all the elements on my page?
------------------------------------------------------------------------------
This library is used in [ember-in-viewport](https://github.com/DockYard/ember-in-viewport) and [ember-infinity](https://github.com/ember-infinity/ember-infinity). This library is particularly important for re-using the IntersectionObserver API.

Most implementations have one Intersection Observer for each target element or so called `sentinel`. However, [IntersectionObserver.observe](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/observe) can observer multiple `sentinels`. So this library will reuse the IntersectionObserver instance for another element on the page with the same set of observer options and root element. This can dramatically improve performance for pages with lots of elements and observers.
Most implementations have one Intersection Observer for each target element or so called `sentinel`. However, [IntersectionObserver.observe](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/observe) can observer multiple `sentinels`. So this library will resuse the IntersectionObserver instance for another element on the page with the same set of observer options and root element. This can dramatically improve performance for pages with lots of elements and observers.

_Note: A companion library is also available for requestAnimationFrame: https://github.com/snewcomer/raf-pool_

Expand Down
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,8 @@ export default class IntersectionObserverAdmin extends Notifications {
if (intersectionObserver) {
intersectionObserver.observe(element);
}
} else if (!potentialRootMatch) {
} else {
// otherwise start observing this element if applicable
// and add entry to WeakMap under a root element
// with watcher so we can use it later on
// watcher is an instance that has an observe method
const intersectionObserver = this.newObserver(element, options);

Expand All @@ -172,12 +170,19 @@ export default class IntersectionObserverAdmin extends Notifications {
options
};

// and add entry to WeakMap under a root element
// with watcher so we can use it later on
const stringifiedOptions: string = this.stringifyOptions(options);

// no root exists, so add to WeakMap
this.elementRegistry.addElement(root, {
[stringifiedOptions]: observerEntry
});
if (potentialRootMatch) {
// if share same root and need to add new entry to root match
// not functional but :shrug
potentialRootMatch[stringifiedOptions] = observerEntry;
} else {
// no root exists, so add to WeakMap
this.elementRegistry.addElement(root, {
[stringifiedOptions]: observerEntry
});
}
}
}

Expand Down

0 comments on commit bc20a6e

Please sign in to comment.