Skip to content

Commit 0206c10

Browse files
crisbetoalxhub
authored andcommitted
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
1 parent 8fb737c commit 0206c10

File tree

11 files changed

+26
-25
lines changed

11 files changed

+26
-25
lines changed

packages/core/src/render3/definition.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import {initNgDevMode} from '../util/ng_dev_mode';
1717
import {stringify} from '../util/stringify';
1818

1919
import {NG_COMP_DEF, NG_DIR_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
20-
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DependencyTypeList, DirectiveDef, DirectiveDefFeature, DirectiveDefList, DirectiveTypesOrFactory, HostBindingsFunction, PipeDef, PipeDefList, PipeTypesOrFactory, TypeOrFactory, ViewQueriesFunction} from './interfaces/definition';
20+
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DependencyTypeList, DirectiveDef, DirectiveDefFeature, DirectiveDefList, HostBindingsFunction, PipeDef, PipeDefList, TypeOrFactory, ViewQueriesFunction} from './interfaces/definition';
2121
import {TAttributes, TConstantsOrFactory} from './interfaces/node';
2222
import {CssSelectorList} from './interfaces/projection';
2323

2424

25-
let _renderCompCount = 0;
25+
/** Counter used to generate unique IDs for component definitions. */
26+
let componentDefCount = 0;
2627

2728

2829
/**
@@ -318,7 +319,7 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
318319
features: componentDefinition.features as DirectiveDefFeature[] || null,
319320
data: componentDefinition.data || {},
320321
encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated,
321-
id: 'c',
322+
id: `c${componentDefCount++}`,
322323
styles: componentDefinition.styles || EMPTY_ARRAY,
323324
_: null,
324325
setInput: null,
@@ -327,7 +328,6 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
327328
};
328329
const dependencies = componentDefinition.dependencies;
329330
const feature = componentDefinition.features;
330-
def.id += _renderCompCount++;
331331
def.inputs = invertObject(componentDefinition.inputs, declaredInputs),
332332
def.outputs = invertObject(componentDefinition.outputs),
333333
feature && feature.forEach((fn) => fn(def));

packages/core/src/render3/features/standalone_feature.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {createEnvironmentInjector} from '../ng_module_ref';
1919
* collected from the imports graph rooted at a given standalone component.
2020
*/
2121
class StandaloneService implements OnDestroy {
22-
cachedInjectors = new Map<ComponentDef<unknown>, EnvironmentInjector|null>();
22+
cachedInjectors = new Map<string, EnvironmentInjector|null>();
2323

2424
constructor(private _injector: EnvironmentInjector) {}
2525

@@ -28,16 +28,16 @@ class StandaloneService implements OnDestroy {
2828
return null;
2929
}
3030

31-
if (!this.cachedInjectors.has(componentDef)) {
31+
if (!this.cachedInjectors.has(componentDef.id)) {
3232
const providers = internalImportProvidersFrom(false, componentDef.type);
3333
const standaloneInjector = providers.length > 0 ?
3434
createEnvironmentInjector(
3535
[providers], this._injector, `Standalone[${componentDef.type.name}]`) :
3636
null;
37-
this.cachedInjectors.set(componentDef, standaloneInjector);
37+
this.cachedInjectors.set(componentDef.id, standaloneInjector);
3838
}
3939

40-
return this.cachedInjectors.get(componentDef)!;
40+
return this.cachedInjectors.get(componentDef.id)!;
4141
}
4242

4343
ngOnDestroy() {

packages/core/src/render3/interfaces/definition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ export interface DirectiveDef<T> {
226226
*/
227227
export interface ComponentDef<T> extends DirectiveDef<T> {
228228
/**
229-
* Runtime unique component ID.
229+
* Unique ID for the component. Used in view encapsulation and
230+
* to keep track of the injector in standalone components.
230231
*/
231232
readonly id: string;
232233

packages/core/test/bundling/animations/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@
549549
"name": "_randomChar"
550550
},
551551
{
552-
"name": "_renderCompCount"
552+
"name": "componentDefCount"
553553
},
554554
{
555555
"name": "_testabilityGetter"
@@ -1403,4 +1403,4 @@
14031403
{
14041404
"name": "ɵɵproperty"
14051405
}
1406-
]
1406+
]

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"name": "_injectImplementation"
6767
},
6868
{
69-
"name": "_renderCompCount"
69+
"name": "componentDefCount"
7070
},
7171
{
7272
"name": "addComponentLogic"
@@ -449,4 +449,4 @@
449449
{
450450
"name": "ɵɵinject"
451451
}
452-
]
452+
]

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@
549549
"name": "_randomChar"
550550
},
551551
{
552-
"name": "_renderCompCount"
552+
"name": "componentDefCount"
553553
},
554554
{
555555
"name": "_symbolIterator"
@@ -1595,4 +1595,4 @@
15951595
{
15961596
"name": "ɵɵtext"
15971597
}
1598-
]
1598+
]

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@
540540
"name": "_randomChar"
541541
},
542542
{
543-
"name": "_renderCompCount"
543+
"name": "componentDefCount"
544544
},
545545
{
546546
"name": "_symbolIterator"
@@ -1583,4 +1583,4 @@
15831583
{
15841584
"name": "ɵɵtext"
15851585
}
1586-
]
1586+
]

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"name": "_injectImplementation"
6464
},
6565
{
66-
"name": "_renderCompCount"
66+
"name": "componentDefCount"
6767
},
6868
{
6969
"name": "allocExpando"
@@ -335,4 +335,4 @@
335335
{
336336
"name": "ɵɵinject"
337337
}
338-
]
338+
]

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@
780780
"name": "_randomChar"
781781
},
782782
{
783-
"name": "_renderCompCount"
783+
"name": "componentDefCount"
784784
},
785785
{
786786
"name": "_stripIndexHtml"
@@ -2018,4 +2018,4 @@
20182018
{
20192019
"name": "ɵɵtextInterpolate1"
20202020
}
2021-
]
2021+
]

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@
342342
"name": "_randomChar"
343343
},
344344
{
345-
"name": "_renderCompCount"
345+
"name": "componentDefCount"
346346
},
347347
{
348348
"name": "_wrapInTimeout"
@@ -881,4 +881,4 @@
881881
{
882882
"name": "ɵɵinject"
883883
}
884-
]
884+
]

0 commit comments

Comments
 (0)