Skip to content

Commit 571c1c0

Browse files
committed
fix(overlay): keep the host tree order when walking it
Copy the roots and children before reversing them in hostBySelector and the NgRx collector, test both and the signal graph over a HostTree, and list the signal graph among the collectors that walk the host tree.
1 parent 38cf8a6 commit 571c1c0

5 files changed

Lines changed: 49 additions & 7 deletions

File tree

‎docs/CONTEXT.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ One inspector's page-side code inside the overlay, such as `forms-collector.ts`
2121
_Avoid_: scraper, probe, watcher
2222

2323
**Host tree**:
24-
The tree Angular rendered into, as the component, injector and NgRx collectors walk it: roots, children, parent and a tag for each host (`HostTree` in `host-tree.ts`). In the browser it is the DOM, through `domTree()`; a platform without a DOM describes its own views.
24+
The tree Angular rendered into, as the component, injector, signal graph and NgRx collectors walk it: roots, children, parent and a tag for each host (`HostTree` in `host-tree.ts`). In the browser it is the DOM, through `domTree()`; a platform without a DOM describes its own views.
2525
_Avoid_: view tree, render tree, DOM (when the code does not depend on it)
2626

2727
**Push**:

‎docs/contributing/coding-standards.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The overlay runs inside the user's app, so it must be correct, cheap and invisib
2929

3030
- **Use Angular's debug APIs**, not guesses: `ng.getComponent`, `ng.getDirectives`, `ng.getDirectiveMetadata`, `ng.getListeners`, `ng.getInjector`, `ng.ɵgetInjectorProviders`, `ng.ɵgetInjectorResolutionPath`, `ng.ɵgetDependenciesFromInjectable`, `ng.ɵgetSignalGraph`. Check the shapes in `node_modules/@angular/core/fesm2022` before relying on a field.
3131
- **Never write to the app's DOM.** Give elements and objects ids through a `WeakMap` (`element-id.ts`, `idFor` in `injector-tree.ts`), so ids stay stable between pushes and selection survives a refresh.
32-
- **Walk the host tree, not the DOM**, in the component, injector and NgRx collectors. Take a `HostTree` (`host-tree.ts`) that defaults to `domTree()`, so the same collector runs on a platform without a DOM.
32+
- **Walk the host tree, not the DOM**, in the component, injector, signal graph and NgRx collectors. Take a `HostTree` (`host-tree.ts`) that defaults to `domTree()`, so the same collector runs on a platform without a DOM.
3333
- **Strip bundler prefixes** from class names (`className()` in `injector-tree.ts`), so `_App` shows as `App`.
3434
- **Don't match `_nghost-*` or `_ngcontent-*` attributes** with exact selectors; their names carry a suffix. Start from `[ng-version]` roots and the debug APIs.
3535
- **Keep pushes cheap.** Skip unchanged reports (compare with the last JSON sent), re-send now and then so the server doesn't expire the page, and avoid `querySelectorAll('*')` on a timer; cache and rescan on a `MutationObserver` signal.

‎packages/ng-devtools/src/__tests__/host-tree-views.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { describe, expect, it } from 'vitest';
22
import { collectComponentTree, hostPath, type ComponentDebugNg } from '../component-tree.ts';
33
import { elementId } from '../element-id.ts';
4-
import type { HostTree } from '../host-tree.ts';
4+
import { hostBySelector, type HostTree } from '../host-tree.ts';
55
import { collectInjectorTree } from '../injector-tree.ts';
66
import { createNgrxCollector } from '../ngrx-collector.ts';
7+
import { collectSignalGraph, type SignalDebugNg } from '../signal-graph.ts';
78

89
interface View {
910
type: string;
@@ -105,4 +106,45 @@ describe('collectors over a non-DOM host tree', () => {
105106
expect(seen.slice(0, 5)).toEqual(['App', 'List', 'StackLayout', 'Row', 'Row']);
106107
collector.stop();
107108
});
109+
110+
it('finds a host by selector without reordering the tree', () => {
111+
const withSelectors: HostTree<View> = {
112+
...tree,
113+
selector: (host) => {
114+
const parent = host.parent;
115+
const own = parent
116+
? `${host.type}:nth-child(${parent.children.indexOf(host) + 1})`
117+
: host.type;
118+
return parent ? `${withSelectors.selector!(parent)} > ${own}` : own;
119+
},
120+
};
121+
const order = () => list.children[0]!.children.map((host) => host === first);
122+
expect(order()).toEqual([true, false]);
123+
expect(
124+
hostBySelector(
125+
withSelectors,
126+
'App > List:nth-child(1) > StackLayout:nth-child(1) > Row:nth-child(2)',
127+
),
128+
).toBe(second);
129+
expect(hostBySelector(withSelectors, 'App > Nope')).toBeNull();
130+
expect(order()).toEqual([true, false]);
131+
expect(hostBySelector(tree, 'App')).toBeNull();
132+
});
133+
134+
it('collects the signal graph of a host found by id', () => {
135+
const signals: SignalDebugNg<View> = {
136+
...ng,
137+
ɵgetSignalGraph: (injector) =>
138+
(injector as { host?: View }).host === second
139+
? {
140+
nodes: [{ id: '2', kind: 'signal', label: 'label', epoch: 1, value: 'second' }],
141+
edges: [],
142+
}
143+
: { nodes: [], edges: [] },
144+
};
145+
const graph = collectSignalGraph(signals, { id: elementId(second) }, tree)!;
146+
expect(graph.source).toBe('selected');
147+
expect(graph.component).toMatchObject({ name: 'Row', path: 'App > List > Row[2]' });
148+
expect(graph.nodes.map((n) => n.id)).toEqual(['2']);
149+
});
108150
});

‎packages/ng-devtools/src/host-tree.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ export function documentTree<H extends object>(): HostTree<H> {
132132

133133
export function hostBySelector<H extends object>(tree: HostTree<H>, selector: string): H | null {
134134
if (!tree.selector) return null;
135-
const stack = tree.roots().reverse();
135+
const stack = [...tree.roots()].reverse();
136136
while (stack.length) {
137137
const host = stack.pop()!;
138138
if (tree.selector(host) === selector) return host;
139-
stack.push(...tree.children(host).reverse());
139+
stack.push(...[...tree.children(host)].reverse());
140140
}
141141
return null;
142142
}

‎packages/ng-devtools/src/ngrx-collector.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ function stripped(token: unknown): string {
109109

110110
function componentElements<H extends object>(ng: NgrxDebugNg<H>, tree: HostTree<H>): H[] {
111111
const out: H[] = [];
112-
const stack = tree.roots().reverse();
112+
const stack = [...tree.roots()].reverse();
113113
while (stack.length) {
114114
const el = stack.pop()!;
115115
if (read(() => !!ng.getComponent?.(el), false)) out.push(el);
116-
stack.push(...tree.children(el).reverse());
116+
stack.push(...[...tree.children(el)].reverse());
117117
}
118118
return out;
119119
}

0 commit comments

Comments
 (0)