The walk as it was: every neighbour of a step fetched before any is looked at.
(nodes: Node[], edges: Edge[], startId: string, options: {
limit?: number; maxDepth?: number; direction?: 'outgoing' | 'incoming' | 'both'; nodeKinds?: Node['kind'][]; edgeKinds?: Edge['kind'][];
})
| 669 | describe('traverseBFS fetches only the neighbours it can keep', () => { |
| 670 | /** The walk as it was: every neighbour of a step fetched before any is looked at. */ |
| 671 | function fetchAllFirst(nodes: Node[], edges: Edge[], startId: string, options: { |
| 672 | limit?: number; maxDepth?: number; direction?: 'outgoing' | 'incoming' | 'both'; nodeKinds?: Node['kind'][]; edgeKinds?: Edge['kind'][]; |
| 673 | }): { nodes: string[]; edges: string[] } { |
| 674 | const byId = new Map(nodes.map((n) => [n.id, n])); |
| 675 | const opts = { limit: 1000, maxDepth: Infinity, direction: 'outgoing' as const, nodeKinds: [] as Node['kind'][], edgeKinds: [] as Edge['kind'][], ...options }; |
| 676 | const site = (e: Edge) => `${e.source}|${e.target}|${e.kind}|${e.line ?? -1}|${e.column ?? -1}`; |
| 677 | const of = (list: Edge[]) => (opts.edgeKinds.length > 0 ? list.filter((e) => opts.edgeKinds.includes(e.kind)) : list); |
| 678 | const adjacent = (id: string): Edge[] => { |
| 679 | const out = of(edges.filter((e) => e.source === id)); |
| 680 | const into = of(edges.filter((e) => e.target === id)); |
| 681 | return opts.direction === 'outgoing' ? out : opts.direction === 'incoming' ? into : [...out, ...into]; |
| 682 | }; |
| 683 | const kept = new Map<string, Node>([[startId, byId.get(startId)!]]); |
| 684 | const keptEdges: Edge[] = []; |
| 685 | const visited = new Set<string>(); |
| 686 | const enqueued = new Set<string>([startId]); |
| 687 | const seen = new Set<string>(); |
| 688 | const queue: Array<{ node: Node; depth: number }> = [{ node: byId.get(startId)!, depth: 0 }]; |
| 689 | while (queue.length > 0 && kept.size < opts.limit) { |
| 690 | const { node, depth } = queue.shift()!; |
| 691 | if (visited.has(node.id)) continue; |
| 692 | visited.add(node.id); |
| 693 | if (depth >= opts.maxDepth) continue; |
| 694 | const adjacentEdges = adjacent(node.id); |
| 695 | const priority = (e: Edge) => (e.kind === 'contains' ? 0 : e.kind === 'calls' ? 1 : 2); |
| 696 | adjacentEdges.sort((a, b) => priority(a) - priority(b)); |
| 697 | const fetched = new Map<string, Node>(); |
| 698 | for (const e of adjacentEdges) { |
| 699 | const id = e.source === node.id ? e.target : e.source; |
| 700 | if (!visited.has(id) && !enqueued.has(id) && byId.has(id)) fetched.set(id, byId.get(id)!); |
| 701 | } |
| 702 | for (const e of adjacentEdges) { |
| 703 | const id = e.source === node.id ? e.target : e.source; |
| 704 | const next = fetched.get(id) ?? kept.get(id); |
| 705 | if (!next) continue; |
| 706 | if (opts.nodeKinds.length > 0 && !opts.nodeKinds.includes(next.kind)) continue; |
| 707 | if (!visited.has(id) && !enqueued.has(id)) { |
| 708 | if (kept.size >= opts.limit) continue; |
| 709 | enqueued.add(id); |
| 710 | kept.set(id, next); |
| 711 | queue.push({ node: next, depth: depth + 1 }); |
| 712 | } |
| 713 | if (!seen.has(site(e))) { |
| 714 | seen.add(site(e)); |
| 715 | keptEdges.push(e); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | return { nodes: [...kept.keys()], edges: keptEdges.map(site) }; |
| 720 | } |
| 721 | |
| 722 | /** A graph traverser that records what was asked of `getNodesByIds`. */ |
| 723 | function counted(nodes: Node[], edges: Edge[], honourKinds: boolean) { |