Skip to content

Commit

Permalink
perf: remove persistent global scroll listener
Browse files Browse the repository at this point in the history
* Removes the global scroll listener from the viewport ruler, which was being used to invalidate the scroll position cache. Caching the scroll position is pointless since it'll be invalidated very frequently. Since most Material components depend on the `ViewportRuler` one way or another, it meant that there was a good chance that the scroll listener would stay around forever.
* Fixes an issue that caused the scroll dispatcher not to remove its global scroll listener, if there are any scrollables on the page, even though nothing was subscribed to it.

Fixes angular#6882.
  • Loading branch information
crisbeto committed Oct 5, 2017
1 parent 630dfad commit aabdc3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/cdk/scrolling/scroll-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ describe('Scroll Dispatcher', () => {
'Expected global listeners to have been removed after the subscription has stopped.');
});

it('should remove global listeners on unsubscribe, despite any other live scrollables', () => {
const fixture = TestBed.createComponent(NestedScrollingComponent);
fixture.detectChanges();

expect(scroll._globalSubscription).toBeNull('Expected no global listeners on init.');
expect(scroll.scrollableReferences.size).toBe(4, 'Expected multiple scrollables');

const subscription = scroll.scrolled(0, () => {});

expect(scroll._globalSubscription).toBeTruthy(
'Expected global listeners after a subscription has been added.');

subscription.unsubscribe();

expect(scroll._globalSubscription).toBeNull(
'Expected global listeners to have been removed after the subscription has stopped.');
expect(scroll.scrollableReferences.size)
.toBe(4, 'Expected scrollable count to stay the same');
});

});
});

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/scrolling/scroll-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ScrollDispatcher {
subscription.add(() => {
this._scrolledCount--;

if (this._globalSubscription && !this.scrollableReferences.size && !this._scrolledCount) {
if (this._globalSubscription && !this._scrolledCount) {
this._globalSubscription.unsubscribe();
this._globalSubscription = null;
}
Expand Down
23 changes: 9 additions & 14 deletions src/cdk/scrolling/viewport-ruler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import {Injectable, Optional, SkipSelf, NgZone, OnDestroy} from '@angular/core';
import {Platform} from '@angular/cdk/platform';
import {ScrollDispatcher} from './scroll-dispatcher';
import {Observable} from 'rxjs/Observable';
import {fromEvent} from 'rxjs/observable/fromEvent';
import {merge} from 'rxjs/observable/merge';
Expand All @@ -32,23 +31,20 @@ export class ViewportRuler implements OnDestroy {
/** Stream of viewport change events. */
private _change: Observable<Event>;

/** Subscriptions to streams that invalidate the cached viewport dimensions. */
private _invalidateCacheSubscriptions: Subscription[];
/** Subscription to streams that invalidate the cached viewport dimensions. */
private _invalidateCache: Subscription;

constructor(platform: Platform, ngZone: NgZone, scrollDispatcher: ScrollDispatcher) {
constructor(platform: Platform, ngZone: NgZone) {
this._change = platform.isBrowser ? ngZone.runOutsideAngular(() => {
return merge<Event>(fromEvent(window, 'resize'), fromEvent(window, 'orientationchange'));
}) : observableOf();

// Subscribe to scroll and resize events and update the document rectangle on changes.
this._invalidateCacheSubscriptions = [
scrollDispatcher.scrolled(0, () => this._cacheViewportGeometry()),
this.change().subscribe(() => this._cacheViewportGeometry())
];
// Update the cached viewport size on changes.
this._invalidateCache = this.change().subscribe(() => this._cacheViewportGeometry());
}

ngOnDestroy() {
this._invalidateCacheSubscriptions.forEach(subscription => subscription.unsubscribe());
this._invalidateCache.unsubscribe();
}

/** Gets a ClientRect for the viewport's bounds. */
Expand Down Expand Up @@ -125,15 +121,14 @@ export class ViewportRuler implements OnDestroy {
/** @docs-private */
export function VIEWPORT_RULER_PROVIDER_FACTORY(parentRuler: ViewportRuler,
platform: Platform,
ngZone: NgZone,
scrollDispatcher: ScrollDispatcher) {
return parentRuler || new ViewportRuler(platform, ngZone, scrollDispatcher);
ngZone: NgZone) {
return parentRuler || new ViewportRuler(platform, ngZone);
}

/** @docs-private */
export const VIEWPORT_RULER_PROVIDER = {
// If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
provide: ViewportRuler,
deps: [[new Optional(), new SkipSelf(), ViewportRuler], Platform, NgZone, ScrollDispatcher],
deps: [[new Optional(), new SkipSelf(), ViewportRuler], Platform, NgZone],
useFactory: VIEWPORT_RULER_PROVIDER_FACTORY
};

0 comments on commit aabdc3b

Please sign in to comment.