Skip to content

fix(virtual-scroll): emit on viewChange inside the NgZone #12873

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 2 commits into from
Aug 29, 2018
Merged
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
6 changes: 4 additions & 2 deletions src/cdk/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
IterableDiffer,
IterableDiffers,
NgIterable,
NgZone,
OnDestroy,
SkipSelf,
TemplateRef,
Expand Down Expand Up @@ -166,14 +167,15 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
/** The set of available differs. */
private _differs: IterableDiffers,
/** The virtual scrolling viewport that these items are being rendered in. */
@SkipSelf() private _viewport: CdkVirtualScrollViewport) {
@SkipSelf() private _viewport: CdkVirtualScrollViewport,
ngZone: NgZone) {
Copy link
Member

Choose a reason for hiding this comment

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

I left this before, but I think it got lost between commits: shouldn't we make this an optional parameter with a breaking change for the next major?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

virtual scroll isn't released yet, so it's not necessary

this.dataStream.subscribe(data => {
this._data = data;
this._onRenderedDataChange();
});
this._viewport.renderedRangeStream.pipe(takeUntil(this._destroyed)).subscribe(range => {
this._renderedRange = range;
this.viewChange.next(this._renderedRange);
ngZone.run(() => this.viewChange.next(this._renderedRange));
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to write a test for this. Something like:

const spy = jasmine.createSpy('something');

renderedRangeStream.subscribe(() => spy(NgZone.isInAngularZone()));

expect(spy).toHaveBeenCalledWith(true);

this._onRenderedDataChange();
});
this._viewport.attach(this);
Expand Down
9 changes: 9 additions & 0 deletions src/cdk/scrolling/virtual-scroll-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {dispatchFakeEvent} from '@angular/cdk/testing';
import {
Component,
Input,
NgZone,
TrackByFunction,
ViewChild,
ViewContainerRef,
Expand Down Expand Up @@ -603,6 +604,13 @@ describe('CdkVirtualScrollViewport', () => {
fixture.destroy();
expect(dispatcher.deregister).toHaveBeenCalledWith(testComponent.viewport);
})));

it('should emit on viewChange inside the Angular zone', fakeAsync(() => {
const zoneTest = jasmine.createSpy('zone test');
testComponent.virtualForOf.viewChange.subscribe(() => zoneTest(NgZone.isInAngularZone()));
finishInit(fixture);
expect(zoneTest).toHaveBeenCalledWith(true);
}));
});

describe('with RTL direction', () => {
Expand Down Expand Up @@ -766,6 +774,7 @@ function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
})
class FixedSizeVirtualScroll {
@ViewChild(CdkVirtualScrollViewport) viewport: CdkVirtualScrollViewport;
@ViewChild(CdkVirtualForOf) virtualForOf: CdkVirtualForOf<any>;
@ViewChild(CdkVirtualForOf, {read: ViewContainerRef}) virtualForViewContainer: ViewContainerRef;

@Input() orientation = 'vertical';
Expand Down