MCPcopy Index your code
hub / github.com/angular/angular / mergeWithExistingDefinition

Function mergeWithExistingDefinition

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

* Merges two component definitions while preseving the original one in place. * @param currentDef Definition that should receive the new metadata. * @param newDef Source of the new metadata.

(
  currentDef: ComponentDef<unknown>,
  newDef: ComponentDef<unknown>,
)

Source from the content-addressed store, hash-verified

123 * @param newDef Source of the new metadata.
124 */
125function mergeWithExistingDefinition(
126 currentDef: ComponentDef<unknown>,
127 newDef: ComponentDef<unknown>,
128) {
129 // Clone the current definition since we reference its original data further
130 // down in the replacement process (e.g. when destroying the renderer).
131 const clone = {...currentDef};
132
133 // Assign the new metadata in place while preserving the object literal. It's important to
134 // Keep the object in place, because there can be references to it, for example in the
135 // `directiveDefs` of another definition.
136 const replacement = Object.assign(currentDef, newDef, {
137 // We need to keep the existing directive and pipe defs, because they can get patched on
138 // by a call to `setComponentScope` from a module file. That call won't make it into the
139 // HMR replacement function, because it lives in an entirely different file.
140 directiveDefs: clone.directiveDefs,
141 pipeDefs: clone.pipeDefs,
142
143 // Preserve the old `setInput` function, because it has some state.
144 // This is fine, because the component instance is preserved as well.
145 setInput: clone.setInput,
146
147 // Externally this is redundant since we redeclare the definition using the original type.
148 // Internally we may receive a definition with an alternate, but identical, type so we have
149 // to ensure that the original one is preserved.
150 type: clone.type,
151 });
152
153 ngDevMode && assertEqual(replacement, currentDef, 'Expected definition to be merged in place');
154 return {newDef: replacement, oldDef: clone};
155}
156
157/**
158 * Finds all LViews matching a specific component definition and recreates them.

Callers 1

ɵɵreplaceMetadataFunction · 0.85

Calls 1

assertEqualFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…