Skip to content

Commit c7a5163

Browse files
committed
fixup! refactor(core): abstract control flow discovery utilities
1 parent a50eda8 commit c7a5163

3 files changed

Lines changed: 18 additions & 39 deletions

File tree

packages/core/src/render3/instructions/control_flow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export function ɵɵrepeaterTrackByIdentity<T>(_: number, value: T) {
253253
return value;
254254
}
255255

256-
class RepeaterMetadata {
256+
export class RepeaterMetadata {
257257
constructor(
258258
public hasEmptyBlock: boolean,
259259
public trackByFn: TrackByFunction<unknown>,

packages/core/src/render3/util/control_flow.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,20 @@ import {
4141
ControlFlowBlockType,
4242
DeferBlockData,
4343
ForLoopBlockData,
44-
RepeaterMetadataShape,
4544
} from './control_flow_types';
4645
import {TNode} from '../interfaces/node';
46+
import {RepeaterMetadata} from '../instructions/control_flow';
4747

4848
/**
4949
* Gets all of the control flow blocks that are present inside the specified DOM node.
5050
* @param node Node in which to look for control flow blocks.
51-
*
52-
* @publicApi
5351
*/
5452
export function getControlFlowBlocks(node: Node): ControlFlowBlock[] {
55-
const results: ControlFlowBlock[] = [];
53+
let results: ControlFlowBlock[] = [];
5654
const lView = getLContext(node)?.lView;
5755

5856
if (lView) {
59-
findControlFlowBlocks(node, lView, results);
57+
results = findControlFlowBlocks(node, lView);
6058
}
6159

6260
return results;
@@ -91,8 +89,6 @@ const deferBlockFinder: ControlFlowBlockViewFinder = ({
9189
const tDetails = getTDeferBlockDetails(tView, tNode);
9290

9391
if (isTDeferBlockDetails(tDetails)) {
94-
// return {lContainer, lView, tNode, tDetails};
95-
9692
const native = getNativeByTNode(tNode, lView);
9793
const lDetails = getLDeferBlockDetails(lView, tNode);
9894

@@ -174,7 +170,7 @@ const forLoopFinder: ControlFlowBlockViewFinder = ({
174170
}: ControlFlowBlockViewFinderConfig) => {
175171
const slot = lView[slotIdx];
176172

177-
if (!isRepeaterMetadata(slot)) {
173+
if (!(slot instanceof RepeaterMetadata)) {
178174
return null;
179175
}
180176

@@ -222,9 +218,14 @@ const CONTROL_FLOW_BLOCK_FINDERS: ControlFlowBlockViewFinder[] = [deferBlockFind
222218
*
223219
* @param node Node in which to search for blocks.
224220
* @param lView View within the node in which to search for blocks.
225-
* @param results Array to which to add blocks once they're found.
221+
* @param results (Optional) Array to which to add blocks once they're found.
222+
* @returns Found control flow blocks results array.
226223
*/
227-
function findControlFlowBlocks(node: Node, lView: LView, results: ControlFlowBlock[]) {
224+
function findControlFlowBlocks(
225+
node: Node,
226+
lView: LView,
227+
results: ControlFlowBlock[] = [],
228+
): ControlFlowBlock[] {
228229
const tView = lView[TVIEW];
229230

230231
for (let i = HEADER_OFFSET; i < tView.bindingStartIndex; i++) {
@@ -255,6 +256,8 @@ function findControlFlowBlocks(node: Node, lView: LView, results: ControlFlowBlo
255256
findControlFlowBlocks(node, slot, results);
256257
}
257258
}
259+
260+
return results;
258261
}
259262

260263
/**
@@ -318,27 +321,13 @@ function getRendererLView(lContainer: LContainer): LView | null {
318321
return lView;
319322
}
320323

321-
/**
322-
* Checks if a value looks like RepeaterMetadata by duck-typing.
323-
* Can't use instanceof because that would require importing from control_flow.ts.
324-
*/
325-
function isRepeaterMetadata(value: unknown): value is RepeaterMetadataShape {
326-
return (
327-
value !== null &&
328-
typeof value === 'object' &&
329-
'hasEmptyBlock' in value &&
330-
'trackByFn' in value &&
331-
typeof (value as RepeaterMetadataShape).trackByFn === 'function'
332-
);
333-
}
334-
335324
/**
336325
* Returns the string representation of the track expression.
337326
*
338327
* @param metadata Metadata containing the track function.
339328
* @returns
340329
*/
341-
function getTrackExpression(metadata: RepeaterMetadataShape): string {
330+
function getTrackExpression(metadata: RepeaterMetadata): string {
342331
const trackByFn = metadata.trackByFn;
343332
if (trackByFn.name === 'ɵɵrepeaterTrackByIndex') {
344333
return '$index';

packages/core/src/render3/util/control_flow_types.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ export interface ControlFlowBlockViewFinderConfig {
9393
/**
9494
* Describes a finder function that extracts `ControlFlowBlock`s from an LView.
9595
*/
96-
export interface ControlFlowBlockViewFinder {
97-
(config: ControlFlowBlockViewFinderConfig): ControlFlowBlock | null;
98-
}
99-
100-
/**
101-
* Represents `RepeaterMetadata` data mirror.
102-
*/
103-
export interface RepeaterMetadataShape {
104-
hasEmptyBlock: boolean;
105-
trackByFn: TrackByFunction<unknown>;
106-
liveCollection?: LiveCollection<unknown, unknown>;
107-
originalTrackByFn?: TrackByFunction<unknown>;
108-
}
96+
export type ControlFlowBlockViewFinder = (
97+
config: ControlFlowBlockViewFinderConfig,
98+
) => ControlFlowBlock | null;

0 commit comments

Comments
 (0)