From 5a8900ed80ac3a1cba996d8e0cdd12a288db0f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= <70905152+haakonflatval-cognite@users.noreply.github.com> Date: Wed, 13 Sep 2023 11:37:59 +0200 Subject: [PATCH] feat: include node cache in keep-alive context (#3693) * feat: include node cache in keep-alive context --- .../NodeCacheProvider/NodeCacheProvider.tsx | 14 +++++++++++++- .../components/RevealKeepAlive/RevealKeepAlive.tsx | 6 +++++- .../RevealKeepAlive/RevealKeepAliveContext.ts | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx index bc973319a9d..699f363486d 100644 --- a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx +++ b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx @@ -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; @@ -97,8 +98,19 @@ 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); + + const isRevealKeepAliveContextProvided = revealKeepAliveData !== undefined; + if (isRevealKeepAliveContextProvided) { + revealKeepAliveData.fdmNodeCache.current = cache; + } + + return cache; + }, [cdfClient, fdmClient]); return ( diff --git a/react-components/src/components/RevealKeepAlive/RevealKeepAlive.tsx b/react-components/src/components/RevealKeepAlive/RevealKeepAlive.tsx index 15bc32eae95..73b284cb8fa 100644 --- a/react-components/src/components/RevealKeepAlive/RevealKeepAlive.tsx +++ b/react-components/src/components/RevealKeepAlive/RevealKeepAlive.tsx @@ -5,10 +5,13 @@ import { type Cognite3DViewer } from '@cognite/reveal'; import { type ReactNode, type ReactElement, useRef, useEffect } from 'react'; import { RevealKeepAliveContext } from './RevealKeepAliveContext'; +import { type FdmNodeCache } from '../NodeCacheProvider/FdmNodeCache'; export function RevealKeepAlive({ children }: { children?: ReactNode }): ReactElement { const viewerRef = useRef(); const isRevealContainerMountedRef = useRef(false); + const fdmNodeCache = useRef(); + useEffect(() => { return () => { viewerRef.current?.dispose(); @@ -16,7 +19,8 @@ export function RevealKeepAlive({ children }: { children?: ReactNode }): ReactEl }; }, []); return ( - + {children} ); diff --git a/react-components/src/components/RevealKeepAlive/RevealKeepAliveContext.ts b/react-components/src/components/RevealKeepAlive/RevealKeepAliveContext.ts index edbabebd0f7..bcd0b27e342 100644 --- a/react-components/src/components/RevealKeepAlive/RevealKeepAliveContext.ts +++ b/react-components/src/components/RevealKeepAlive/RevealKeepAliveContext.ts @@ -3,10 +3,12 @@ */ import { type Cognite3DViewer } from '@cognite/reveal'; import { type MutableRefObject, createContext, useContext } from 'react'; +import { type FdmNodeCache } from '../NodeCacheProvider/FdmNodeCache'; export type RevealKeepAliveData = { viewerRef: MutableRefObject; isRevealContainerMountedRef: MutableRefObject; + fdmNodeCache: MutableRefObject; }; export const RevealKeepAliveContext = createContext(undefined);