MCPcopy Index your code
hub / github.com/simstudioai/sim / calculateSubflowDepths

Function calculateSubflowDepths

apps/sim/lib/workflows/autolayout/utils.ts:584–623  ·  view source on GitHub ↗
(
  blocks: Record<string, BlockState>,
  edges: Edge[],
  assignLayersFn: (blocks: Record<string, BlockState>, edges: Edge[]) => Map<string, GraphNode>
)

Source from the content-addressed store, hash-verified

582 * @returns Map of container block IDs to their internal layer depth
583 */
584export function calculateSubflowDepths(
585 blocks: Record<string, BlockState>,
586 edges: Edge[],
587 assignLayersFn: (blocks: Record<string, BlockState>, edges: Edge[]) => Map<string, GraphNode>
588): Map<string, number> {
589 const depths = new Map<string, number>()
590 const { children } = getBlocksByParent(blocks)
591
592 for (const [containerId, childIds] of children.entries()) {
593 if (childIds.length === 0) {
594 depths.set(containerId, 1)
595 continue
596 }
597
598 const childBlocks: Record<string, BlockState> = {}
599 const layoutChildIds = filterLayoutEligibleBlockIds(childIds, blocks)
600 for (const childId of layoutChildIds) {
601 childBlocks[childId] = blocks[childId]
602 }
603
604 const childEdges = edges.filter(
605 (edge) => layoutChildIds.includes(edge.source) && layoutChildIds.includes(edge.target)
606 )
607
608 if (Object.keys(childBlocks).length === 0) {
609 depths.set(containerId, 1)
610 continue
611 }
612
613 const childNodes = assignLayersFn(childBlocks, childEdges)
614 let maxLayer = 0
615 for (const node of childNodes.values()) {
616 maxLayer = Math.max(maxLayer, node.layer)
617 }
618
619 depths.set(containerId, Math.max(maxLayer + 1, 1))
620 }
621
622 return depths
623}
624
625/**
626 * Layout function type for preparing container dimensions.

Callers 2

applyTargetedLayoutFunction · 0.90
applyAutoLayoutFunction · 0.90

Calls 3

getBlocksByParentFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected