-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathCameraOperation.ts
45 lines (37 loc) · 1.23 KB
/
CameraOperation.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
import { Camera } from "cesium";
import { useEffect, useRef, FC } from "react";
import { useCesium } from "./context";
export type CameraOperationProps = {
cancelFlightOnUnmount?: boolean;
once?: boolean;
};
export const createCameraOperation = <P>(
name: string,
cameraOperationStart: (camera: Camera, props: P, prevProps?: P) => void,
): FC<P & CameraOperationProps> => {
/* eslint-disable react-hooks/rules-of-hooks */
const component: FC<P & CameraOperationProps> = props => {
const ctx = useCesium();
const prevProps = useRef<P | undefined>(undefined);
const first = useRef(false);
useEffect(() => {
return () => {
if (ctx.camera && props.cancelFlightOnUnmount) {
ctx.camera.cancelFlight();
}
};
}, [ctx.camera, props.cancelFlightOnUnmount]);
useEffect(() => {
if (ctx.camera && ctx.scene && !ctx.scene.isDestroyed() && (!props.once || !first.current)) {
ctx.camera.cancelFlight();
cameraOperationStart(ctx.camera, props, prevProps.current);
first.current = true;
}
prevProps.current = props;
});
return null;
};
/* eslint-enable react-hooks/rules-of-hooks */
component.displayName = name;
return component;
};