Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: dont do anything if already have match #28

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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.
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.

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

Expand Down
21 changes: 8 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ export default class IntersectionObserverAdmin extends Notifications {
if (intersectionObserver) {
intersectionObserver.observe(element);
}
} else {
} else if (!potentialRootMatch) {
// 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 @@ -170,19 +172,12 @@ 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);
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
});
}

// no root exists, so add to WeakMap
this.elementRegistry.addElement(root, {
[stringifiedOptions]: observerEntry
});
}
}

Expand Down