Skip to content

virtual-scroll: use detectChanges rather than markForCheck in CdkVirtualScrollViewport #12158

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

Merged
merged 3 commits into from
Jul 12, 2018

Conversation

mmalerba
Copy link
Contributor

No description provided.

@mmalerba mmalerba requested review from amcdnl, jelbourn and crisbeto July 11, 2018 17:26
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jul 11, 2018
@mmalerba
Copy link
Contributor Author

So theoretically this should be a little better for performance, I think.. markForCheck checks from the root and detectChanges just checks the component you call it on. All of the examples I have in the demo app still seem to work fine after this change

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM

this._ngZone.runOutsideAngular(() => {
this._forOf = forOf;
this._forOf.dataStream.pipe(takeUntil(this._detachedSubject)).subscribe(data => {
const len = data.length;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be called something like newLength?

@crisbeto crisbeto added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Jul 11, 2018
@ngbot
Copy link

ngbot bot commented Jul 11, 2018

I see that you just added the pr: merge ready label, but the following checks are still failing:
    failure missing required label: "target: *"
    pending status "continuous-integration/travis-ci/pr" is pending

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

constructor(public elementRef: ElementRef, private _changeDetectorRef: ChangeDetectorRef,
private _ngZone: NgZone, private _sanitizer: DomSanitizer,
@Inject(VIRTUAL_SCROLL_STRATEGY) private _scrollStrategy: VirtualScrollStrategy) {}

ngOnInit() {
// It's still too early to measure the viewport at this point. Deferring with a promise allows
// the Viewport to be rendered with the correct size before we measure.
Promise.resolve().then(() => {
this._ngZone.runOutsideAngular(() => Promise.resolve().then(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Expand comment to explain why you run outside the zone?

@@ -102,37 +102,29 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
/** Observable that emits when the viewport is destroyed. */
private _destroyed = new Subject<void>();

/** Whether there is a pending change detection cycle. */
private _checkPending = false;
Copy link
Member

Choose a reason for hiding this comment

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

_isPendingChangeDetection or _needsChangeDetection?

this._dataLength = len;
this._scrollStrategy.onDataLengthChanged();
}
this._ngZone.runOutsideAngular(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Add comment that explains why this is going out of the zone?

private _doCheck() {
this._checkPending = false;
this._ngZone.run(() => this._changeDetectorRef.detectChanges());
// In order to batch setting the scroll offset together with other DOM writes, we wait until a
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite follow what this comment means about waiting, since detectChanges is synchronous

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but in the _markForCheck method above I'm batching them up using a Promise.resolve(). This way if we change multiple properties it waits until after they're all updated and calls detectChanges once

Copy link
Member

Choose a reason for hiding this comment

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

Should the comment live where the wait is scheduled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I split the comment up to explain the batching part where it actually happens

}

/** Run change detection. */
private _doCheck() {
Copy link
Member

Choose a reason for hiding this comment

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

Call this method something like _detectChangesForScroll?

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn merged commit 3255cf3 into angular:master Jul 12, 2018
}
}

for (let fn of this._runAfterChangeDetection) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use const instead of let

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 added fix to #12183

Copy link
Contributor

@amcdnl amcdnl left a comment

Choose a reason for hiding this comment

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

LGTM

@naveedahmed1
Copy link

Is it merged in 6.4.0? I am using virtual-scroll in a component with changeDetection: ChangeDetectionStrategy.OnPush which was working properly in 6.3.3. With 6.4.0 all I see is a blank space until I remove `changeDetection: ChangeDetectionStrategy.OnPush'

@mmalerba
Copy link
Contributor Author

@naveedahmed1 You are right, it looks like this broke it for parents that are using OnPush strategy. I will create a fix

mmalerba added a commit that referenced this pull request Jul 24, 2018
…12329)

breaks viewports that are inside an `OnPush` component.

Fixes issue introduced by #12158
victoriaaa234 pushed a commit that referenced this pull request Jul 25, 2018
mmalerba added a commit that referenced this pull request Jul 30, 2018
…12329)

breaks viewports that are inside an `OnPush` component.

Fixes issue introduced by #12158
@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 Sep 9, 2019
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 cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants