refactor(ivy): move hostVars/hostAttrs from instruction to DirectiveDef (#34683)

This change moves information from instructions to declarative position:
- `ɵɵallocHostVars(vars)` => `DirectiveDef.hostVars`
- `ɵɵelementHostAttrs(attrs)` => `DirectiveDef.hostAttrs`

When merging directives it is necessary to know about `hostVars` and `hostAttrs`. Before this change the information was stored in the `hostBindings` function. This was problematic, because in order to get to the information the `hostBindings` would have to be executed. In order for `hostBindings` to be executed the directives would have to be instantiated. This means that the directive instantiation would happen before we had knowledge about the `hostAttrs` and as a result the directive could observe in the constructor that not all of the `hostAttrs` have been applied. This further complicates the runtime as we have to apply `hostAttrs` in parts over many invocations.

`ɵɵallocHostVars` was unnecessarily complicated because it would have to update the `LView` (and Blueprint) while existing directives are already executing. By moving it out of `hostBindings` function we can access it statically and we can create correct `LView` (and Blueprint) in a single pass.

This change only changes how the instructions are generated, but does not change the runtime much. (We cheat by emulating the old behavior by calling `ɵɵallocHostVars` and `ɵɵelementHostAttrs`) Subsequent change will refactor the runtime to take advantage of the static information.

PR Close #34683
This commit is contained in:
Miško Hevery
2020-01-08 11:32:33 -08:00
parent 239c602f69
commit d566621fef
33 changed files with 751 additions and 185 deletions

View File

@ -0,0 +1,119 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AttributeMarker} from '@angular/core/src/render3';
import {TAttributes} from '@angular/core/src/render3/interfaces/node';
import {mergeHostAttribute, mergeHostAttrs} from '@angular/core/src/render3/util/attrs_utils';
import {describe} from '@angular/core/testing/src/testing_internal';
describe('attr_util', () => {
describe('mergeHostAttribute', () => {
it('should add new attributes', () => {
const attrs: TAttributes = [];
mergeHostAttribute(attrs, -1, 'Key', null, 'value');
expect(attrs).toEqual(['Key', 'value']);
mergeHostAttribute(attrs, -1, 'A', null, 'a');
expect(attrs).toEqual(['Key', 'value', 'A', 'a']);
mergeHostAttribute(attrs, -1, 'X', null, 'x');
expect(attrs).toEqual(['Key', 'value', 'A', 'a', 'X', 'x']);
mergeHostAttribute(attrs, -1, 'Key', null, 'new');
expect(attrs).toEqual(['Key', 'new', 'A', 'a', 'X', 'x']);
});
it('should add new classes', () => {
const attrs: TAttributes = [];
mergeHostAttribute(attrs, AttributeMarker.Classes, 'CLASS', null, null);
expect(attrs).toEqual([AttributeMarker.Classes, 'CLASS']);
mergeHostAttribute(attrs, AttributeMarker.Classes, 'A', null, null);
expect(attrs).toEqual([AttributeMarker.Classes, 'CLASS', 'A']);
mergeHostAttribute(attrs, AttributeMarker.Classes, 'X', null, null);
expect(attrs).toEqual([AttributeMarker.Classes, 'CLASS', 'A', 'X']);
mergeHostAttribute(attrs, AttributeMarker.Classes, 'CLASS', null, null);
expect(attrs).toEqual([AttributeMarker.Classes, 'CLASS', 'A', 'X']);
});
it('should add new styles', () => {
const attrs: TAttributes = [];
mergeHostAttribute(attrs, AttributeMarker.Styles, 'Style', null, 'v1');
expect(attrs).toEqual([AttributeMarker.Styles, 'Style', 'v1']);
mergeHostAttribute(attrs, AttributeMarker.Styles, 'A', null, 'v2');
expect(attrs).toEqual([AttributeMarker.Styles, 'Style', 'v1', 'A', 'v2']);
mergeHostAttribute(attrs, AttributeMarker.Styles, 'X', null, 'v3');
expect(attrs).toEqual([AttributeMarker.Styles, 'Style', 'v1', 'A', 'v2', 'X', 'v3']);
mergeHostAttribute(attrs, AttributeMarker.Styles, 'Style', null, 'new');
expect(attrs).toEqual([AttributeMarker.Styles, 'Style', 'new', 'A', 'v2', 'X', 'v3']);
});
it('should keep different types together', () => {
const attrs: TAttributes = [];
mergeHostAttribute(attrs, -1, 'Key', null, 'value');
expect(attrs).toEqual(['Key', 'value']);
mergeHostAttribute(attrs, AttributeMarker.Classes, 'CLASS', null, null);
expect(attrs).toEqual(['Key', 'value', AttributeMarker.Classes, 'CLASS']);
mergeHostAttribute(attrs, AttributeMarker.Styles, 'Style', null, 'v1');
expect(attrs).toEqual([
'Key', 'value', AttributeMarker.Classes, 'CLASS', AttributeMarker.Styles, 'Style', 'v1'
]);
mergeHostAttribute(attrs, -1, 'Key2', null, 'value2');
expect(attrs).toEqual([
'Key', 'value', 'Key2', 'value2', AttributeMarker.Classes, 'CLASS', AttributeMarker.Styles,
'Style', 'v1'
]);
mergeHostAttribute(attrs, AttributeMarker.Classes, 'CLASS2', null, null);
expect(attrs).toEqual([
'Key', 'value', 'Key2', 'value2', AttributeMarker.Classes, 'CLASS', 'CLASS2',
AttributeMarker.Styles, 'Style', 'v1'
]);
mergeHostAttribute(attrs, AttributeMarker.Styles, 'Style2', null, 'v2');
expect(attrs).toEqual([
'Key', 'value', 'Key2', 'value2', AttributeMarker.Classes, 'CLASS', 'CLASS2',
AttributeMarker.Styles, 'Style', 'v1', 'Style2', 'v2'
]);
mergeHostAttribute(attrs, AttributeMarker.NamespaceURI, 'uri', 'key', 'value');
expect(attrs).toEqual([
'Key', 'value', 'Key2', 'value2', AttributeMarker.NamespaceURI, 'uri', 'key', 'value',
AttributeMarker.Classes, 'CLASS', 'CLASS2', AttributeMarker.Styles, 'Style', 'v1', 'Style2',
'v2'
]);
mergeHostAttribute(attrs, AttributeMarker.NamespaceURI, 'uri', 'key', 'new value');
expect(attrs).toEqual([
'Key', 'value', 'Key2', 'value2', AttributeMarker.NamespaceURI, 'uri', 'key', 'new value',
AttributeMarker.Classes, 'CLASS', 'CLASS2', AttributeMarker.Styles, 'Style', 'v1', 'Style2',
'v2'
]);
});
});
describe('mergeHostAttrs', () => {
it('should ignore nulls/empty', () => {
expect(mergeHostAttrs(null, null)).toEqual(null);
expect(mergeHostAttrs([], null)).toEqual([]);
expect(mergeHostAttrs(null, [])).toEqual(null);
});
it('should copy if dst is null', () => {
expect(mergeHostAttrs(null, ['K', 'v'])).toEqual(['K', 'v']);
expect(mergeHostAttrs(['K', '', 'X', 'x'], ['K', 'v'])).toEqual(['K', 'v', 'X', 'x']);
});
});
});