fix(animation): handle promise rejection and avoid throw (#5861)

This commit is contained in:
Nathan Walker
2018-05-31 05:58:50 -07:00
committed by Alexander Vakrilov
parent b77675b312
commit 9308bab172

View File

@@ -74,7 +74,6 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
public iterations: number = 1;
private _resolve;
private _reject;
private _isPlaying: boolean;
private _isForwards: boolean;
private _nativeAnimations: Array<Animation>;
@@ -161,21 +160,19 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
let animation = this._nativeAnimations[0];
this._resetAnimationValues(this._target, animation);
}
this._rejectAnimationFinishedPromise();
this._resetAnimations();
}
public play(view: View): Promise<void> {
if (this._isPlaying) {
const reason = "Keyframe animation is already playing.";
traceWrite(reason, traceCategories.Animation, traceType.warn);
return new Promise<void>((resolve, reject) => {
reject(reason);
traceWrite("Keyframe animation is already playing.", traceCategories.Animation, traceType.warn);
return new Promise<void>(resolve => {
resolve();
});
}
let animationFinishedPromise = new Promise<void>((resolve, reject) => {
let animationFinishedPromise = new Promise<void>(resolve => {
this._resolve = resolve;
this._reject = reject;
});
this._isPlaying = true;
@@ -239,10 +236,10 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
// Catch the animation cancel to prevent unhandled promise rejection warnings
animation.play().then(() => {
this.animate(view, index + 1, iterations);
}, (error: any) => {
traceWrite(typeof error === "string" ? error : error.message, traceCategories.Animation, traceType.warn);
}).catch((error: any) => {
if (error.message.indexOf("Animation cancelled") < 0) {
throw error;
}
traceWrite(typeof error === "string" ? error : error.message, traceCategories.Animation, traceType.warn);
}); // tslint:disable-line
this._nativeAnimations.push(animation);
}
@@ -255,11 +252,10 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
this._resolve();
}
public _rejectAnimationFinishedPromise() {
public _resetAnimations() {
this._nativeAnimations = new Array<Animation>();
this._isPlaying = false;
this._target = null;
this._reject(new Error("Animation cancelled."));
}
private _resetAnimationValues(view: View, animation: Object) {