MCPcopy Create free account
hub / github.com/angular/angular / recreateLView

Function recreateLView

packages/core/src/render3/hmr.ts:231–326  ·  view source on GitHub ↗

* Recreates an LView in-place from a new component definition. * @param importMeta `import.meta` information. * @param id HMR ID for the component. * @param newDef Definition from which to recreate the view. * @param oldDef Previous component definition being swapped out. * @param lView View to

(
  importMeta: ImportMetaExtended | null,
  id: string | null,
  newDef: ComponentDef<unknown>,
  oldDef: ComponentDef<unknown>,
  lView: LView<unknown>,
)

Source from the content-addressed store, hash-verified

229 * @param lView View to be recreated.
230 */
231function recreateLView(
232 importMeta: ImportMetaExtended | null,
233 id: string | null,
234 newDef: ComponentDef<unknown>,
235 oldDef: ComponentDef<unknown>,
236 lView: LView<unknown>,
237): void {
238 const instance = lView[CONTEXT];
239 let host = lView[HOST]! as HTMLElement;
240 // In theory the parent can also be an LContainer, but it appears like that's
241 // only the case for embedded views which we won't be replacing here.
242 const parentLView = lView[PARENT] as LView;
243 ngDevMode && assertLView(parentLView);
244 const tNode = lView[T_HOST] as TElementNode;
245 ngDevMode && assertTNodeType(tNode, TNodeType.Element);
246 ngDevMode && assertNotEqual(newDef, oldDef, 'Expected different component definition');
247 const zone = lView[INJECTOR].get(NgZone, null);
248 const recreate = () => {
249 // If we're recreating a component with shadow DOM encapsulation, it will have attached a
250 // shadow root. The browser will throw if we attempt to attach another one and there's no way
251 // to detach it. Our only option is to make a clone only of the root node, replace the node
252 // with the clone and use it for the newly-created LView.
253 if (
254 oldDef.encapsulation === ViewEncapsulation.ShadowDom ||
255 oldDef.encapsulation === ViewEncapsulation.ExperimentalIsolatedShadowDom
256 ) {
257 const newHost = host.cloneNode(false) as HTMLElement;
258 host.replaceWith(newHost);
259 host = newHost;
260 }
261
262 // Recreate the TView since the template might've changed.
263 const newTView = getOrCreateComponentTView(newDef);
264
265 // Create a new LView from the new TView, but reusing the existing TNode and DOM node.
266 const newLView = createLView(
267 parentLView,
268 newTView,
269 instance,
270 getInitialLViewFlagsFromDef(newDef),
271 host,
272 tNode,
273 null,
274 null, // The renderer will be created a bit further down once the old one is destroyed.
275 null,
276 null,
277 null,
278 );
279
280 // Detach the LView from its current place in the tree so we don't
281 // start traversing any siblings and modifying their structure.
282 replaceLViewInTree(parentLView, lView, newLView, tNode.index);
283
284 // Destroy the detached LView.
285 destroyLView(lView[TVIEW], lView);
286
287 // Clean up any dehydrated views left over from SSR hydration.
288 // Neither destroyLView nor removeViewFromDOM handle DOM nodes

Callers 1

recreateMatchingLViewsFunction · 0.85

Calls 6

assertLViewFunction · 0.90
assertTNodeTypeFunction · 0.90
assertNotEqualFunction · 0.90
getMethod · 0.65
runMethod · 0.65

Tested by

no test coverage detected