Skip to content
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(progress-circle): clean up animation on destroy #617

Merged
merged 1 commit into from
Jun 3, 2016
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
21 changes: 21 additions & 0 deletions src/components/progress-circle/progress-circle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ describe('MdProgressCircular', () => {
done();
});
});

it('should clean up the indeterminate animation when the element is destroyed',
(done: () => void) => {
let template = `<md-progress-circle
mode="indeterminate"
*ngIf="!isHidden"></md-progress-circle>`;

builder
.overrideTemplate(TestApp, template)
.createAsync(TestApp)
.then((fixture) => {
fixture.detectChanges();
let progressElement = getChildDebugElement(fixture.debugElement, 'md-progress-circle');
expect(progressElement.componentInstance.interdeterminateInterval).toBeTruthy();

fixture.debugElement.componentInstance.isHidden = true;
fixture.detectChanges();
expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy();
done();
});
});
});


Expand Down
29 changes: 21 additions & 8 deletions src/components/progress-circle/progress-circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
HostBinding,
ChangeDetectorRef,
ChangeDetectionStrategy,
OnDestroy,
Input
} from '@angular/core';

Expand Down Expand Up @@ -41,13 +42,23 @@ type EasingFn = (currentTime: number, startValue: number,
styleUrls: ['progress-circle.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdProgressCircle {
export class MdProgressCircle implements OnDestroy {
/** The id of the last requested animation. */
private _lastAnimationId: number = 0;

/** The id of the indeterminate interval. */
private _interdeterminateInterval: number;

/** @internal */
get interdeterminateInterval() {
Copy link
Member

@jelbourn jelbourn Jun 2, 2016

Choose a reason for hiding this comment

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

This should be marked as /** @internal */

return this._interdeterminateInterval;
}
/** @internal */
set interdeterminateInterval(interval: number) {
clearInterval(this._interdeterminateInterval);
this._interdeterminateInterval = interval;
}

/** The current path value, representing the progres circle. */
private _currentPath: string;
get currentPath() {
Expand All @@ -60,6 +71,11 @@ export class MdProgressCircle {
this._changeDetectorRef.markForCheck();
}

/** Clean up any animations that were running. */
ngOnDestroy() {
this._cleanupIndeterminateAnimation();
}

/**
* Value of the progress circle.
*
Expand Down Expand Up @@ -162,8 +178,8 @@ export class MdProgressCircle {
end = -temp;
};

if (!this._interdeterminateInterval) {
this._interdeterminateInterval = setInterval(
if (!this.interdeterminateInterval) {
this.interdeterminateInterval = setInterval(
animate, duration + 50, 0, false);
animate();
}
Expand All @@ -174,10 +190,7 @@ export class MdProgressCircle {
* Removes interval, ending the animation.
*/
private _cleanupIndeterminateAnimation() {
if (this._interdeterminateInterval) {
clearInterval(this._interdeterminateInterval);
this._interdeterminateInterval = null;
}
this.interdeterminateInterval = null;
}
}

Expand Down Expand Up @@ -219,7 +232,7 @@ function clamp(v: number) {
* Returns the current timestamp either based on the performance global or a date object.
*/
function now() {
if (typeof performance !== 'undefined') {
if (typeof performance !== 'undefined' && performance.now) {
return performance.now();
}
return Date.now();
Expand Down