Skip to content

Commit de08ca8

Browse files
committed
refactor(IntersectionService): providedIn root, allow multiple observers, add unobserve() method
1 parent 6a19217 commit de08ca8

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

projects/coreui-angular/src/lib/services/intersection.service.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { TestBed } from '@angular/core/testing';
33
import { IntersectionService } from './intersection.service';
44

55
describe('IntersectionService', () => {
6-
let service: IntersectionService;
6+
let service: any;
77

88
beforeEach(() => {
99
TestBed.configureTestingModule({
10-
imports: [IntersectionService]
10+
providers: [IntersectionService]
1111
});
12-
service = new IntersectionService();
13-
// service = TestBed.inject(IntersectionService);
12+
service = TestBed.inject(IntersectionService);
1413
});
1514

1615
it('should be created', () => {
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Injectable, OnDestroy } from '@angular/core';
1+
import { isPlatformServer } from '@angular/common';
2+
import { ElementRef, inject, Injectable, OnDestroy, PLATFORM_ID } from '@angular/core';
23
import { BehaviorSubject } from 'rxjs';
34

45
export interface IIntersectionObserverInit {
@@ -7,40 +8,58 @@ export interface IIntersectionObserverInit {
78
threshold?: number | number[];
89
}
910

10-
@Injectable()
11+
@Injectable(
12+
{ providedIn: 'root' }
13+
)
1114
export class IntersectionService implements OnDestroy {
1215

13-
constructor() { }
16+
platformId = inject(PLATFORM_ID);
1417

15-
private intersecting = new BehaviorSubject<boolean>(false);
16-
intersecting$ = this.intersecting.asObservable();
18+
readonly #intersecting = new BehaviorSubject<boolean>(false);
19+
readonly intersecting$ = this.#intersecting.asObservable();
1720

1821
private intersectionObserver!: IntersectionObserver;
19-
private hostElement!: { nativeElement: Element; };
22+
private hostElement!: ElementRef;
2023

2124
private defaultObserverOptions: IIntersectionObserverInit = {
2225
root: null,
2326
rootMargin: '0px',
2427
threshold: 0.2
2528
};
2629

27-
createIntersectionObserver(hostElement: { nativeElement: Element; }, observerOptions = this.defaultObserverOptions) {
30+
hostElementRefs: Map<ElementRef, IntersectionObserver | null> = new Map();
2831

29-
const options = { ...this.defaultObserverOptions, ...observerOptions };
32+
createIntersectionObserver(hostElement: ElementRef, observerOptions = this.defaultObserverOptions) {
33+
34+
if (isPlatformServer(this.platformId)) {
35+
this.#intersecting.next(true);
36+
return;
37+
}
3038

31-
this.hostElement = hostElement;
39+
// this.hostElement = hostElement;
40+
const options = { ...this.defaultObserverOptions, ...observerOptions };
3241

33-
const handleIntersect = (entries: any[], observer: any) => {
42+
const handleIntersect = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
3443
entries.forEach((entry: any) => {
35-
this.intersecting.next(entry.isIntersecting);
44+
this.#intersecting.next(entry.isIntersecting);
3645
});
3746
};
3847

39-
this.intersectionObserver = new IntersectionObserver(handleIntersect, options);
40-
this.intersectionObserver.observe(hostElement.nativeElement);
48+
const intersectionObserver: IntersectionObserver = new IntersectionObserver(handleIntersect, options);
49+
intersectionObserver.observe(hostElement.nativeElement);
50+
this.hostElementRefs.set(hostElement, intersectionObserver);
51+
52+
}
53+
54+
unobserve(elementRef: ElementRef) {
55+
this.hostElementRefs.get(elementRef)?.unobserve(elementRef.nativeElement);
56+
this.hostElementRefs.set(elementRef, null);
57+
this.hostElementRefs.delete(elementRef);
4158
}
4259

4360
ngOnDestroy(): void {
44-
this.intersectionObserver?.unobserve(this.hostElement?.nativeElement);
61+
this.hostElementRefs.forEach((observer, elementRef) => {
62+
observer?.unobserve(elementRef.nativeElement);
63+
});
4564
}
4665
}

0 commit comments

Comments
 (0)