Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): When using setInput, mark view dirty in same was as `markF… #49711

Closed
wants to merge 1 commit into from

Conversation

atscott
Copy link
Contributor

@atscott atscott commented Apr 5, 2023

…orCheck`

ComponentRef.setInput internally calls markDirtyIfOnPush which only marks the given view as dirty but does not mark parents dirty like ChangeDetectorRef.markForCheck would.

export function markDirtyIfOnPush(lView: LView, viewIndex: number): void {
ngDevMode && assertLView(lView);
const childComponentLView = getComponentLViewByIndex(viewIndex, lView);
if (!(childComponentLView[FLAGS] & LViewFlags.CheckAlways)) {
childComponentLView[FLAGS] |= LViewFlags.Dirty;
}
}

markDirtyIfOnPush has an assumption that it’s being called from the parent’s template. That is, we don’t need to mark dirty to the root, because we’ve already traversed down to it. The function used to only be called during template execution for input bindings but was added to setInput later. It's not a good fit because it means that if you are responding to events such as an emit from an Observable and call setInput, the view of your ComponentRef won't necessarily get checked when change detection runs next. If this lives inside some OnPush component tree that's not already dirty, it only gets refreshed if you also call ChangeDetectorRef.markForCheck in the host component (because it will be "shielded" be a non-dirty parent).

@atscott atscott added area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release labels Apr 5, 2023
@ngbot ngbot bot added this to the Backlog milestone Apr 5, 2023
Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a comment

Choose a reason for hiding this comment

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

Seems like a good direction, will need test + golden symbols update.
Ping me when it is ready for review - I'm curious what code we are going to pull and how it is going to impact size

…orCheck`

`ComponentRef.setInput` internally calls `markDirtyIfOnPush` which only marks
the given view as dirty but does not mark parents dirty like `ChangeDetectorRef.markForCheck` would.
https://github.com/angular/angular/blob/f071224720f8affb97fd32fb5aeaa13155b13693/packages/core/src/render3/instructions/shared.ts#L1018-L1024

`markDirtyIfOnPush` has an assumption that it’s being called from the parent’s template. That is, we don’t need to mark dirty to the root, because we’ve already traversed down to it.
The function used to only be called during template execution for input
bindings but was added to `setInput` later. It's not a good fit because
it means that if you are responding to events such as an emit from an `Observable`
and call `setInput`, the view of your `ComponentRef` won't necessarily get checked
when change detection runs next. If this lives inside some `OnPush` component tree
that's not already dirty, it only gets refreshed if you also call
`ChangeDetectorRef.markForCheck` in the host component (because it will be "shielded" be a non-dirty parent).
@atscott atscott marked this pull request as ready for review April 5, 2023 15:57
@pkozlowski-opensource pkozlowski-opensource added the action: merge The PR is ready for merge by the caretaker label Apr 5, 2023
@atscott atscott added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Apr 5, 2023
@dylhunn
Copy link
Contributor

dylhunn commented Apr 5, 2023

This PR was merged into the repository by commit a4e749f.

@dylhunn dylhunn closed this in a4e749f Apr 5, 2023
@endoffline
Copy link

Hey,

thanks for fixing this issue in v16. As of now we've got this exact update problem in our application. setInput() isn't working exactly for the described reason.

As we are still a month away from the scheduled release of v16 and further away from upgrading our project to the newest version, I'd like to request to backport this fix to v15.1 and v15.2 respectively.

I've created an example using an overlay having a projected component into it. Inputs for the projected component are set via the ComponentRef using setInput(), which doesn't update the component properly if ChangeDetectionStrategy.OnPush is utilized on the parent component. The workaround for this is using markForCheck() after the inputs have been set, yet this shouldn't be necessary as setInput() should take care of that itself.

Example: Hover on the 'Hover me' text to see if the overlay component gets rendered properly and enable and disable markForCheck to activate the workaround.

Stackblitz example using v15.2.6 having buggy behavior:
https://stackblitz.com/edit/angular-p4pqib?file=src/main.ts

Stackblitz example using v16.0.0-next.7 validating this fix as being effective:
https://stackblitz.com/edit/angular-wcryrd?file=src/main.ts

Please backport this fix to v15.1 and 15.2. Thanks!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants