Skip to content

Commit

Permalink
fix: ngzone issue in inject-is-intersecting and docs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eneajaho committed Oct 15, 2023
1 parent da38aa8 commit 7a903b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class MyComponent {

isIntersecting$ = injectIsIntersecting();

isInViewport$ = this.isIntersecting$.pipe(filter(x.intersectionRatio > 0), take(1));
isInViewport$ = this.isIntersecting$.pipe(
filter((x) => x.intersectionRatio > 0),
take(1)
);

ngOnInit() {
this.getData().subscribe();
Expand Down Expand Up @@ -55,7 +58,7 @@ export class MyComponent implements OnInit {
const divInViewport$ = injectIsIntersecting({
element: this.myDivEl,
injector: this.injector,
}).pipe(filter(x.intersectionRatio > 0));
}).pipe(filter((x) => x.intersectionRatio > 0));

// Only fetch data when the element is in the viewport
divInViewport$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface InjectIsIntersectingOptions {
*
* isIntersecting$ = injectIsIntersecting();
* isInViewport$ = this.isIntersecting$.pipe(
* filter(x.intersectionRatio > 0),
* filter(x => x.intersectionRatio > 0),
* take(1),
* );
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export class IsInViewportService implements IsInViewportServiceInterface {
}

intersect(element: Element, entry: IntersectionObserverEntry) {
this.#observerListeners.get(element)?.next(entry);
const subject = this.#observerListeners.get(element);
// only emit if the subject is subscribed to
if (subject?.observed) {
this.ngZone.run(() => subject.next(entry));
}
}

#disconnect() {
Expand Down

0 comments on commit 7a903b2

Please sign in to comment.