Skip to content

Commit

Permalink
refactor(core): misc refactoring to support upcoming changes to impro…
Browse files Browse the repository at this point in the history
…ve synthetic host bindings (angular#35884)

This commit performs a few updates to internal functions that would be required in upcoming changes to support synthetic host bindings in Directives.

* the `elementPropertyInternal` function was refactored to accept renderer as an argument (prior to that, there was a function that loads the renderer in some specific way for animation bindings)
* `elementPropertyInternal`, `elementAttributeInternal` and `listenerInternal` functions were updated to have a fixed set of arguments (for better performance)
* `elementPropertyInternal` and `elementAttributeInternal` functions were updated to take `tNode` as an argument instead of passing node index (that was used to retrieve `tNode` internally), in some cases we already have `tNode` available or we can retrieve it from the state

The refactoring was triggered by the need to pass different renderers to the `elementPropertyInternal` to support synthetic host bindings in Directives (see this comment for additional context: https://github.com/angular/angular/pull/35568/files#r388034584).

PR Close angular#35884
  • Loading branch information
AndrewKushnir committed Mar 16, 2020
1 parent e552591 commit 168a393
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 117 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/render3/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ function readCreateOpCodes(
const attrValue = createOpCodes[++i] as string;
// This code is used for ICU expressions only, since we don't support
// directives/components in ICUs, we don't need to worry about inputs here
elementAttributeInternal(elementNodeIndex, attrName, attrValue, tView, lView);
elementAttributeInternal(
getTNode(tView, elementNodeIndex), lView, attrName, attrValue, null, null);
break;
default:
throw new Error(`Unable to determine the type of mutate operation for "${opCode}"`);
Expand Down Expand Up @@ -877,7 +878,9 @@ function readUpdateOpCodes(
case I18nUpdateOpCode.Attr:
const propName = updateOpCodes[++j] as string;
const sanitizeFn = updateOpCodes[++j] as SanitizerFn | null;
elementPropertyInternal(tView, lView, nodeIndex, propName, value, sanitizeFn);
elementPropertyInternal(
tView, getTNode(tView, nodeIndex), lView, propName, value, lView[RENDERER],
sanitizeFn, false);
break;
case I18nUpdateOpCode.Text:
textBindingInternal(lView, nodeIndex, value);
Expand Down Expand Up @@ -1042,7 +1045,7 @@ function i18nAttributesFirstPass(lView: LView, tView: TView, index: number, valu
// Set attributes for Elements only, for other types (like ElementContainer),
// only set inputs below
if (tNode.type === TNodeType.Element) {
elementAttributeInternal(previousElementIndex, attrName, value, tView, lView);
elementAttributeInternal(tNode, lView, attrName, value, null, null);
}
// Check if that attribute is a directive input
const dataValue = tNode.inputs !== null && tNode.inputs[attrName];
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/render3/instructions/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import {bindingUpdated} from '../bindings';
import {SanitizerFn} from '../interfaces/sanitization';
import {getLView, getSelectedIndex, getTView, nextBindingIndex} from '../state';
import {getLView, getSelectedIndex, getSelectedTNode, getTView, nextBindingIndex} from '../state';
import {elementAttributeInternal, storePropertyBindingMetadata} from './shared';


Expand All @@ -31,10 +31,10 @@ export function ɵɵattribute(
const lView = getLView();
const bindingIndex = nextBindingIndex();
if (bindingUpdated(lView, bindingIndex, value)) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(nodeIndex, name, value, tView, lView, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(tView.data, nodeIndex, 'attr.' + name, bindingIndex);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, name, value, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(tView.data, tNode, 'attr.' + name, bindingIndex);
}
return ɵɵattribute;
}
73 changes: 28 additions & 45 deletions packages/core/src/render3/instructions/attribute_interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {SanitizerFn} from '../interfaces/sanitization';
import {getBindingIndex, getLView, getSelectedIndex, getTView} from '../state';
import {getBindingIndex, getLView, getSelectedTNode, getTView} from '../state';
import {NO_CHANGE} from '../tokens';
import {interpolation1, interpolation2, interpolation3, interpolation4, interpolation5, interpolation6, interpolation7, interpolation8, interpolationV} from './interpolation';
import {elementAttributeInternal, storePropertyBindingMetadata} from './shared';
Expand Down Expand Up @@ -43,13 +43,11 @@ export function ɵɵattributeInterpolate1(
const lView = getLView();
const interpolatedValue = interpolation1(lView, prefix, v0, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode &&
storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 1, prefix, suffix);
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 1, prefix, suffix);
}
return ɵɵattributeInterpolate1;
}
Expand Down Expand Up @@ -86,13 +84,11 @@ export function ɵɵattributeInterpolate2(
const lView = getLView();
const interpolatedValue = interpolation2(lView, prefix, v0, i0, v1, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode &&
storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 2, prefix, i0, suffix);
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 2, prefix, i0, suffix);
}
return ɵɵattributeInterpolate2;
}
Expand Down Expand Up @@ -132,12 +128,10 @@ export function ɵɵattributeInterpolate3(
const lView = getLView();
const interpolatedValue = interpolation3(lView, prefix, v0, i0, v1, i1, v2, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 3, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 3, prefix, i0,
i1, suffix);
}
return ɵɵattributeInterpolate3;
Expand Down Expand Up @@ -181,12 +175,10 @@ export function ɵɵattributeInterpolate4(
const lView = getLView();
const interpolatedValue = interpolation4(lView, prefix, v0, i0, v1, i1, v2, i2, v3, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 4, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 4, prefix, i0,
i1, i2, suffix);
}
return ɵɵattributeInterpolate4;
Expand Down Expand Up @@ -233,12 +225,10 @@ export function ɵɵattributeInterpolate5(
const interpolatedValue =
interpolation5(lView, prefix, v0, i0, v1, i1, v2, i2, v3, i3, v4, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 5, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 5, prefix, i0,
i1, i2, i3, suffix);
}
return ɵɵattributeInterpolate5;
Expand Down Expand Up @@ -287,12 +277,10 @@ export function ɵɵattributeInterpolate6(
const interpolatedValue =
interpolation6(lView, prefix, v0, i0, v1, i1, v2, i2, v3, i3, v4, i4, v5, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 6, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 6, prefix, i0,
i1, i2, i3, i4, suffix);
}
return ɵɵattributeInterpolate6;
Expand Down Expand Up @@ -343,12 +331,10 @@ export function ɵɵattributeInterpolate7(
const interpolatedValue =
interpolation7(lView, prefix, v0, i0, v1, i1, v2, i2, v3, i3, v4, i4, v5, i5, v6, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 7, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 7, prefix, i0,
i1, i2, i3, i4, i5, suffix);
}
return ɵɵattributeInterpolate7;
Expand Down Expand Up @@ -401,12 +387,10 @@ export function ɵɵattributeInterpolate8(
const interpolatedValue = interpolation8(
lView, prefix, v0, i0, v1, i1, v2, i2, v3, i3, v4, i4, v5, i5, v6, i6, v7, suffix);
if (interpolatedValue !== NO_CHANGE) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementAttributeInternal(
nodeIndex, attrName, interpolatedValue, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolatedValue, sanitizer, namespace);
ngDevMode && storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName, getBindingIndex() - 8, prefix, i0,
getTView().data, tNode, 'attr.' + attrName, getBindingIndex() - 8, prefix, i0,
i1, i2, i3, i4, i5, i6, suffix);
}
return ɵɵattributeInterpolate8;
Expand Down Expand Up @@ -444,16 +428,15 @@ export function ɵɵattributeInterpolateV(
const lView = getLView();
const interpolated = interpolationV(lView, values);
if (interpolated !== NO_CHANGE) {
const tView = getTView();
const nodeIndex = getSelectedIndex();
elementAttributeInternal(nodeIndex, attrName, interpolated, tView, lView, sanitizer, namespace);
const tNode = getSelectedTNode();
elementAttributeInternal(tNode, lView, attrName, interpolated, sanitizer, namespace);
if (ngDevMode) {
const interpolationInBetween = [values[0]]; // prefix
for (let i = 2; i < values.length; i += 2) {
interpolationInBetween.push(values[i]);
}
storePropertyBindingMetadata(
tView.data, nodeIndex, 'attr.' + attrName,
getTView().data, tNode, 'attr.' + attrName,
getBindingIndex() - interpolationInBetween.length + 1, ...interpolationInBetween);
}
}
Expand Down
18 changes: 10 additions & 8 deletions packages/core/src/render3/instructions/host_property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
*/
import {bindingUpdated} from '../bindings';
import {SanitizerFn} from '../interfaces/sanitization';
import {getLView, getSelectedIndex, getTView, nextBindingIndex} from '../state';
import {RENDERER} from '../interfaces/view';
import {getLView, getSelectedTNode, getTView, nextBindingIndex} from '../state';
import {NO_CHANGE} from '../tokens';

import {elementPropertyInternal, loadComponentRenderer, storePropertyBindingMetadata} from './shared';

/**
Expand All @@ -30,10 +32,10 @@ export function ɵɵhostProperty<T>(
const lView = getLView();
const bindingIndex = nextBindingIndex();
if (bindingUpdated(lView, bindingIndex, value)) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementPropertyInternal(tView, lView, nodeIndex, propName, value, sanitizer, true);
ngDevMode && storePropertyBindingMetadata(tView.data, nodeIndex, propName, bindingIndex);
const tNode = getSelectedTNode();
elementPropertyInternal(tView, tNode, lView, propName, value, lView[RENDERER], sanitizer, true);
ngDevMode && storePropertyBindingMetadata(tView.data, tNode, propName, bindingIndex);
}
return ɵɵhostProperty;
}
Expand Down Expand Up @@ -66,11 +68,11 @@ export function ɵɵupdateSyntheticHostBinding<T>(
const lView = getLView();
const bindingIndex = nextBindingIndex();
if (bindingUpdated(lView, bindingIndex, value)) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementPropertyInternal(
tView, lView, nodeIndex, propName, value, sanitizer, true, loadComponentRenderer);
ngDevMode && storePropertyBindingMetadata(tView.data, nodeIndex, propName, bindingIndex);
const tNode = getSelectedTNode();
const renderer = loadComponentRenderer(tNode, lView);
elementPropertyInternal(tView, tNode, lView, propName, value, renderer, sanitizer, true);
ngDevMode && storePropertyBindingMetadata(tView.data, tNode, propName, bindingIndex);
}
return ɵɵupdateSyntheticHostBinding;
}
12 changes: 7 additions & 5 deletions packages/core/src/render3/instructions/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import {bindingUpdated} from '../bindings';
import {TNode} from '../interfaces/node';
import {SanitizerFn} from '../interfaces/sanitization';
import {LView, TView} from '../interfaces/view';
import {getLView, getSelectedIndex, getTView, nextBindingIndex} from '../state';
import {LView, RENDERER, TView} from '../interfaces/view';
import {getLView, getSelectedTNode, getTView, nextBindingIndex} from '../state';

import {elementPropertyInternal, setInputsForProperty, storePropertyBindingMetadata} from './shared';


Expand All @@ -36,10 +37,11 @@ export function ɵɵproperty<T>(
const lView = getLView();
const bindingIndex = nextBindingIndex();
if (bindingUpdated(lView, bindingIndex, value)) {
const nodeIndex = getSelectedIndex();
const tView = getTView();
elementPropertyInternal(tView, lView, nodeIndex, propName, value, sanitizer);
ngDevMode && storePropertyBindingMetadata(tView.data, nodeIndex, propName, bindingIndex);
const tNode = getSelectedTNode();
elementPropertyInternal(
tView, tNode, lView, propName, value, lView[RENDERER], sanitizer, false);
ngDevMode && storePropertyBindingMetadata(tView.data, tNode, propName, bindingIndex);
}
return ɵɵproperty;
}
Expand Down
Loading

0 comments on commit 168a393

Please sign in to comment.