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

Function recreate

packages/core/src/render3/hmr.ts:248–318  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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
289 // stored in LContainer[DEHYDRATED_VIEWS], which causes duplicated
290 // content when the view is re-rendered during HMR.
291 cleanupDehydratedLView(lView);
292
293 // Always force the creation of a new renderer to ensure state captured during construction
294 // stays consistent with the new component definition by clearing any old ached factories.
295 const rendererFactory = lView[ENVIRONMENT].rendererFactory;
296 clearRendererCache(rendererFactory, oldDef);
297
298 // Patch a brand-new renderer onto the new view only after the old
299 // view is destroyed so that the runtime doesn't try to reuse it.
300 newLView[RENDERER] = rendererFactory.createRenderer(host, newDef);
301
302 // Remove the nodes associated with the destroyed LView. This removes the
303 // descendants, but not the host which we want to stay in place.
304 removeViewFromDOM(lView[TVIEW], lView);
305

Callers

nothing calls this directly

Calls 11

createLViewFunction · 0.90
destroyLViewFunction · 0.90
removeViewFromDOMFunction · 0.90
renderViewFunction · 0.90
refreshViewFunction · 0.90
replaceLViewInTreeFunction · 0.85
clearRendererCacheFunction · 0.85
resetProjectionStateFunction · 0.85
createRendererMethod · 0.65

Tested by

no test coverage detected