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

fix(Picking): guard against unmounted state #131

Open
wants to merge 2 commits into
base: beta
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions src/core/Picking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import deletionRegistry from '../utils/DeletionRegistry';
import useDebounce from '../utils/useDebounce';
import useGetterRef from '../utils/useGetterRef';
import { useIsMounted } from '../utils/useIsMounted';
import useMount from '../utils/useMount';
import useUnmount from '../utils/useUnmount';
import {
Expand Down Expand Up @@ -136,6 +137,8 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {
};
});

const isMounted = useIsMounted();

// --- Props --- //

const {
Expand All @@ -152,12 +155,16 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {

const pickClosest = useCallback(
(xp: number, yp: number, tolerance: number): PickResult[] => {
if (!isMounted.current) return [];

const x1 = Math.floor(xp - tolerance);
const y1 = Math.floor(yp - tolerance);
const x2 = Math.ceil(xp + tolerance);
const y2 = Math.ceil(yp + tolerance);

const selector = getSelector();
if (selector.isDeleted()) return [];

const openGLRenderWindow = openGLRenderWindowAPI.get();
const renderer = rendererAPI.get();

Expand Down Expand Up @@ -218,12 +225,16 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {
},
] as PickResult[];
},
[rendererAPI, openGLRenderWindowAPI, getSelector]
[rendererAPI, openGLRenderWindowAPI, isMounted, getSelector]
);

const pick = useCallback(
(x1: number, y1: number, x2: number, y2: number): PickResult[] => {
if (!isMounted.current) return [];

const selector = getSelector();
if (selector.isDeleted()) return [];

const openGLRenderWindow = openGLRenderWindowAPI.get();
const renderer = rendererAPI.get();

Expand Down Expand Up @@ -277,7 +288,7 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {
})
.filter(Boolean) as PickResult[];
},
[rendererAPI, openGLRenderWindowAPI, getSelector]
[rendererAPI, openGLRenderWindowAPI, isMounted, getSelector]
);

const pickWithFrustum = useCallback(
Expand All @@ -287,7 +298,11 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {
x2: number,
y2: number
): FrustumPickResult | null => {
if (!isMounted.current) return null;

const selector = getSelector();
if (selector.isDeleted()) return null;

const openGLRenderWindow = openGLRenderWindowAPI.get();
const renderer = rendererAPI.get();

Expand Down Expand Up @@ -324,7 +339,7 @@ export default forwardRef(function ViewPicking(props: PickingProps, fwdRef) {
});
return { frustum, representationIds };
},
[rendererAPI, openGLRenderWindowAPI, getSelector]
[rendererAPI, openGLRenderWindowAPI, isMounted, getSelector]
);

const api = useMemo(
Expand Down
17 changes: 17 additions & 0 deletions src/utils/useIsMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useRef } from 'react';
import useMount from './useMount';
import useUnmount from './useUnmount';

export function useIsMounted() {
const isMounted = useRef(false);

useMount(() => {
isMounted.current = true;
});

useUnmount(() => {
isMounted.current = false;
});

return isMounted;
}