refactor(core): minor ComponentDef improvements (#46093)

Makes the following improvements in the runtime:
* Uses the unique ID of the component definition to keep track of its injector in the `StandaloneFeature`, instead of the definition itself. This reduces the amount of memory we can leak, if something doesn't get cleaned up.
* Changes the naming and description of the `ComponentDef.id` to reflect what it is used for.

PR Close #46093
This commit is contained in:
Kristiyan Kostadinov
2022-05-22 10:42:28 +02:00
committed by Alex Rickabaugh
parent 8fb737cfa4
commit 0206c10f8e
11 changed files with 26 additions and 25 deletions
+4 -4
View File
@@ -17,12 +17,13 @@ import {initNgDevMode} from '../util/ng_dev_mode';
import {stringify} from '../util/stringify';
import {NG_COMP_DEF, NG_DIR_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DependencyTypeList, DirectiveDef, DirectiveDefFeature, DirectiveDefList, DirectiveTypesOrFactory, HostBindingsFunction, PipeDef, PipeDefList, PipeTypesOrFactory, TypeOrFactory, ViewQueriesFunction} from './interfaces/definition';
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DependencyTypeList, DirectiveDef, DirectiveDefFeature, DirectiveDefList, HostBindingsFunction, PipeDef, PipeDefList, TypeOrFactory, ViewQueriesFunction} from './interfaces/definition';
import {TAttributes, TConstantsOrFactory} from './interfaces/node';
import {CssSelectorList} from './interfaces/projection';
let _renderCompCount = 0;
/** Counter used to generate unique IDs for component definitions. */
let componentDefCount = 0;
/**
@@ -318,7 +319,7 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
features: componentDefinition.features as DirectiveDefFeature[] || null,
data: componentDefinition.data || {},
encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated,
id: 'c',
id: `c${componentDefCount++}`,
styles: componentDefinition.styles || EMPTY_ARRAY,
_: null,
setInput: null,
@@ -327,7 +328,6 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
};
const dependencies = componentDefinition.dependencies;
const feature = componentDefinition.features;
def.id += _renderCompCount++;
def.inputs = invertObject(componentDefinition.inputs, declaredInputs),
def.outputs = invertObject(componentDefinition.outputs),
feature && feature.forEach((fn) => fn(def));
@@ -19,7 +19,7 @@ import {createEnvironmentInjector} from '../ng_module_ref';
* collected from the imports graph rooted at a given standalone component.
*/
class StandaloneService implements OnDestroy {
cachedInjectors = new Map<ComponentDef<unknown>, EnvironmentInjector|null>();
cachedInjectors = new Map<string, EnvironmentInjector|null>();
constructor(private _injector: EnvironmentInjector) {}
@@ -28,16 +28,16 @@ class StandaloneService implements OnDestroy {
return null;
}
if (!this.cachedInjectors.has(componentDef)) {
if (!this.cachedInjectors.has(componentDef.id)) {
const providers = internalImportProvidersFrom(false, componentDef.type);
const standaloneInjector = providers.length > 0 ?
createEnvironmentInjector(
[providers], this._injector, `Standalone[${componentDef.type.name}]`) :
null;
this.cachedInjectors.set(componentDef, standaloneInjector);
this.cachedInjectors.set(componentDef.id, standaloneInjector);
}
return this.cachedInjectors.get(componentDef)!;
return this.cachedInjectors.get(componentDef.id)!;
}
ngOnDestroy() {
@@ -226,7 +226,8 @@ export interface DirectiveDef<T> {
*/
export interface ComponentDef<T> extends DirectiveDef<T> {
/**
* Runtime unique component ID.
* Unique ID for the component. Used in view encapsulation and
* to keep track of the injector in standalone components.
*/
readonly id: string;
@@ -549,7 +549,7 @@
"name": "_randomChar"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_testabilityGetter"
@@ -1403,4 +1403,4 @@
{
"name": "ɵɵproperty"
}
]
]
@@ -66,7 +66,7 @@
"name": "_injectImplementation"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "addComponentLogic"
@@ -449,4 +449,4 @@
{
"name": "ɵɵinject"
}
]
]
@@ -549,7 +549,7 @@
"name": "_randomChar"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_symbolIterator"
@@ -1595,4 +1595,4 @@
{
"name": "ɵɵtext"
}
]
]
@@ -540,7 +540,7 @@
"name": "_randomChar"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_symbolIterator"
@@ -1583,4 +1583,4 @@
{
"name": "ɵɵtext"
}
]
]
@@ -63,7 +63,7 @@
"name": "_injectImplementation"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "allocExpando"
@@ -335,4 +335,4 @@
{
"name": "ɵɵinject"
}
]
]
@@ -780,7 +780,7 @@
"name": "_randomChar"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_stripIndexHtml"
@@ -2018,4 +2018,4 @@
{
"name": "ɵɵtextInterpolate1"
}
]
]
@@ -342,7 +342,7 @@
"name": "_randomChar"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_wrapInTimeout"
@@ -881,4 +881,4 @@
{
"name": "ɵɵinject"
}
]
]
@@ -165,7 +165,7 @@
"name": "_injectImplementation"
},
{
"name": "_renderCompCount"
"name": "componentDefCount"
},
{
"name": "_symbolIterator"
@@ -866,4 +866,4 @@
{
"name": "ɵɵtextInterpolate1"
}
]
]