FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(animation): remove throw and fix promise handling by NathanWalker · Pull Request #5861 · NativeScript/NativeScript · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 9 additions & 13 deletions tns-core-modules/ui/animation/keyframe-animation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Just wondering - do you need both error callback and .catch()?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@vakrilov Why not ? if this.animate(view, index + 1, iterations); will throw ( for some reason) , it won't be cached in the callback of , (error: any) =>.
But - it will be catched in the catch.

http://jsbin.com/zujibirese/1/edit?html,js,output

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Due to number of reports around instability here I was keeping for assurance it was stopped.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
Technically the error callback appears preferred as catch internally calls rejected. I would presume an error callback is better everywhere instead of a catch based on way docs have it stated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Although as @RoyiNamir points out I believe there are others cases that having both may provide utmost resiliency against.

}).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);
}
Expand All @@ -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) {
Expand Down

Back | FazBrowse Home | New Git URL