-
Notifications
You must be signed in to change notification settings - Fork 11
/
offscreen-observer.ts
50 lines (39 loc) · 1.49 KB
/
offscreen-observer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { DotLottie } from './dotlottie';
import type { DotLottieWorker } from './worker/dotlottie';
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class OffscreenObserver {
private static _observer: IntersectionObserver | null = null;
private static readonly _observedCanvases = new Map<HTMLCanvasElement, DotLottie | DotLottieWorker>();
private static _initializeObserver(): void {
if (this._observer) return;
const intersectionObserverCallback = (entries: IntersectionObserverEntry[]): void => {
entries.forEach((entry) => {
const instance = this._observedCanvases.get(entry.target as HTMLCanvasElement);
if (instance) {
if (entry.isIntersecting) {
instance.unfreeze();
} else {
instance.freeze();
}
}
});
};
this._observer = new IntersectionObserver(intersectionObserverCallback, {
threshold: 0,
});
}
public static observe(canvas: HTMLCanvasElement, dotLottieInstance: DotLottie | DotLottieWorker): void {
this._initializeObserver();
if (this._observedCanvases.has(canvas)) return;
this._observedCanvases.set(canvas, dotLottieInstance);
this._observer?.observe(canvas);
}
public static unobserve(canvas: HTMLCanvasElement): void {
this._observer?.unobserve(canvas);
this._observedCanvases.delete(canvas);
if (this._observedCanvases.size === 0) {
this._observer?.disconnect();
this._observer = null;
}
}
}