From b22a7c6a1ff5318296a68d283fdbfed71444a95d Mon Sep 17 00:00:00 2001 From: Savelii Novikov Date: Fri, 8 Sep 2023 15:12:18 +0200 Subject: [PATCH 1/5] Current state of things --- .../NodeCacheProvider/FdmNodeCache.ts | 50 +++++++++++++------ .../NodeCacheProvider/NodeCacheProvider.tsx | 8 +-- .../NodeCacheProvider/RevisionFdmNodeCache.ts | 48 +++++++++++++----- .../src/components/NodeCacheProvider/types.ts | 2 +- .../stories/HighlightNode.stories.tsx | 14 +++++- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts index 752820dcfa9..172fc0178be 100644 --- a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts @@ -9,7 +9,7 @@ import { type EdgeItem, type FdmSDK } from '../../utilities/FdmSDK'; -import { RevisionFdmNodeCache } from './RevisionFdmNodeCache'; +import { RevisionFdmNodeCache, checkDefinedView } from './RevisionFdmNodeCache'; import { type FdmEdgeWithNode, type FdmCadEdge, @@ -160,16 +160,17 @@ export class FdmNodeCache { } public async getAllMappingExternalIds( - modelRevisionIds: ModelRevisionId[] + modelRevisionIds: ModelRevisionId[], + fetchViews: boolean = false ): Promise { const [cachedRevisionIds, nonCachedRevisionIds] = partition(modelRevisionIds, (ids) => { const key = createModelRevisionKey(ids.modelId, ids.revisionId); return this._completeRevisions.has(key); }); - const cachedEdges = cachedRevisionIds.map((id) => this.getCachedEdgesForRevision(id)); + const cachedEdges = await this.getCachedEdges(cachedRevisionIds, fetchViews); - const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap(nonCachedRevisionIds); + const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap(nonCachedRevisionIds, fetchViews); cachedEdges.forEach(([revisionKey, edges]) => { revisionToEdgesMap.set(revisionKey, edges); @@ -178,12 +179,28 @@ export class FdmNodeCache { return revisionToEdgesMap; } - private getCachedEdgesForRevision(id: { + private async getCachedEdges( + modelRevisionIds: ModelRevisionId[], + fetchViews: boolean = false + ): Promise<[ModelRevisionKey, FdmEdgeWithNode[]][]> { + const cachedEdges = modelRevisionIds.map((id) => this.getCachedEdgesForRevision(id, fetchViews)); + + const edges = await Promise.all(cachedEdges); + + return edges; + } + + private async getCachedEdgesForRevision(id: { modelId: number; revisionId: number; - }): [ModelRevisionKey, FdmEdgeWithNode[]] { + }, fetchViews = false): Promise<[ModelRevisionKey, FdmEdgeWithNode[]]> { const revisionCache = this.getOrCreateRevisionCache(id.modelId, id.revisionId); const revisionKey = createModelRevisionKey(id.modelId, id.revisionId); + + if (fetchViews) { + await revisionCache.fetchViewsForAllEdges(); + } + const cachedRevisionEdges = revisionCache.getAllEdges(); return [revisionKey, cachedRevisionEdges]; @@ -203,13 +220,16 @@ export class FdmNodeCache { } private async getAndCacheRevisionToEdgesMap( - modelRevisionIds: ModelRevisionId[] + modelRevisionIds: ModelRevisionId[], + fetchViews: boolean = false ): Promise> { const revisionIds = modelRevisionIds.map((modelRevisionId) => modelRevisionId.revisionId); const edges = await this.getEdgesForRevisions(revisionIds, this._fdmClient); - const edgesWithViews = await this.getViewsForEdges(edges); + + const edgesWithOptionalViews = fetchViews ? await this.getViewsForEdges(edges) : edges.map((edge) => ({ edge })); + const revisionToEdgesMap = await createRevisionToEdgesMap( - edgesWithViews, + edgesWithOptionalViews, modelRevisionIds, this._cdfClient ); @@ -223,7 +243,7 @@ export class FdmNodeCache { modelId: number, revisionId: number, treeIndex: number - ): Promise { + ): Promise[]> { const revisionCache = this.getOrCreateRevisionCache(modelId, revisionId); return await revisionCache.getClosestParentFdmData(treeIndex); @@ -287,7 +307,7 @@ export class FdmNodeCache { } async function createRevisionToEdgesMap( - edgesWithViews: Array<{ edge: FdmCadEdge; view: Source }>, + edgesWithViews: Array<{ edge: FdmCadEdge; view?: Source }>, modelRevisionIds: ModelRevisionId[], cdfClient: CogniteClient ): Promise> { @@ -306,9 +326,9 @@ async function createRevisionToEdgesMap( const value = createFdmEdgeWithNode( modelRevisionId, + modelNodeIdToNodeMap, edgeWithView.edge, edgeWithView.view, - modelNodeIdToNodeMap ); insertEdgeIntoMapList(value, map, modelRevisionId); @@ -319,9 +339,9 @@ async function createRevisionToEdgesMap( function createFdmEdgeWithNode( modelRevisionId: ModelRevisionId, + modelNodeIdToNodeMap: Map, edge: FdmCadEdge, - view: Source, - modelNodeIdToNodeMap: Map + view?: Source, ): FdmEdgeWithNode { const revisionNodeIdKey = createModelNodeIdKey( modelRevisionId.modelId, @@ -378,7 +398,7 @@ async function createModelNodeIdToNodeMap( } function createRevisionToNodeIdMap( - edgesWithViews: Array<{ edge: FdmCadEdge; view: Source }> + edgesWithViews: Array<{ edge: FdmCadEdge; view?: Source }> ): Map { return edgesWithViews.reduce((revisionNodeIdMap, edgeWithView) => { const { revisionNodeId, revisionId } = edgeWithView.edge.properties; diff --git a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx index 0ec0e844bcf..02e2c7a6363 100644 --- a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx +++ b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx @@ -32,6 +32,7 @@ export const useFdmNodeCache = (): FdmNodeCacheContent => { export const useMappedEdgesForRevisions = ( modelRevisionIds: Array<{ modelId: number; revisionId: number }>, + fetchViews = false, enabled = true ): UseQueryResult => { const content = useFdmNodeCache(); @@ -40,9 +41,10 @@ export const useMappedEdgesForRevisions = ( [ 'reveal', 'react-components', - ...modelRevisionIds.map((modelRevisionId) => modelRevisionId.revisionId.toString()).sort() + ...modelRevisionIds.map((modelRevisionId) => modelRevisionId.revisionId.toString()).sort(), + fetchViews ], - async () => await content.cache.getAllMappingExternalIds(modelRevisionIds), + async () => await content.cache.getAllMappingExternalIds(modelRevisionIds, fetchViews), { staleTime: Infinity, enabled: enabled && modelRevisionIds.length > 0 } ); }; @@ -51,7 +53,7 @@ export const useFdm3dNodeData = ( modelId: number | undefined, revisionId: number | undefined, treeIndex: number | undefined -): UseQueryResult => { +): UseQueryResult[]> => { const content = useFdmNodeCache(); const enableQuery = diff --git a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts index d10afa1d7e4..e2f8d35610c 100644 --- a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts @@ -38,23 +38,21 @@ export class RevisionFdmNodeCache { this._revisionId = revisionId; } - public async getClosestParentFdmData(searchTreeIndex: number): Promise { + public async getClosestParentFdmData(searchTreeIndex: number): Promise[]> { const cachedFdmData = this._treeIndexToFdmEdges.get(searchTreeIndex); - if (cachedFdmData !== undefined) { + if (checkDefinedView(cachedFdmData)) { return cachedFdmData; } - const cachedFdmEdges = this._treeIndexToFdmEdges.get(searchTreeIndex); - - if (cachedFdmEdges !== undefined) { - return await this.getDataWithViewsForFdmEdges(cachedFdmEdges, []); + if (cachedFdmData !== undefined) { + return await this.getDataWithViewsForFdmEdges(cachedFdmData, []); } return await this.findNodeDataFromAncestors(searchTreeIndex); } - private async findNodeDataFromAncestors(treeIndex: TreeIndex): Promise { + private async findNodeDataFromAncestors(treeIndex: TreeIndex): Promise[]> { const { edges, ancestorsWithSameMapping, firstMappedAncestorTreeIndex } = await this.getClosestParentMapping(treeIndex); @@ -64,7 +62,7 @@ export class RevisionFdmNodeCache { const cachedFdmData = this._treeIndexToFdmEdges.get(firstMappedAncestorTreeIndex); - if (cachedFdmData !== undefined) { + if (checkDefinedView(cachedFdmData)) { this.setCacheForNodes(ancestorsWithSameMapping, cachedFdmData); return cachedFdmData; @@ -90,7 +88,7 @@ export class RevisionFdmNodeCache { private async getDataWithViewsForFdmEdges( nodeEdges: Array<{ edge: FdmCadEdge; node: Node3D }>, ancestorsWithSameMapping: Node3D[] - ): Promise { + ): Promise[]> { const nodeInspectionResults = await inspectNodes( this._fdmClient, nodeEdges.map((edge) => edge.edge.startNode) @@ -101,9 +99,9 @@ export class RevisionFdmNodeCache { view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] })); - ancestorsWithSameMapping.forEach((ancestor) => - this._treeIndexToFdmEdges.set(ancestor.treeIndex, dataWithViews) - ); + ancestorsWithSameMapping.forEach((ancestor) => { + this._treeIndexToFdmEdges.set(ancestor.treeIndex, dataWithViews); + }); return dataWithViews; } @@ -180,6 +178,26 @@ export class RevisionFdmNodeCache { return ancestorMappings.edges; } + public async fetchViewsForAllEdges(): Promise { + const allEdges = this.getAllEdges(); + + if (allEdges.length === 0) { + return; + } + + const nodeInspectionResults = await inspectNodes( + this._fdmClient, + allEdges.map((edge) => edge.edge.startNode) + ); + + const dataWithViews = allEdges.map((fdmEdgeWithNode, ind) => ({ + ...fdmEdgeWithNode, + view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] + })); + + this.setCacheForNodes(allEdges.map((edge) => edge.node), dataWithViews); + } + public insertTreeIndexMappings(treeIndex: TreeIndex, edge: FdmEdgeWithNode): void { const edgeArray = this._treeIndexToFdmEdges.get(treeIndex); if (edgeArray === undefined) { @@ -222,3 +240,9 @@ function getAncestorDataForTreeIndex( firstMappedAncestorTreeIndex: treeIndex }; } + +export function checkDefinedView(edges?: FdmEdgeWithNode[]): edges is Required[] { + if (!edges) return false; + + return edges?.every((edge): edge is Required => edge.view !== undefined); +} diff --git a/react-components/src/components/NodeCacheProvider/types.ts b/react-components/src/components/NodeCacheProvider/types.ts index dedd5be190a..630c5cf70e5 100644 --- a/react-components/src/components/NodeCacheProvider/types.ts +++ b/react-components/src/components/NodeCacheProvider/types.ts @@ -6,7 +6,7 @@ import { type EdgeItem, type DmsUniqueIdentifier, type Source } from '../../util import { type InModel3dEdgeProperties } from '../../utilities/globalDataModels'; export type FdmCadEdge = EdgeItem; -export type FdmEdgeWithNode = { edge: FdmCadEdge; node: Node3D; view: Source }; +export type FdmEdgeWithNode = { edge: FdmCadEdge; node: Node3D; view?: Source }; export type ModelId = number; export type RevisionId = number; diff --git a/react-components/stories/HighlightNode.stories.tsx b/react-components/stories/HighlightNode.stories.tsx index 0d8e9c8aa91..92b06c64c0d 100644 --- a/react-components/stories/HighlightNode.stories.tsx +++ b/react-components/stories/HighlightNode.stories.tsx @@ -10,7 +10,9 @@ import { useClickedNodeData, useCameraNavigation, type AddResourceOptions, - type FdmAssetStylingGroup + type FdmAssetStylingGroup, + useMappedEdgesForRevisions, + useReveal } from '../src'; import { Color } from 'three'; import { type ReactElement, useState, useEffect } from 'react'; @@ -61,6 +63,13 @@ const StoryContent = ({ resources }: { resources: AddResourceOptions[] }): React const [stylingGroups, setStylingGroups] = useState([]); const cameraNavigation = useCameraNavigation(); const nodeData = useClickedNodeData(); + const viewer = useReveal(); + const isEnabled = stylingGroups.length > 0; + const {data} = useMappedEdgesForRevisions(viewer.models, isEnabled, isEnabled); + + useEffect(() => { + console.log('MappedEdges', data); + }, [data]); useEffect(() => { if (nodeData?.fdmNode === undefined) { @@ -76,7 +85,10 @@ const StoryContent = ({ resources }: { resources: AddResourceOptions[] }): React style: { cad: DefaultNodeAppearance.Highlighted } } ]); + void cameraNavigation.fitCameraToInstance(nodeData.fdmNode.externalId, nodeData.fdmNode.space); + + console.log('Clicked node data', nodeData); }, [nodeData?.fdmNode]); return ( From 7d98b39f140a1924f145d9b2823d3929896b30c6 Mon Sep 17 00:00:00 2001 From: Savelii Novikov Date: Fri, 8 Sep 2023 16:37:40 +0200 Subject: [PATCH 2/5] Made view fetching optional --- .../useApplyCadModelStyling.tsx | 2 ++ .../NodeCacheProvider/FdmNodeCache.ts | 3 +++ .../NodeCacheProvider/RevisionFdmNodeCache.ts | 25 +++++++++++++------ .../stories/HighlightNode.stories.tsx | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx index 3b84363989c..fee46c57ae7 100644 --- a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx +++ b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx @@ -97,6 +97,8 @@ async function applyStyling( return model.styledNodeCollections.length; } + + console.log('Styling applied', stylingGroups); } async function isEqualOrUpdated( diff --git a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts index 172fc0178be..42cc8a08358 100644 --- a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts @@ -269,6 +269,9 @@ export class FdmNodeCache { revisionIds: number[], fdmClient: FdmSDK ): Promise>> { + if (revisionIds.length === 0) + return []; + const versionedPropertiesKey = `${SYSTEM_3D_EDGE_SOURCE.externalId}/${SYSTEM_3D_EDGE_SOURCE.version}`; const filter = { in: { diff --git a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts index e2f8d35610c..2b078a01c78 100644 --- a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts @@ -63,7 +63,7 @@ export class RevisionFdmNodeCache { const cachedFdmData = this._treeIndexToFdmEdges.get(firstMappedAncestorTreeIndex); if (checkDefinedView(cachedFdmData)) { - this.setCacheForNodes(ancestorsWithSameMapping, cachedFdmData); + this.setSameCacheForNodes(ancestorsWithSameMapping, cachedFdmData); return cachedFdmData; } @@ -79,7 +79,7 @@ export class RevisionFdmNodeCache { return await this.getDataWithViewsForFdmEdges(nodeEdges, ancestorsWithSameMapping); } - private setCacheForNodes(nodes: Node3D[], nodeData: FdmEdgeWithNode[]): void { + private setSameCacheForNodes(nodes: Node3D[], nodeData: FdmEdgeWithNode[]): void { nodes.forEach((node) => { this._treeIndexToFdmEdges.set(node.treeIndex, nodeData); }); @@ -190,19 +190,28 @@ export class RevisionFdmNodeCache { allEdges.map((edge) => edge.edge.startNode) ); - const dataWithViews = allEdges.map((fdmEdgeWithNode, ind) => ({ - ...fdmEdgeWithNode, - view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] - })); - - this.setCacheForNodes(allEdges.map((edge) => edge.node), dataWithViews); + allEdges.forEach((fdmEdgeWithNode, ind) => { + const edgeWithView = { + ...fdmEdgeWithNode, + view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] + } + + this.insertTreeIndexMappings(edgeWithView.node.treeIndex, edgeWithView); + }); } public insertTreeIndexMappings(treeIndex: TreeIndex, edge: FdmEdgeWithNode): void { const edgeArray = this._treeIndexToFdmEdges.get(treeIndex); + const presentEdge = edgeArray?.find((e) => e.node.id === edge.node.id); + if (edgeArray === undefined) { this._treeIndexToFdmEdges.set(treeIndex, [edge]); } else { + if (presentEdge !== undefined) { + presentEdge.view = edge.view; + return; + } + edgeArray.push(edge); } } diff --git a/react-components/stories/HighlightNode.stories.tsx b/react-components/stories/HighlightNode.stories.tsx index 92b06c64c0d..65ae33c56b1 100644 --- a/react-components/stories/HighlightNode.stories.tsx +++ b/react-components/stories/HighlightNode.stories.tsx @@ -65,7 +65,7 @@ const StoryContent = ({ resources }: { resources: AddResourceOptions[] }): React const nodeData = useClickedNodeData(); const viewer = useReveal(); const isEnabled = stylingGroups.length > 0; - const {data} = useMappedEdgesForRevisions(viewer.models, isEnabled, isEnabled); + const {data} = useMappedEdgesForRevisions([{modelId:2231774635735416, revisionId: 912809199849811 }], true); useEffect(() => { console.log('MappedEdges', data); From 8ab78426e69b6d3848ad5a771e5d0d9f7b79e87a Mon Sep 17 00:00:00 2001 From: Savelii Novikov Date: Fri, 8 Sep 2023 16:43:24 +0200 Subject: [PATCH 3/5] Lint fixes --- .../useApplyCadModelStyling.tsx | 2 - .../NodeCacheProvider/FdmNodeCache.ts | 41 +++++++++++-------- .../NodeCacheProvider/NodeCacheProvider.tsx | 2 +- .../NodeCacheProvider/RevisionFdmNodeCache.ts | 20 +++++---- .../stories/HighlightNode.stories.tsx | 15 ++----- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx index fee46c57ae7..3b84363989c 100644 --- a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx +++ b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx @@ -97,8 +97,6 @@ async function applyStyling( return model.styledNodeCollections.length; } - - console.log('Styling applied', stylingGroups); } async function isEqualOrUpdated( diff --git a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts index 42cc8a08358..179cc8b9566 100644 --- a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts @@ -9,7 +9,7 @@ import { type EdgeItem, type FdmSDK } from '../../utilities/FdmSDK'; -import { RevisionFdmNodeCache, checkDefinedView } from './RevisionFdmNodeCache'; +import { RevisionFdmNodeCache } from './RevisionFdmNodeCache'; import { type FdmEdgeWithNode, type FdmCadEdge, @@ -170,7 +170,10 @@ export class FdmNodeCache { const cachedEdges = await this.getCachedEdges(cachedRevisionIds, fetchViews); - const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap(nonCachedRevisionIds, fetchViews); + const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap( + nonCachedRevisionIds, + fetchViews + ); cachedEdges.forEach(([revisionKey, edges]) => { revisionToEdgesMap.set(revisionKey, edges); @@ -182,18 +185,23 @@ export class FdmNodeCache { private async getCachedEdges( modelRevisionIds: ModelRevisionId[], fetchViews: boolean = false - ): Promise<[ModelRevisionKey, FdmEdgeWithNode[]][]> { - const cachedEdges = modelRevisionIds.map((id) => this.getCachedEdgesForRevision(id, fetchViews)); + ): Promise> { + const cachedEdges = modelRevisionIds.map( + async (id) => await this.getCachedEdgesForRevision(id, fetchViews) + ); const edges = await Promise.all(cachedEdges); return edges; } - private async getCachedEdgesForRevision(id: { - modelId: number; - revisionId: number; - }, fetchViews = false): Promise<[ModelRevisionKey, FdmEdgeWithNode[]]> { + private async getCachedEdgesForRevision( + id: { + modelId: number; + revisionId: number; + }, + fetchViews = false + ): Promise<[ModelRevisionKey, FdmEdgeWithNode[]]> { const revisionCache = this.getOrCreateRevisionCache(id.modelId, id.revisionId); const revisionKey = createModelRevisionKey(id.modelId, id.revisionId); @@ -225,9 +233,11 @@ export class FdmNodeCache { ): Promise> { const revisionIds = modelRevisionIds.map((modelRevisionId) => modelRevisionId.revisionId); const edges = await this.getEdgesForRevisions(revisionIds, this._fdmClient); - - const edgesWithOptionalViews = fetchViews ? await this.getViewsForEdges(edges) : edges.map((edge) => ({ edge })); - + + const edgesWithOptionalViews = fetchViews + ? await this.getViewsForEdges(edges) + : edges.map((edge) => ({ edge })); + const revisionToEdgesMap = await createRevisionToEdgesMap( edgesWithOptionalViews, modelRevisionIds, @@ -243,7 +253,7 @@ export class FdmNodeCache { modelId: number, revisionId: number, treeIndex: number - ): Promise[]> { + ): Promise>> { const revisionCache = this.getOrCreateRevisionCache(modelId, revisionId); return await revisionCache.getClosestParentFdmData(treeIndex); @@ -269,8 +279,7 @@ export class FdmNodeCache { revisionIds: number[], fdmClient: FdmSDK ): Promise>> { - if (revisionIds.length === 0) - return []; + if (revisionIds.length === 0) return []; const versionedPropertiesKey = `${SYSTEM_3D_EDGE_SOURCE.externalId}/${SYSTEM_3D_EDGE_SOURCE.version}`; const filter = { @@ -331,7 +340,7 @@ async function createRevisionToEdgesMap( modelRevisionId, modelNodeIdToNodeMap, edgeWithView.edge, - edgeWithView.view, + edgeWithView.view ); insertEdgeIntoMapList(value, map, modelRevisionId); @@ -344,7 +353,7 @@ function createFdmEdgeWithNode( modelRevisionId: ModelRevisionId, modelNodeIdToNodeMap: Map, edge: FdmCadEdge, - view?: Source, + view?: Source ): FdmEdgeWithNode { const revisionNodeIdKey = createModelNodeIdKey( modelRevisionId.modelId, diff --git a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx index 02e2c7a6363..bc973319a9d 100644 --- a/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx +++ b/react-components/src/components/NodeCacheProvider/NodeCacheProvider.tsx @@ -53,7 +53,7 @@ export const useFdm3dNodeData = ( modelId: number | undefined, revisionId: number | undefined, treeIndex: number | undefined -): UseQueryResult[]> => { +): UseQueryResult>> => { const content = useFdmNodeCache(); const enableQuery = diff --git a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts index 2b078a01c78..0ef450d0bce 100644 --- a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts @@ -38,7 +38,9 @@ export class RevisionFdmNodeCache { this._revisionId = revisionId; } - public async getClosestParentFdmData(searchTreeIndex: number): Promise[]> { + public async getClosestParentFdmData( + searchTreeIndex: number + ): Promise>> { const cachedFdmData = this._treeIndexToFdmEdges.get(searchTreeIndex); if (checkDefinedView(cachedFdmData)) { @@ -52,7 +54,9 @@ export class RevisionFdmNodeCache { return await this.findNodeDataFromAncestors(searchTreeIndex); } - private async findNodeDataFromAncestors(treeIndex: TreeIndex): Promise[]> { + private async findNodeDataFromAncestors( + treeIndex: TreeIndex + ): Promise>> { const { edges, ancestorsWithSameMapping, firstMappedAncestorTreeIndex } = await this.getClosestParentMapping(treeIndex); @@ -88,7 +92,7 @@ export class RevisionFdmNodeCache { private async getDataWithViewsForFdmEdges( nodeEdges: Array<{ edge: FdmCadEdge; node: Node3D }>, ancestorsWithSameMapping: Node3D[] - ): Promise[]> { + ): Promise>> { const nodeInspectionResults = await inspectNodes( this._fdmClient, nodeEdges.map((edge) => edge.edge.startNode) @@ -194,8 +198,8 @@ export class RevisionFdmNodeCache { const edgeWithView = { ...fdmEdgeWithNode, view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] - } - + }; + this.insertTreeIndexMappings(edgeWithView.node.treeIndex, edgeWithView); }); } @@ -250,8 +254,10 @@ function getAncestorDataForTreeIndex( }; } -export function checkDefinedView(edges?: FdmEdgeWithNode[]): edges is Required[] { - if (!edges) return false; +export function checkDefinedView( + edges?: FdmEdgeWithNode[] +): edges is Array> { + if (edges === undefined) return false; return edges?.every((edge): edge is Required => edge.view !== undefined); } diff --git a/react-components/stories/HighlightNode.stories.tsx b/react-components/stories/HighlightNode.stories.tsx index 65ae33c56b1..48bb13e40c8 100644 --- a/react-components/stories/HighlightNode.stories.tsx +++ b/react-components/stories/HighlightNode.stories.tsx @@ -10,9 +10,7 @@ import { useClickedNodeData, useCameraNavigation, type AddResourceOptions, - type FdmAssetStylingGroup, - useMappedEdgesForRevisions, - useReveal + type FdmAssetStylingGroup } from '../src'; import { Color } from 'three'; import { type ReactElement, useState, useEffect } from 'react'; @@ -63,13 +61,6 @@ const StoryContent = ({ resources }: { resources: AddResourceOptions[] }): React const [stylingGroups, setStylingGroups] = useState([]); const cameraNavigation = useCameraNavigation(); const nodeData = useClickedNodeData(); - const viewer = useReveal(); - const isEnabled = stylingGroups.length > 0; - const {data} = useMappedEdgesForRevisions([{modelId:2231774635735416, revisionId: 912809199849811 }], true); - - useEffect(() => { - console.log('MappedEdges', data); - }, [data]); useEffect(() => { if (nodeData?.fdmNode === undefined) { @@ -85,9 +76,9 @@ const StoryContent = ({ resources }: { resources: AddResourceOptions[] }): React style: { cad: DefaultNodeAppearance.Highlighted } } ]); - + void cameraNavigation.fitCameraToInstance(nodeData.fdmNode.externalId, nodeData.fdmNode.space); - + console.log('Clicked node data', nodeData); }, [nodeData?.fdmNode]); From e65aab070b4cc67a227304a90cfb7940cde2f007 Mon Sep 17 00:00:00 2001 From: Savelii Novikov Date: Fri, 8 Sep 2023 17:42:54 +0200 Subject: [PATCH 4/5] Small refactor --- .../NodeCacheProvider/FdmNodeCache.ts | 41 +++++++++---------- .../NodeCacheProvider/RevisionFdmNodeCache.ts | 8 ++-- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts index 179cc8b9566..aa552fad204 100644 --- a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts @@ -168,7 +168,11 @@ export class FdmNodeCache { return this._completeRevisions.has(key); }); - const cachedEdges = await this.getCachedEdges(cachedRevisionIds, fetchViews); + if (fetchViews) { + await this.fetchAllViewsForCachedRevisions(cachedRevisionIds); + } + + const cachedEdges = cachedRevisionIds.map((id) => this.getCachedEdgesForRevision(id)); const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap( nonCachedRevisionIds, @@ -182,32 +186,25 @@ export class FdmNodeCache { return revisionToEdgesMap; } - private async getCachedEdges( - modelRevisionIds: ModelRevisionId[], - fetchViews: boolean = false - ): Promise> { - const cachedEdges = modelRevisionIds.map( - async (id) => await this.getCachedEdgesForRevision(id, fetchViews) - ); - - const edges = await Promise.all(cachedEdges); - - return edges; - } - - private async getCachedEdgesForRevision( - id: { + private async fetchAllViewsForCachedRevisions( + revisions: Array<{ modelId: number; revisionId: number; - }, - fetchViews = false - ): Promise<[ModelRevisionKey, FdmEdgeWithNode[]]> { - const revisionCache = this.getOrCreateRevisionCache(id.modelId, id.revisionId); - const revisionKey = createModelRevisionKey(id.modelId, id.revisionId); + }> + ): Promise { + for (const revision of revisions) { + const revisionCache = this.getOrCreateRevisionCache(revision.modelId, revision.revisionId); - if (fetchViews) { await revisionCache.fetchViewsForAllEdges(); } + } + + private getCachedEdgesForRevision(id: { + modelId: number; + revisionId: number; + }): [ModelRevisionKey, FdmEdgeWithNode[]] { + const revisionCache = this.getOrCreateRevisionCache(id.modelId, id.revisionId); + const revisionKey = createModelRevisionKey(id.modelId, id.revisionId); const cachedRevisionEdges = revisionCache.getAllEdges(); diff --git a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts index 0ef450d0bce..cda91e0df6b 100644 --- a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts @@ -183,18 +183,18 @@ export class RevisionFdmNodeCache { } public async fetchViewsForAllEdges(): Promise { - const allEdges = this.getAllEdges(); + const allEdgesWithoutView = this.getAllEdges().filter((edge) => edge.view === undefined); - if (allEdges.length === 0) { + if (allEdgesWithoutView.length === 0) { return; } const nodeInspectionResults = await inspectNodes( this._fdmClient, - allEdges.map((edge) => edge.edge.startNode) + allEdgesWithoutView.map((edge) => edge.edge.startNode) ); - allEdges.forEach((fdmEdgeWithNode, ind) => { + allEdgesWithoutView.forEach((fdmEdgeWithNode, ind) => { const edgeWithView = { ...fdmEdgeWithNode, view: nodeInspectionResults.items[ind].inspectionResults.involvedViewsAndContainers.views[0] From a1a60f78716e77fbf7b5c394543b21825107d190 Mon Sep 17 00:00:00 2001 From: Savelii Novikov Date: Mon, 11 Sep 2023 12:22:28 +0200 Subject: [PATCH 5/5] Removed default value and small optimisation --- .../src/components/NodeCacheProvider/FdmNodeCache.ts | 4 ++-- .../src/components/NodeCacheProvider/RevisionFdmNodeCache.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts index aa552fad204..6836ce6b5fe 100644 --- a/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/FdmNodeCache.ts @@ -122,7 +122,7 @@ export class FdmNodeCache { const fdmKeySet = new Set(uniqueIds.map((id) => createFdmKey(id.space, id.externalId))); - const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap(modelRevisions); + const revisionToEdgesMap = await this.getAndCacheRevisionToEdgesMap(modelRevisions, false); const modelDataPromises = modelRevisions.map(async ({ modelId, revisionId }) => { const revisionKey = createModelRevisionKey(modelId, revisionId); @@ -226,7 +226,7 @@ export class FdmNodeCache { private async getAndCacheRevisionToEdgesMap( modelRevisionIds: ModelRevisionId[], - fetchViews: boolean = false + fetchViews: boolean ): Promise> { const revisionIds = modelRevisionIds.map((modelRevisionId) => modelRevisionId.revisionId); const edges = await this.getEdgesForRevisions(revisionIds, this._fdmClient); diff --git a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts index cda91e0df6b..0dc7678ef6c 100644 --- a/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts +++ b/react-components/src/components/NodeCacheProvider/RevisionFdmNodeCache.ts @@ -206,11 +206,12 @@ export class RevisionFdmNodeCache { public insertTreeIndexMappings(treeIndex: TreeIndex, edge: FdmEdgeWithNode): void { const edgeArray = this._treeIndexToFdmEdges.get(treeIndex); - const presentEdge = edgeArray?.find((e) => e.node.id === edge.node.id); if (edgeArray === undefined) { this._treeIndexToFdmEdges.set(treeIndex, [edge]); } else { + const presentEdge = edgeArray?.find((e) => e.node.id === edge.node.id); + if (presentEdge !== undefined) { presentEdge.view = edge.view; return;