MCPcopy Create free account
hub / github.com/angular/angular / ɵɵreplaceMetadata

Function ɵɵreplaceMetadata

packages/core/src/render3/hmr.ts:83–120  ·  view source on GitHub ↗
(
  type: Type<unknown>,
  applyMetadata: (...args: [Type<unknown>, unknown[], ...unknown[]]) => void,
  namespaces: unknown[],
  locals: unknown[],
  importMeta: ImportMetaExtended | null = null,
  id: string | null = null,
)

Source from the content-addressed store, hash-verified

81 * @codeGenApi
82 */
83export function ɵɵreplaceMetadata(
84 type: Type<unknown>,
85 applyMetadata: (...args: [Type<unknown>, unknown[], ...unknown[]]) => void,
86 namespaces: unknown[],
87 locals: unknown[],
88 importMeta: ImportMetaExtended | null = null,
89 id: string | null = null,
90) {
91 ngDevMode && assertComponentDef(type);
92 const currentDef = getComponentDef(type)!;
93
94 // The reason `applyMetadata` is a callback that is invoked (almost) immediately is because
95 // the compiler usually produces more code than just the component definition, e.g. there
96 // can be functions for embedded views, the variables for the constant pool and `setClassMetadata`
97 // calls. The callback allows us to keep them isolate from the rest of the app and to invoke
98 // them at the right time.
99 applyMetadata.apply(null, [type, namespaces, ...locals]);
100
101 const {newDef, oldDef} = mergeWithExistingDefinition(currentDef, getComponentDef(type)!);
102
103 // TODO(crisbeto): the `applyMetadata` call above will replace the definition on the type.
104 // Ideally we should adjust the compiler output so the metadata is returned, however that'll
105 // require some internal changes. We re-add the metadata here manually.
106 (type as any)[NG_COMP_DEF] = newDef;
107
108 // If a `tView` hasn't been created yet, it means that this component hasn't been instantianted
109 // before. In this case there's nothing left for us to do aside from patching it in.
110 if (oldDef.tView) {
111 const trackedViews = getTrackedLViews().values();
112 for (const root of trackedViews) {
113 // Note: we have the additional check, because `IsRoot` can also indicate
114 // a component created through something like `createComponent`.
115 if (isRootView(root) && root[PARENT] === null) {
116 recreateMatchingLViews(importMeta, id, newDef, oldDef, root);
117 }
118 }
119 }
120}
121
122/**
123 * Merges two component definitions while preseving the original one in place.

Callers 1

replaceMetadataFunction · 0.90

Calls 8

assertComponentDefFunction · 0.90
getComponentDefFunction · 0.90
getTrackedLViewsFunction · 0.90
isRootViewFunction · 0.90
recreateMatchingLViewsFunction · 0.85
valuesMethod · 0.80
applyMethod · 0.65

Tested by

no test coverage detected