A graph traverser that records what was asked of `getNodesByIds`.
(nodes: Node[], edges: Edge[], honourKinds: boolean)
| 721 | |
| 722 | /** A graph traverser that records what was asked of `getNodesByIds`. */ |
| 723 | function counted(nodes: Node[], edges: Edge[], honourKinds: boolean) { |
| 724 | const byId = new Map(nodes.map((n) => [n.id, n])); |
| 725 | const asked: Array<{ ids: number; kinds: readonly string[] | undefined }> = []; |
| 726 | const q = { |
| 727 | getNodeById: (id: string) => byId.get(id) ?? null, |
| 728 | getNodesByIds: (ids: readonly string[], kinds?: readonly string[]) => { |
| 729 | asked.push({ ids: ids.length, kinds }); |
| 730 | const m = new Map<string, Node>(); |
| 731 | for (const id of ids) { |
| 732 | const n = byId.get(id); |
| 733 | if (n && (!honourKinds || !kinds || kinds.includes(n.kind))) m.set(id, n); |
| 734 | } |
| 735 | return m; |
| 736 | }, |
| 737 | getOutgoingEdges: (source: string, kinds?: string[]) => edges.filter((e) => e.source === source && (!kinds || kinds.includes(e.kind))), |
| 738 | // With `honourKinds`, as the database answers it: no edge to a node of a kind not asked for. |
| 739 | getOutgoingEdgesToKinds: (source: string, kinds: string[] | undefined, nodeKinds: readonly string[]) => |
| 740 | edges.filter((e) => e.source === source && (!kinds || kinds.includes(e.kind)) && |
| 741 | (!honourKinds || !byId.has(e.target) || nodeKinds.includes(byId.get(e.target)!.kind))), |
| 742 | getIncomingEdges: (target: string, kinds?: string[]) => edges.filter((e) => e.target === target && (!kinds || kinds.includes(e.kind))), |
| 743 | }; |
| 744 | return { traverser: new GraphTraverser(q as never), asked }; |
| 745 | } |
| 746 | |
| 747 | const site = (e: Edge) => `${e.source}|${e.target}|${e.kind}|${e.line ?? -1}|${e.column ?? -1}`; |
| 748 |