-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
perf(scroll-dispatcher): avoid triggering change detection on scroll #3687
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import {Injectable, ElementRef, Optional, SkipSelf} from '@angular/core'; | ||
import {Injectable, ElementRef, Optional, SkipSelf, NgZone} from '@angular/core'; | ||
import {Scrollable} from './scrollable'; | ||
import {Subject} from 'rxjs/Subject'; | ||
import {Observable} from 'rxjs/Observable'; | ||
|
@@ -17,6 +17,8 @@ export const DEFAULT_SCROLL_TIME = 20; | |
*/ | ||
@Injectable() | ||
export class ScrollDispatcher { | ||
constructor(private _ngZone: NgZone) { } | ||
|
||
/** Subject for notifying that a registered scrollable reference element has been scrolled. */ | ||
_scrolled: Subject<void> = new Subject<void>(); | ||
|
||
|
@@ -69,10 +71,12 @@ export class ScrollDispatcher { | |
this._scrolledCount++; | ||
|
||
if (!this._globalSubscription) { | ||
this._globalSubscription = Observable.merge( | ||
Observable.fromEvent(window.document, 'scroll'), | ||
Observable.fromEvent(window, 'resize') | ||
).subscribe(() => this._notify()); | ||
this._globalSubscription = this._ngZone.runOutsideAngular(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment explaining why we need to escape from the Angular zone here (and in the directive)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess, although the unit tests could be considered a form of documentation. |
||
return Observable.merge( | ||
Observable.fromEvent(window.document, 'scroll'), | ||
Observable.fromEvent(window, 'resize') | ||
).subscribe(() => this._notify()); | ||
}); | ||
} | ||
|
||
// Note that we need to do the subscribing from here, in order to be able to remove | ||
|
@@ -118,13 +122,14 @@ export class ScrollDispatcher { | |
} | ||
} | ||
|
||
export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher: ScrollDispatcher) { | ||
return parentDispatcher || new ScrollDispatcher(); | ||
export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher: ScrollDispatcher, | ||
ngZone: NgZone) { | ||
return parentDispatcher || new ScrollDispatcher(ngZone); | ||
} | ||
|
||
export const SCROLL_DISPATCHER_PROVIDER = { | ||
// If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one. | ||
provide: ScrollDispatcher, | ||
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher]], | ||
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone], | ||
useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth also adding a unit test that somehow asserts that change detection wasn't run (which is the ultimate goal)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what the test is doing: If we didn't hit the zone, we can assume that change detection didn't run. I can also rework it so it uses a
ngDoCheck
hook with a spy instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair; I was more thinking a test that looked at something in the DOM not being updated, but it's all really the same.