feat(devtools): add ability to toggle control flow and defer blocks

Add options for toggling @for and @defer blocks in the settings.
This commit is contained in:
Georgi Serev
2026-09-23 08:48:31 -07:00
committed by GitHub
parent cff4402cb9
commit 1881f9dc0d
13 changed files with 110 additions and 2 deletions
@@ -155,6 +155,9 @@ export const subscribeToClientEvents = (
.changeDetection$.pipe(debounceTime(250))
.subscribe(() => messageBus.emit('componentTreeDirty'));
});
getConfig().onChange('deferBlocks', () => messageBus.emit('componentTreeDirty'));
getConfig().onChange('forBlocks', () => messageBus.emit('componentTreeDirty'));
}
};
@@ -17,6 +17,8 @@ class DevtoolsConfigState {
hydrationOverlays: false,
cdHighlighting: false,
cdDataStream: false,
deferBlocks: false,
forBlocks: false,
};
private readonly listeners = new Map<keyof DevtoolsConfig, ((v: any) => void)[]>();
@@ -15,6 +15,7 @@ ts_project(
"//:node_modules/@types/semver",
"//:node_modules/semver",
"//devtools/projects/ng-devtools-backend/src/lib:version",
"//devtools/projects/ng-devtools-backend/src/lib/config",
"//devtools/projects/ng-devtools-backend/src/lib/directive-forest/utils:control-flow",
"//devtools/projects/ng-devtools-backend/src/lib/shared:interfaces",
"//devtools/projects/ng-devtools-backend/src/lib/shared/ng-debug-api",
@@ -32,6 +33,7 @@ ts_test_library(
deps = [
":tree-strategies",
"//:node_modules/@angular/core",
"//devtools/projects/ng-devtools-backend/src/lib/config",
],
)
@@ -14,6 +14,7 @@ import {
ɵControlFlowBlockType as ControlFlowBlockType,
} from '@angular/core';
import {RTreeStrategy} from './render-tree';
import {getConfig} from '../../config/config';
describe('render tree extraction', () => {
let treeStrategy: RTreeStrategy;
@@ -29,6 +30,9 @@ describe('render tree extraction', () => {
directiveMetadataMap = new Map();
controlFlowBlocksMap = new Map();
// Control flow blocks are filtered out by default.
getConfig().set({deferBlocks: true, forBlocks: true});
(window as any).ng = {
getDirectiveMetadata(dir: any): DirectiveDebugMetadata | null {
return directiveMetadataMap.get(dir) ?? null;
@@ -45,7 +49,10 @@ describe('render tree extraction', () => {
} satisfies Partial<FrameworkAgnosticGlobalUtils>;
});
afterEach(() => delete (window as any).ng);
afterEach(() => {
delete (window as any).ng;
getConfig().set({deferBlocks: false, forBlocks: false});
});
it('should detect Angular Ivy apps', () => {
expect(treeStrategy.supports()).toBeTrue();
@@ -238,4 +245,40 @@ describe('render tree extraction', () => {
}),
);
});
it('should skip control flow blocks that are disabled in the config', () => {
// Represent:
//
// <app>
// @for (...) {
// <child />
// }
// </app>
const appNode = document.createElement('app');
const forHostNode = document.createElement('comment');
const childNode = document.createElement('child');
appNode.appendChild(forHostNode);
appNode.appendChild(childNode);
componentMap.set(appNode, {});
componentMap.set(childNode, {});
controlFlowBlocksMap.set(appNode, [
{
type: ControlFlowBlockType.For,
hostNode: forHostNode,
rootNodes: [childNode],
items: [],
},
]);
getConfig().set({forBlocks: false});
const rtree = treeStrategy.build(appNode);
// The child component is a direct descendant of the app root.
expect(rtree[0].children.map((c) => c.tagName)).toEqual(['child']);
});
});
@@ -18,9 +18,11 @@ import {isCustomElement} from '../../shared/utils/general';
import {
ControlFlowBlocksIterator,
createControlFlowTreeNode,
blocksFilter,
isControlFlowBlock,
} from '../utils/control-flow';
import {AngularDevtoolsError} from '../../shared/utils/error';
import {getConfig} from '../../config/config';
interface TreeExtractionContext {
blocksIterator: ControlFlowBlocksIterator;
@@ -186,7 +188,10 @@ export class RTreeStrategy {
build(element: Element, rootId: number = 0): ComponentTreeNode[] {
const ng = ngDebugClient();
const controlFlowBlocks = ng.ɵgetControlFlowBlocks?.(element) ?? [];
const controlFlowBlocks = (ng.ɵgetControlFlowBlocks?.(element) ?? []).filter((block) =>
blocksFilter(block, getConfig().snapshot()),
);
const ctx: TreeExtractionContext = {
blocksIterator: new ControlFlowBlocksIterator(controlFlowBlocks),
rootId,
@@ -17,6 +17,7 @@ import {
ForLoopBlock,
DeferBlock,
RenderedDeferBlock,
DevtoolsConfig,
} from '../../../../../protocol';
import {ComponentTreeNode} from '../../shared/interfaces';
import {serializeValue} from '../../shared/state-serializer/state-serializer';
@@ -106,6 +107,18 @@ export class ControlFlowBlocksIterator<
}
}
export function blocksFilter(block: ControlFlowBlockInternal, config: DevtoolsConfig) {
if (!config.forBlocks && block.type === ControlFlowBlockTypeInternal.For) {
return false;
}
if (!config.deferBlocks && block.type === ControlFlowBlockTypeInternal.Defer) {
return false;
}
return true;
}
function groupTriggers(triggers: string[]) {
const defer: string[] = [];
const hydrate: string[] = [];
@@ -122,6 +122,8 @@ export function applyMigrations(
// since the SettingsStore is responsible for the initial/default values.
'highlight_change_detection@profiling': undefined as any,
'show_cd_in_explorer@components': undefined as any,
'show_defer_blocks@components': undefined as any,
'show_for_blocks@components': undefined as any,
};
dataCopy = newData as unknown as {[key: string]: unknown};
@@ -42,6 +42,8 @@ describe('applyMigrations', () => {
'show_hydration_overlays@components': false,
'highlight_change_detection@profiling': undefined as any,
'show_cd_in_explorer@components': undefined as any,
'show_defer_blocks@components': undefined as any,
'show_for_blocks@components': undefined as any,
} satisfies SettingsDataV2);
});
});
@@ -35,6 +35,8 @@ export interface SettingsDataV2 extends SettingsDataBase {
[DATA_VERSION_KEY]: 2;
'show_comment_nodes@components': boolean;
'show_cd_in_explorer@components': boolean;
'show_defer_blocks@components': boolean;
'show_for_blocks@components': boolean;
'performance_track@profiling': boolean;
'highlight_change_detection@profiling': boolean;
'theme@general': ThemePreference;
@@ -27,6 +27,18 @@ export class Settings {
initialValue: false,
});
readonly showDeferBlocks = this.settingsStore.create({
key: 'show_defer_blocks',
category: 'components',
initialValue: true,
});
readonly showForBlocks = this.settingsStore.create({
key: 'show_for_blocks',
category: 'components',
initialValue: true,
});
readonly performanceTrack = this.settingsStore.create({
key: 'performance_track',
category: 'profiling',
@@ -26,6 +26,24 @@
/>
<label for="show-comment-nodes">Show comment nodes</label>
</li>
<li>
<input
id="show-defer-blocks"
type="checkbox"
(change)="settings.showDeferBlocks.set($event.target.checked)"
[checked]="settings.showDeferBlocks()"
/>
<label for="show-defer-blocks">Show &#64;defer blocks</label>
</li>
<li>
<input
id="show-for-blocks"
type="checkbox"
(change)="settings.showForBlocks.set($event.target.checked)"
[checked]="settings.showForBlocks()"
/>
<label for="show-for-blocks">Show &#64;for blocks</label>
</li>
<li>
<input
id="show-hydration-overlays"
@@ -124,6 +124,8 @@ export class DevToolsComponent implements OnDestroy {
hydrationOverlays: this.settings.showHydrationOverlays(),
cdHighlighting: this.settings.highlightChangeDetection(),
cdDataStream: this.settings.showCdInExplorer(),
deferBlocks: this.settings.showDeferBlocks(),
forBlocks: this.settings.showForBlocks(),
},
]);
},
@@ -395,6 +395,8 @@ export interface DevtoolsConfig {
hydrationOverlays: boolean;
cdHighlighting: boolean;
cdDataStream: boolean;
forBlocks: boolean;
deferBlocks: boolean;
}
export interface Events {