Content-Length: 717629 | pFad | http://github.com/angular/angular/commit/7a308ccc3bc9764fe4bd73d6c87067b0f8de992d

DF refactor(core): consolidate logic to determine whether node can be hy… · angular/angular@7a308cc · GitHub
Skip to content

Commit 7a308cc

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(core): consolidate logic to determine whether node can be hydrated (#61409)
Several instructions were repeating the logic that checks if a specific can be hydrated. These changes move it into a common location. PR Close #61409
1 parent 6783fb7 commit 7a308cc

File tree

6 files changed

+33
-36
lines changed

6 files changed

+33
-36
lines changed

packages/core/src/hydration/utils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {LContainer} from '../render3/interfaces/container';
1313
import {getDocument} from '../render3/interfaces/document';
1414
import {RElement, RNode} from '../render3/interfaces/renderer_dom';
1515
import {isRootView} from '../render3/interfaces/type_checks';
16-
import {HEADER_OFFSET, LView, TVIEW, TViewType} from '../render3/interfaces/view';
16+
import {HEADER_OFFSET, HYDRATION, LView, TVIEW, TViewType} from '../render3/interfaces/view';
1717
import {makeStateKey, StateKey, TransferState} from '../transfer_state';
1818
import {assertDefined, assertEqual} from '../util/assert';
1919
import type {HydrationContext} from './annotate';
@@ -39,6 +39,9 @@ import {DeferBlockTrigger, HydrateTriggerDetails} from '../defer/interfaces';
3939
import {hoverEventNames, interactionEventNames} from '../defer/dom_triggers';
4040
import {DEHYDRATED_BLOCK_REGISTRY} from '../defer/registry';
4141
import {sharedMapFunction} from '../event_delegation_utils';
42+
import {isDetachedByI18n} from '../i18n/utils';
43+
import {isInSkipHydrationBlock} from '../render3/state';
44+
import {TNode} from '../render3/interfaces/node';
4245

4346
/**
4447
* The name of the key used in the TransferState collection,
@@ -496,6 +499,22 @@ export function isDisconnectedNode(hydrationInfo: DehydratedView, index: number)
496499
return !!initDisconnectedNodes(hydrationInfo)?.has(index);
497500
}
498501

502+
/**
503+
* Checks whether a node can be hydrated.
504+
* @param lView View in which the node instance is placed.
505+
* @param tNode Node to be checked.
506+
*/
507+
export function canHydrateNode(lView: LView, tNode: TNode): boolean {
508+
const hydrationInfo = lView[HYDRATION];
509+
510+
return (
511+
hydrationInfo !== null &&
512+
!isInSkipHydrationBlock() &&
513+
!isDetachedByI18n(tNode) &&
514+
!isDisconnectedNode(hydrationInfo, tNode.index - HEADER_OFFSET)
515+
);
516+
}
517+
499518
/**
500519
* Helper function to prepare text nodes for serialization by ensuring
501520
* that seperate logical text blocks in the DOM remain separate after

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import {
1717
hasSkipHydrationAttrOnTNode,
1818
} from '../../hydration/skip_hydration';
1919
import {
20+
canHydrateNode,
2021
getSerializedContainerViews,
21-
isDisconnectedNode,
2222
markRNodeAsClaimedByHydration,
2323
markRNodeAsSkippedByHydration,
2424
setSegmentHead,
2525
} from '../../hydration/utils';
26-
import {isDetachedByI18n} from '../../i18n/utils';
2726
import {assertDefined, assertEqual} from '../../util/assert';
2827
import {clearElementContents, createElementNode} from '../dom_node_manipulation';
2928
import {hasClassInput, hasStyleInput, TNode, TNodeType} from '../interfaces/node';
@@ -40,7 +39,6 @@ import {
4039
getLView,
4140
getNamespace,
4241
getTView,
43-
isInSkipHydrationBlock,
4442
isSkipHydrationRootTNode,
4543
lastNodeWasCreated,
4644
leaveSkipHydrationBlock,
@@ -192,12 +190,7 @@ function locateOrCreateElementNodeImpl(
192190
name: string,
193191
index: number,
194192
): RElement {
195-
const hydrationInfo = lView[HYDRATION];
196-
const isNodeCreationMode =
197-
!hydrationInfo ||
198-
isInSkipHydrationBlock() ||
199-
isDetachedByI18n(tNode) ||
200-
isDisconnectedNode(hydrationInfo, index);
193+
const isNodeCreationMode = !canHydrateNode(lView, tNode);
201194
lastNodeWasCreated(isNodeCreationMode);
202195

203196
// Regular creation mode.
@@ -206,6 +199,7 @@ function locateOrCreateElementNodeImpl(
206199
}
207200

208201
// Hydration mode, looking up an existing element in DOM.
202+
const hydrationInfo = lView[HYDRATION]!;
209203
const native = locateNextRNode<RElement>(hydrationInfo, tView, lView, tNode)!;
210204
ngDevMode && validateMatchingNode(native, Node.ELEMENT_NODE, name, lView, tNode);
211205
ngDevMode && markRNodeAsClaimedByHydration(native);

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import {validateMatchingNode, validateNodeExists} from '../../hydration/error_handling';
99
import {locateNextRNode, siblingAfter} from '../../hydration/node_lookup_utils';
1010
import {
11+
canHydrateNode,
1112
getNgContainerSize,
12-
isDisconnectedNode,
1313
markRNodeAsClaimedByHydration,
1414
setSegmentHead,
1515
} from '../../hydration/utils';
16-
import {isDetachedByI18n} from '../../i18n/utils';
1716
import {assertDefined, assertEqual, assertNumber} from '../../util/assert';
1817
import {createCommentNode} from '../dom_node_manipulation';
1918
import {TNode, TNodeType} from '../interfaces/node';
@@ -26,7 +25,6 @@ import {
2625
getCurrentTNode,
2726
getLView,
2827
getTView,
29-
isInSkipHydrationBlock,
3028
lastNodeWasCreated,
3129
} from '../state';
3230
import {elementLikeEndShared, elementLikeStartShared} from './shared';
@@ -134,12 +132,7 @@ function locateOrCreateElementContainerNode(
134132
index: number,
135133
): RComment {
136134
let comment: RComment;
137-
const hydrationInfo = lView[HYDRATION];
138-
const isNodeCreationMode =
139-
!hydrationInfo ||
140-
isInSkipHydrationBlock() ||
141-
isDisconnectedNode(hydrationInfo, index) ||
142-
isDetachedByI18n(tNode);
135+
const isNodeCreationMode = !canHydrateNode(lView, tNode);
143136

144137
lastNodeWasCreated(isNodeCreationMode);
145138

@@ -149,6 +142,7 @@ function locateOrCreateElementContainerNode(
149142
}
150143

151144
// Hydration mode, looking up existing elements in DOM.
145+
const hydrationInfo = lView[HYDRATION]!;
152146
const currentRNode = locateNextRNode(hydrationInfo, tView, lView, tNode)!;
153147
ngDevMode && validateNodeExists(currentRNode, lView, tNode);
154148

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import {TEMPLATES} from '../../hydration/interfaces';
1010
import {locateNextRNode, siblingAfter} from '../../hydration/node_lookup_utils';
1111
import {
1212
calcSerializedContainerSize,
13-
isDisconnectedNode,
13+
canHydrateNode,
1414
markRNodeAsClaimedByHydration,
1515
setSegmentHead,
1616
} from '../../hydration/utils';
17-
import {isDetachedByI18n} from '../../i18n/utils';
1817
import {populateDehydratedViewsInLContainer} from '../../linker/view_container_ref';
1918
import {assertEqual} from '../../util/assert';
2019
import {assertFirstCreatePass} from '../assert';
@@ -263,19 +262,15 @@ function locateOrCreateContainerAnchorImpl(
263262
tNode: TNode,
264263
index: number,
265264
): RComment {
266-
const hydrationInfo = lView[HYDRATION];
267-
const isNodeCreationMode =
268-
!hydrationInfo ||
269-
isInSkipHydrationBlock() ||
270-
isDetachedByI18n(tNode) ||
271-
isDisconnectedNode(hydrationInfo, index);
265+
const isNodeCreationMode = !canHydrateNode(lView, tNode);
272266
lastNodeWasCreated(isNodeCreationMode);
273267

274268
// Regular creation mode.
275269
if (isNodeCreationMode) {
276270
return createContainerAnchorImpl(tView, lView, tNode, index);
277271
}
278272

273+
const hydrationInfo = lView[HYDRATION]!;
279274
const ssrId = hydrationInfo.data[TEMPLATES]?.[index] ?? null;
280275

281276
// Apply `ssrId` value to the underlying TView if it was not previously set.

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
*/
88
import {validateMatchingNode} from '../../hydration/error_handling';
99
import {locateNextRNode} from '../../hydration/node_lookup_utils';
10-
import {isDisconnectedNode, markRNodeAsClaimedByHydration} from '../../hydration/utils';
11-
import {isDetachedByI18n} from '../../i18n/utils';
10+
import {canHydrateNode, markRNodeAsClaimedByHydration} from '../../hydration/utils';
1211
import {assertEqual, assertIndexInRange} from '../../util/assert';
1312
import {TElementNode, TNode, TNodeType} from '../interfaces/node';
1413
import {RText} from '../interfaces/renderer_dom';
@@ -19,7 +18,6 @@ import {
1918
getBindingIndex,
2019
getLView,
2120
getTView,
22-
isInSkipHydrationBlock,
2321
lastNodeWasCreated,
2422
setCurrentTNode,
2523
wasLastNodeCreated,
@@ -84,12 +82,7 @@ function locateOrCreateTextNodeImpl(
8482
value: string,
8583
index: number,
8684
): RText {
87-
const hydrationInfo = lView[HYDRATION];
88-
const isNodeCreationMode =
89-
!hydrationInfo ||
90-
isInSkipHydrationBlock() ||
91-
isDetachedByI18n(tNode) ||
92-
isDisconnectedNode(hydrationInfo, index);
85+
const isNodeCreationMode = !canHydrateNode(lView, tNode);
9386
lastNodeWasCreated(isNodeCreationMode);
9487

9588
// Regular creation mode.
@@ -98,6 +91,7 @@ function locateOrCreateTextNodeImpl(
9891
}
9992

10093
// Hydration mode, looking up an existing element in DOM.
94+
const hydrationInfo = lView[HYDRATION]!;
10195
const textNative = locateNextRNode(hydrationInfo, tView, lView, tNode) as RText;
10296

10397
ngDevMode && validateMatchingNode(textNative, Node.TEXT_NODE, null, lView, tNode);

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"callHook",
192192
"callHookInternal",
193193
"callHooks",
194+
"canHydrateNode",
194195
"checkStable",
195196
"cleanUpView",
196197
"cleanupDehydratedIcuData",

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/commit/7a308ccc3bc9764fe4bd73d6c87067b0f8de992d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy