Skip to content

Commit

Permalink
perf(ripple): avoid triggering change detection (#3066)
Browse files Browse the repository at this point in the history
* perf(ripple): avoid triggering change detection

Prevents the ripple event handlers from triggering change detection.

Related to #2985.

* chore: rename test
  • Loading branch information
crisbeto authored and andrewseguin committed Feb 17, 2017
1 parent 5cdeb75 commit 1a67107
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/lib/core/ripple/ripple-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ export class RippleRenderer {

if (element) {
// If the element is not null, register all event listeners on the trigger element.
this._triggerEvents.forEach((fn, type) => element.addEventListener(type, fn));
this._ngZone.runOutsideAngular(() => {
this._triggerEvents.forEach((fn, type) => element.addEventListener(type, fn));
});
}

this._triggerElement = element;
}

/** Listener being called on mousedown event. */
private onMousedown(event: MouseEvent) {
if (this.rippleDisabled) {
return;
if (!this.rippleDisabled) {
this._isMousedown = true;
this.fadeInRipple(event.pageX, event.pageY, this.rippleConfig);
}

this._isMousedown = true;
this.fadeInRipple(event.pageX, event.pageY, this.rippleConfig);
}

/** Listener being called on mouseup event. */
Expand Down
17 changes: 14 additions & 3 deletions src/lib/core/ripple/ripple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ describe('MdRipple', () => {
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
});

it('does not run events inside the NgZone', () => {
const spy = jasmine.createSpy('zone unstable callback');
const subscription = fixture.ngZone.onUnstable.subscribe(spy);

dispatchMouseEvent('mousedown');
dispatchMouseEvent('mouseup');

expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});

describe('when page is scrolled', () => {
const startingWindowWidth = window.innerWidth;
const startingWindowHeight = window.innerHeight;
Expand Down Expand Up @@ -374,7 +385,7 @@ describe('MdRipple', () => {

@Component({
template: `
<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
style="position: relative; width:300px; height:200px;">
</div>
`,
Expand All @@ -387,7 +398,7 @@ class BasicRippleContainer {
template: `
<div id="container" style="position: relative; width:300px; height:200px;"
mat-ripple
[mdRippleSpeedFactor]="0"
[mdRippleSpeedFactor]="0"
[mdRippleTrigger]="trigger"
[mdRippleCentered]="centered"
[mdRippleRadius]="radius"
Expand All @@ -406,7 +417,7 @@ class RippleContainerWithInputBindings {
@ViewChild(MdRipple) ripple: MdRipple;
}

@Component({ template: `<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
@Component({ template: `<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
*ngIf="!isDestroyed"></div>` })
class RippleContainerWithNgIf {
@ViewChild(MdRipple) ripple: MdRipple;
Expand Down

0 comments on commit 1a67107

Please sign in to comment.