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

feat: include node cache in keep-alive context #3693

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { type DmsUniqueIdentifier } from '../../utilities/FdmSDK';
import { type TypedReveal3DModel } from '../Reveal3DResources/types';
import { type ThreeDModelMappings } from '../../hooks/types';
import { DEFAULT_QUERY_STALE_TIME } from '../../utilities/constants';
import { useRevealKeepAlive } from '../RevealKeepAlive/RevealKeepAliveContext';

export type FdmNodeCacheContent = {
cache: FdmNodeCache;
Expand Down Expand Up @@ -97,8 +98,15 @@ export const useFdmAssetMappings = (
export function NodeCacheProvider({ children }: { children?: ReactNode }): ReactElement {
const fdmClient = useFdmSdk();
const cdfClient = useSDK();
const revealKeepAliveData = useRevealKeepAlive();

const fdmCache = useMemo(() => new FdmNodeCache(cdfClient, fdmClient), []);
const fdmCache = useMemo(() => {
const cache = revealKeepAliveData?.fdmNodeCache.current ?? new FdmNodeCache(cdfClient, fdmClient);
if (revealKeepAliveData !== undefined) {
revealKeepAliveData.fdmNodeCache.current = cache;
}
return cache;
Savokr marked this conversation as resolved.
Show resolved Hide resolved
}, []);

return (
<FdmNodeCacheContext.Provider value={{ cache: fdmCache }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import { type Cognite3DViewer } from '@cognite/reveal';
import { type ReactNode, type ReactElement, useRef, useEffect } from 'react';
import { RevealKeepAliveContext } from './RevealKeepAliveContext';
import { FdmNodeCache } from '../NodeCacheProvider/FdmNodeCache';

export function RevealKeepAlive({ children }: { children?: ReactNode }): ReactElement {
const viewerRef = useRef<Cognite3DViewer>();
const isRevealContainerMountedRef = useRef<boolean>(false);
const fdmNodeCache = useRef<FdmNodeCache>();

useEffect(() => {
return () => {
viewerRef.current?.dispose();
viewerRef.current = undefined;
};
}, []);
return (
<RevealKeepAliveContext.Provider value={{ viewerRef, isRevealContainerMountedRef }}>
<RevealKeepAliveContext.Provider value={{ viewerRef, isRevealContainerMountedRef, fdmNodeCache }}>
{children}
</RevealKeepAliveContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*/
import { type Cognite3DViewer } from '@cognite/reveal';
import { type MutableRefObject, createContext, useContext } from 'react';
import { FdmNodeCache } from '../NodeCacheProvider/FdmNodeCache';

export type RevealKeepAliveData = {
viewerRef: MutableRefObject<Cognite3DViewer | undefined>;
isRevealContainerMountedRef: MutableRefObject<boolean>;
fdmNodeCache: MutableRefObject<FdmNodeCache | undefined>;
};

export const RevealKeepAliveContext = createContext<RevealKeepAliveData | undefined>(undefined);
Expand Down
Loading