Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {diPublicInInjector, getOrCreateNodeInjectorForNode, NodeInjector} from '
import {throwProviderNotFoundError} from './errors_di';
import {registerPostOrderHooks} from './hooks';
import {reportUnknownPropertyError} from './instructions/element_validation';
import {addToViewTree, CLEAN_PROMISE, createLView, createTView, getOrCreateTComponentView, getOrCreateTNode, initTNodeFlags, instantiateRootComponent, invokeHostBindingsInCreationMode, locateHostElement, markAsComponentHost, markDirtyIfOnPush, registerHostBindingOpCodes, renderView, setInputsForProperty} from './instructions/shared';
import {addToViewTree, CLEAN_PROMISE, createLView, createTView, getOrCreateTComponentView, getOrCreateTNode, initTNodeFlags, instantiateRootComponent, invokeHostBindingsInCreationMode, locateHostElement, markAsComponentHost, markDirtyIfOnPush, registerHostBindingOpCodes, renderView, setInput} from './instructions/shared';
import {ComponentDef, RenderFlags} from './interfaces/definition';
import {PropertyAliasValue, TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeType} from './interfaces/node';
import {PlayerHandler} from './interfaces/player';
Expand Down Expand Up @@ -288,17 +288,21 @@ export class ComponentRef<T> extends AbstractComponentRef<T> {
let dataValue: PropertyAliasValue|undefined;
if (inputData !== null && (dataValue = inputData[name])) {
const lView = this._rootLView;
setInputsForProperty(lView[TVIEW], lView, dataValue, name, value);
markDirtyIfOnPush(lView, this._tNode.index);
} else {
if (ngDevMode) {
const cmpNameForError = stringifyForError(this.componentType);
let message =
`Can't set value of the '${name}' input on the '${cmpNameForError}' component. `;
message += `Make sure that the '${
name}' property is annotated with @Input() or a mapped @Input('${name}') exists.`;
reportUnknownPropertyError(message);
for (let i = 0; i < dataValue.length; i += 2) {
const index = dataValue[i] as number;
if (this._rootLView[index] === this.instance) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figuring out which inputs belong to this instance like this felt a little weird, but the only alternative would be to look at the index and I couldn't find a good way of getting the index of the component instance within the root LView.

setInput(lView[TVIEW], this.instance, index, name, dataValue[i + 1] as string, value);
markDirtyIfOnPush(lView, this._tNode.index);
break;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This break may be problematic if we somehow end up with multiple inputs on the same instance with the same name. Not sure if that's actually possible, but I'm open to removing it.

}
}
} else if (ngDevMode) {
const cmpNameForError = stringifyForError(this.componentType);
let message =
`Can't set value of the '${name}' input on the '${cmpNameForError}' component. `;
message += `Make sure that the '${
name}' property is annotated with @Input() or a mapped @Input('${name}') exists.`;
reportUnknownPropertyError(message);
}
}

Expand Down Expand Up @@ -435,7 +439,7 @@ export function createRootComponentView(
* Creates a root component and sets it up with features and host bindings.Shared by
* renderComponent() and ViewContainerRef.createComponent().
*/
export function createRootComponent<T>(
function createRootComponent<T>(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change to the rest of the fix, I just saw that this function is only used within the file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we move those refactor changes to a separate (refactor) commit?

componentView: LView, componentDef: ComponentDef<T>, rootLView: LView, rootContext: RootContext,
hostFeatures: HostFeature[]|null): any {
const tView = rootLView[TVIEW];
Expand Down
34 changes: 25 additions & 9 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1992,17 +1992,33 @@ export function handleError(lView: LView, error: any): void {
*/
export function setInputsForProperty(
tView: TView, lView: LView, inputs: PropertyAliasValue, publicName: string, value: any): void {
for (let i = 0; i < inputs.length;) {
const index = inputs[i++] as number;
const privateName = inputs[i++] as string;
for (let i = 0; i < inputs.length; i += 2) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is also unrelated to the fix, but I think that it's more readable like this compared to incrementing i from within the loop. I'm fine with reverting it as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we move those refactor changes to a separate (refactor) commit?

const index = inputs[i] as number;
const privateName = inputs[i + 1] as string;
const instance = lView[index];
ngDevMode && assertIndexInRange(lView, index);
const def = tView.data[index] as DirectiveDef<any>;
if (def.setInput !== null) {
def.setInput!(instance, value, publicName, privateName);
} else {
instance[privateName] = value;
}
setInput(tView, instance, index, publicName, privateName, value);
}
}

/**
* Sets a single input on a directive instance.
*
* @param tView Current TView.
* @param instance Directive instance to which to write the input.
* @param index Index at which to find the directive's defintion in the TView.
* @param publicName Public name of the input.
* @param privateName Private name of the input.
* @param value New value that should be assigned.
*/
export function setInput(
tView: TView, instance: any, index: number, publicName: string, privateName: string,
value: unknown) {
const def = tView.data[index] as DirectiveDef<any>;
if (def.setInput !== null) {
def.setInput(instance, value, publicName, privateName);
} else {
instance[privateName] = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setInputsForProperty"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "maybeWrapInNotSelector"
},
Expand Down Expand Up @@ -966,7 +969,7 @@
"name": "setInjectImplementation"
},
{
"name": "setInputsForProperty"
"name": "setInput"
},
{
"name": "setInputsFromAttrs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setInputsForProperty"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setInputsForProperty"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@
{
"name": "makeRecord"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "maybeWrapInNotSelector"
},
Expand Down Expand Up @@ -713,6 +716,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setSelectedIndex"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "markViewDirty"
},
Expand Down Expand Up @@ -1824,7 +1827,7 @@
"name": "setInjectImplementation"
},
{
"name": "setInputsForProperty"
"name": "setInput"
},
{
"name": "setInputsFromAttrs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@
{
"name": "makeRecord"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "maybeWrapInNotSelector"
},
Expand Down Expand Up @@ -803,6 +806,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setSelectedIndex"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInput"
},
{
"name": "setInputsForProperty"
},
Expand Down