|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Type} from '../interface/type'; |
| 10 | +import {assertDefined} from '../util/assert'; |
| 11 | +import {assertLView} from './assert'; |
| 12 | +import {getComponentDef} from './definition'; |
| 13 | +import {assertComponentDef} from './errors'; |
| 14 | +import {refreshView} from './instructions/change_detection'; |
| 15 | +import {renderView} from './instructions/render'; |
| 16 | +import { |
| 17 | + createLView, |
| 18 | + getInitialLViewFlagsFromDef, |
| 19 | + getOrCreateComponentTView, |
| 20 | +} from './instructions/shared'; |
| 21 | +import {CONTAINER_HEADER_OFFSET} from './interfaces/container'; |
| 22 | +import {ComponentDef} from './interfaces/definition'; |
| 23 | +import {getTrackedLViews} from './interfaces/lview_tracking'; |
| 24 | +import {isTNodeShape, TElementNode, TNodeFlags, TNodeType} from './interfaces/node'; |
| 25 | +import {isLContainer, isLView} from './interfaces/type_checks'; |
| 26 | +import { |
| 27 | + CHILD_HEAD, |
| 28 | + CHILD_TAIL, |
| 29 | + CONTEXT, |
| 30 | + ENVIRONMENT, |
| 31 | + FLAGS, |
| 32 | + HEADER_OFFSET, |
| 33 | + HOST, |
| 34 | + LView, |
| 35 | + LViewFlags, |
| 36 | + NEXT, |
| 37 | + PARENT, |
| 38 | + T_HOST, |
| 39 | + TVIEW, |
| 40 | +} from './interfaces/view'; |
| 41 | +import {assertTNodeType} from './node_assert'; |
| 42 | +import {destroyLView, removeViewFromDOM} from './node_manipulation'; |
| 43 | + |
| 44 | +/** |
| 45 | + * Replaces the metadata of a component type and re-renders all live instances of the component. |
| 46 | + * @param type Class whose metadata will be replaced. |
| 47 | + * @param applyMetadata Callback that will apply a new set of metadata on the `type` when invoked. |
| 48 | + * @codeGenApi |
| 49 | + */ |
| 50 | +export function ɵɵreplaceMedata(type: Type<unknown>, applyMetadata: () => void) { |
| 51 | + ngDevMode && assertComponentDef(type); |
| 52 | + const oldDef = getComponentDef(type)!; |
| 53 | + |
| 54 | + // The reason `applyMetadata` is a callback that is invoked (almost) immediately is because |
| 55 | + // the compiler usually produces more code than just the component definition, e.g. there |
| 56 | + // can be functions for embedded views, the variables for the constant pool and `setClassMetadata` |
| 57 | + // calls. The callback allows us to keep them isolate from the rest of the app and to invoke |
| 58 | + // them at the right time. |
| 59 | + applyMetadata(); |
| 60 | + |
| 61 | + // If a `tView` hasn't been created yet, it means that this component hasn't been instantianted |
| 62 | + // before. In this case there's nothing left for us to do aside from patching it in. |
| 63 | + if (oldDef.tView) { |
| 64 | + const trackedViews = getTrackedLViews().values(); |
| 65 | + for (const root of trackedViews) { |
| 66 | + // Note: we have the additional check, because `IsRoot` can also indicate |
| 67 | + // a component created through something like `createComponent`. |
| 68 | + if (root[FLAGS] & LViewFlags.IsRoot && root[PARENT] === null) { |
| 69 | + recreateMatchingLViews(oldDef, root); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Finds all LViews matching a specific component definition and recreates them. |
| 77 | + * @param def Component definition to search for. |
| 78 | + * @param rootLView View from which to start the search. |
| 79 | + */ |
| 80 | +function recreateMatchingLViews(def: ComponentDef<unknown>, rootLView: LView): void { |
| 81 | + ngDevMode && |
| 82 | + assertDefined( |
| 83 | + def.tView, |
| 84 | + 'Expected a component definition that has been instantiated at least once', |
| 85 | + ); |
| 86 | + |
| 87 | + const tView = rootLView[TVIEW]; |
| 88 | + |
| 89 | + // Use `tView` to match the LView since `instanceof` can |
| 90 | + // produce false positives when using inheritance. |
| 91 | + if (tView === def.tView) { |
| 92 | + ngDevMode && assertComponentDef(def.type); |
| 93 | + recreateLView(getComponentDef(def.type)!, rootLView); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + for (let i = HEADER_OFFSET; i < tView.bindingStartIndex; i++) { |
| 98 | + const current = rootLView[i]; |
| 99 | + |
| 100 | + if (isLContainer(current)) { |
| 101 | + for (let i = CONTAINER_HEADER_OFFSET; i < current.length; i++) { |
| 102 | + recreateMatchingLViews(def, current[i]); |
| 103 | + } |
| 104 | + } else if (isLView(current)) { |
| 105 | + recreateMatchingLViews(def, current); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Recreates an LView in-place from a new component definition. |
| 112 | + * @param def Definition from which to recreate the view. |
| 113 | + * @param lView View to be recreated. |
| 114 | + */ |
| 115 | +function recreateLView(def: ComponentDef<unknown>, lView: LView<unknown>): void { |
| 116 | + const instance = lView[CONTEXT]; |
| 117 | + const host = lView[HOST]!; |
| 118 | + // In theory the parent can also be an LContainer, but it appears like that's |
| 119 | + // only the case for embedded views which we won't be replacing here. |
| 120 | + const parentLView = lView[PARENT] as LView; |
| 121 | + ngDevMode && assertLView(parentLView); |
| 122 | + const tNode = lView[T_HOST] as TElementNode; |
| 123 | + ngDevMode && assertTNodeType(tNode, TNodeType.Element); |
| 124 | + |
| 125 | + // Recreate the TView since the template might've changed. |
| 126 | + const newTView = getOrCreateComponentTView(def); |
| 127 | + |
| 128 | + // Create a new LView from the new TView, but reusing the existing TNode and DOM node. |
| 129 | + const newLView = createLView( |
| 130 | + parentLView, |
| 131 | + newTView, |
| 132 | + instance, |
| 133 | + getInitialLViewFlagsFromDef(def), |
| 134 | + host, |
| 135 | + tNode, |
| 136 | + null, |
| 137 | + lView[ENVIRONMENT].rendererFactory.createRenderer(host, def), |
| 138 | + null, |
| 139 | + null, |
| 140 | + null, |
| 141 | + ); |
| 142 | + |
| 143 | + // Detach the LView from its current place in the tree so we don't |
| 144 | + // start traversing any siblings and modifying their structure. |
| 145 | + replaceLViewInTree(parentLView, lView, newLView, tNode.index); |
| 146 | + |
| 147 | + // Destroy the detached LView. |
| 148 | + destroyLView(lView[TVIEW], lView); |
| 149 | + |
| 150 | + // Remove the nodes associated with the destroyed LView. This removes the |
| 151 | + // descendants, but not the host which we want to stay in place. |
| 152 | + removeViewFromDOM(lView[TVIEW], lView); |
| 153 | + |
| 154 | + // Reset the content projection state of the TNode before the first render. |
| 155 | + // Note that this has to happen after the LView has been destroyed or we |
| 156 | + // risk some projected nodes not being removed correctly. |
| 157 | + resetProjectionState(tNode); |
| 158 | + |
| 159 | + // Creation pass for the new view. |
| 160 | + renderView(newTView, newLView, instance); |
| 161 | + |
| 162 | + // Update pass for the new view. |
| 163 | + refreshView(newTView, newLView, newTView.template, instance); |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Replaces one LView in the tree with another one. |
| 168 | + * @param parentLView Parent of the LView being replaced. |
| 169 | + * @param oldLView LView being replaced. |
| 170 | + * @param newLView Replacement LView to be inserted. |
| 171 | + * @param index Index at which the LView should be inserted. |
| 172 | + */ |
| 173 | +function replaceLViewInTree( |
| 174 | + parentLView: LView, |
| 175 | + oldLView: LView, |
| 176 | + newLView: LView, |
| 177 | + index: number, |
| 178 | +): void { |
| 179 | + // Update the sibling whose `NEXT` pointer refers to the old view. |
| 180 | + for (let i = HEADER_OFFSET; i < parentLView[TVIEW].bindingStartIndex; i++) { |
| 181 | + const current = parentLView[i]; |
| 182 | + |
| 183 | + if ((isLView(current) || isLContainer(current)) && current[NEXT] === oldLView) { |
| 184 | + current[NEXT] = newLView; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Set the new view as the head, if the old view was first. |
| 190 | + if (parentLView[CHILD_HEAD] === oldLView) { |
| 191 | + parentLView[CHILD_HEAD] = newLView; |
| 192 | + } |
| 193 | + |
| 194 | + // Set the new view as the tail, if the old view was last. |
| 195 | + if (parentLView[CHILD_TAIL] === oldLView) { |
| 196 | + parentLView[CHILD_TAIL] = newLView; |
| 197 | + } |
| 198 | + |
| 199 | + // Update the `NEXT` pointer to the same as the old view. |
| 200 | + newLView[NEXT] = oldLView[NEXT]; |
| 201 | + |
| 202 | + // Clear out the `NEXT` of the old view. |
| 203 | + oldLView[NEXT] = null; |
| 204 | + |
| 205 | + // Insert the new LView at the correct index. |
| 206 | + parentLView[index] = newLView; |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * Child nodes mutate the `projection` state of their parent node as they're being projected. |
| 211 | + * This function resets the `project` back to its initial state. |
| 212 | + * @param tNode |
| 213 | + */ |
| 214 | +function resetProjectionState(tNode: TElementNode): void { |
| 215 | + // The `projection` is mutated by child nodes as they're being projected. We need to |
| 216 | + // reset it to the initial state so projection works after the template is swapped out. |
| 217 | + if (tNode.projection !== null) { |
| 218 | + for (const current of tNode.projection) { |
| 219 | + if (isTNodeShape(current)) { |
| 220 | + // Reset `projectionNext` since it can affect the traversal order during projection. |
| 221 | + current.projectionNext = null; |
| 222 | + current.flags &= ~TNodeFlags.isProjected; |
| 223 | + } |
| 224 | + } |
| 225 | + tNode.projection = null; |
| 226 | + } |
| 227 | +} |
0 commit comments