|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | +import { |
| 9 | + REACTIVE_LVIEW_CONSUMER_NODE, |
| 10 | + ReactiveLViewConsumer, |
| 11 | + TEMPORARY_CONSUMER_NODE, |
| 12 | +} from '../reactive_lview_consumer'; |
| 13 | +import {assertTNode, assertLView} from '../assert'; |
| 14 | +import {getFrameworkDIDebugData} from '../debug/framework_injector_profiler'; |
| 15 | +import {NodeInjector, getNodeInjectorTNode, getNodeInjectorLView} from '../di'; |
| 16 | +import {REACTIVE_TEMPLATE_CONSUMER, HOST, LView} from '../interfaces/view'; |
| 17 | +import {EffectNode, EffectRefImpl, ROOT_EFFECT_NODE, VIEW_EFFECT_NODE} from '../reactivity/effect'; |
| 18 | +import {Injector} from '../../di/injector'; |
| 19 | +import {R3Injector} from '../../di/r3_injector'; |
| 20 | +import {throwError} from '../../util/assert'; |
| 21 | +import { |
| 22 | + ComputedNode, |
| 23 | + ReactiveNode, |
| 24 | + SIGNAL, |
| 25 | + SIGNAL_NODE, |
| 26 | + SignalNode, |
| 27 | +} from '@angular/core/primitives/signals'; |
| 28 | + |
| 29 | +export interface DebugSignalGraphNode { |
| 30 | + kind: string; |
| 31 | + label?: string; |
| 32 | + value?: unknown; |
| 33 | +} |
| 34 | + |
| 35 | +export interface DebugSignalGraphEdge { |
| 36 | + /** |
| 37 | + * Index of a signal node in the `nodes` array that is a consumer of the signal produced by the producer node. |
| 38 | + */ |
| 39 | + consumer: number; |
| 40 | + |
| 41 | + /** |
| 42 | + * Index of a signal node in the `nodes` array that is a producer of the signal consumed by the consumer node. |
| 43 | + */ |
| 44 | + producer: number; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A debug representation of the signal graph. |
| 49 | + */ |
| 50 | +export interface DebugSignalGraph { |
| 51 | + nodes: DebugSignalGraphNode[]; |
| 52 | + edges: DebugSignalGraphEdge[]; |
| 53 | +} |
| 54 | + |
| 55 | +function isComputedNode(node: ReactiveNode): node is ComputedNode<unknown> { |
| 56 | + return node.kind === 'computed'; |
| 57 | +} |
| 58 | + |
| 59 | +function isTemplateEffectNode(node: ReactiveNode): node is ReactiveLViewConsumer { |
| 60 | + return node.kind === 'template'; |
| 61 | +} |
| 62 | + |
| 63 | +function isEffectNode(node: ReactiveNode): node is EffectNode { |
| 64 | + return node.kind === 'effect'; |
| 65 | +} |
| 66 | + |
| 67 | +function isSignalNode(node: ReactiveNode): node is SignalNode<unknown> { |
| 68 | + return node.kind === 'signal'; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * |
| 73 | + * @param injector |
| 74 | + * @returns Template consumer of given NodeInjector |
| 75 | + */ |
| 76 | +function getTemplateConsumer(injector: NodeInjector): ReactiveLViewConsumer | null { |
| 77 | + const tNode = getNodeInjectorTNode(injector)!; |
| 78 | + assertTNode(tNode); |
| 79 | + const lView = getNodeInjectorLView(injector)!; |
| 80 | + assertLView(lView); |
| 81 | + const templateLView = lView[tNode.index]!; |
| 82 | + assertLView(templateLView); |
| 83 | + |
| 84 | + return templateLView[REACTIVE_TEMPLATE_CONSUMER]; |
| 85 | +} |
| 86 | + |
| 87 | +function getNodesAndEdgesFromSignalMap(signalMap: ReadonlyMap<ReactiveNode, ReactiveNode[]>): { |
| 88 | + nodes: DebugSignalGraphNode[]; |
| 89 | + edges: DebugSignalGraphEdge[]; |
| 90 | +} { |
| 91 | + const nodes = Array.from(signalMap.keys()); |
| 92 | + const debugSignalGraphNodes: DebugSignalGraphNode[] = []; |
| 93 | + const edges: DebugSignalGraphEdge[] = []; |
| 94 | + |
| 95 | + for (const [consumer, producers] of signalMap.entries()) { |
| 96 | + const consumerIndex = nodes.indexOf(consumer); |
| 97 | + |
| 98 | + // collect node |
| 99 | + if (isComputedNode(consumer) || isSignalNode(consumer)) { |
| 100 | + debugSignalGraphNodes.push({ |
| 101 | + label: consumer.debugName, |
| 102 | + value: consumer.value, |
| 103 | + kind: consumer.kind, |
| 104 | + }); |
| 105 | + } else if (isTemplateEffectNode(consumer)) { |
| 106 | + debugSignalGraphNodes.push({ |
| 107 | + label: consumer.debugName ?? consumer.lView?.[HOST]?.tagName?.toLowerCase?.(), |
| 108 | + kind: consumer.kind, |
| 109 | + }); |
| 110 | + } else if (isEffectNode(consumer)) { |
| 111 | + debugSignalGraphNodes.push({ |
| 112 | + label: consumer.debugName, |
| 113 | + kind: consumer.kind, |
| 114 | + }); |
| 115 | + } else { |
| 116 | + debugSignalGraphNodes.push({ |
| 117 | + label: consumer.debugName, |
| 118 | + kind: consumer.kind, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + // collect edges for node |
| 123 | + for (const producer of producers) { |
| 124 | + edges.push({consumer: consumerIndex, producer: nodes.indexOf(producer)}); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return {nodes: debugSignalGraphNodes, edges}; |
| 129 | +} |
| 130 | + |
| 131 | +function extractEffectsFromInjector(injector: Injector): ReactiveNode[] { |
| 132 | + let diResolver: Injector | LView<unknown> = injector; |
| 133 | + if (injector instanceof NodeInjector) { |
| 134 | + const lView = getNodeInjectorLView(injector)!; |
| 135 | + diResolver = lView; |
| 136 | + } |
| 137 | + |
| 138 | + const resolverToEffects = getFrameworkDIDebugData().resolverToEffects as Map< |
| 139 | + Injector | LView<unknown>, |
| 140 | + EffectRefImpl[] |
| 141 | + >; |
| 142 | + const effects = resolverToEffects.get(diResolver) ?? []; |
| 143 | + |
| 144 | + return effects.map((effect: EffectRefImpl) => effect[SIGNAL]); |
| 145 | +} |
| 146 | + |
| 147 | +function extractSignalNodesAndEdgesFromRoots( |
| 148 | + nodes: ReactiveNode[], |
| 149 | + signalDependenciesMap: Map<ReactiveNode, ReactiveNode[]> = new Map(), |
| 150 | +): Map<ReactiveNode, ReactiveNode[]> { |
| 151 | + for (const node of nodes) { |
| 152 | + if (signalDependenciesMap.has(node)) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + const producerNodes = (node.producerNode ?? []) as ReactiveNode[]; |
| 157 | + signalDependenciesMap.set(node, producerNodes); |
| 158 | + extractSignalNodesAndEdgesFromRoots(producerNodes, signalDependenciesMap); |
| 159 | + } |
| 160 | + |
| 161 | + return signalDependenciesMap; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Returns a debug representation of the signal graph for the given injector. |
| 166 | + * |
| 167 | + * Currently only supports element injectors. Starts by discovering the consumer nodes |
| 168 | + * and then traverses their producer nodes to build the signal graph. |
| 169 | + * |
| 170 | + * @param injector The injector to get the signal graph for. |
| 171 | + * @returns A debug representation of the signal graph. |
| 172 | + * @throws If the injector is an environment injector. |
| 173 | + */ |
| 174 | +export function getSignalGraph(injector: Injector): DebugSignalGraph { |
| 175 | + let templateConsumer: ReactiveLViewConsumer | null = null; |
| 176 | + |
| 177 | + if (!(injector instanceof NodeInjector) && !(injector instanceof R3Injector)) { |
| 178 | + return throwError('getSignalGraph must be called with a NodeInjector or R3Injector'); |
| 179 | + } |
| 180 | + |
| 181 | + if (injector instanceof NodeInjector) { |
| 182 | + templateConsumer = getTemplateConsumer(injector as NodeInjector); |
| 183 | + } |
| 184 | + |
| 185 | + const nonTemplateEffectNodes = extractEffectsFromInjector(injector); |
| 186 | + |
| 187 | + const signalNodes = templateConsumer |
| 188 | + ? [templateConsumer, ...nonTemplateEffectNodes] |
| 189 | + : nonTemplateEffectNodes; |
| 190 | + |
| 191 | + const signalDependenciesMap = extractSignalNodesAndEdgesFromRoots(signalNodes); |
| 192 | + |
| 193 | + return getNodesAndEdgesFromSignalMap(signalDependenciesMap); |
| 194 | +} |
0 commit comments