juliangarnier / anime

JavaScript animation engine
https://animejs.com
MIT License
50.45k stars 3.69k forks source link

Feature/resolve evenbetter #762

Open gillyspy opened 3 years ago

gillyspy commented 3 years ago

the promise behind an animation is a bit awkward to use. It is a deferred pattern that the anime controls internally and recreates. There are 2 situations that are problematic:

Situation A

because the resolve does not return the instance i find that i am regularly assigning a variable to the animation and then referring to that in promises. This is a bit awkward, especially for combined promises (like race, etc)

Promise.race( [anime({targets : X}), anime({targets:Y})] )
   .then(()=>UmWhoWon())

after this PR

Promise.race( [anime({targets : X}), anime({targets:Y})] )
   .then(winner=> chickenDinner(winner) )

Situation B

An instance that is not in a timeline will never really resolve if there is no timeline attached but the animation is removed. This is because there is an internal pause that user-code cannot know about; e.g.:

const a = anime({ targets : X});  
a.finished.then(a=>ohShitImBored() ); 
a.remove('*'); // this will remove all (selector based) targets and imply a pause

after this PR

const ImTheBossOfMe = function(a){
   //some stuff
   a.play();
}
const a = anime({ targets : X, onPause: ImTheBossOfMe });  
a.finished.then(a=>definitelyGettingHereNow() ); 
a.remove('*'); // this will remove all (selector based) targets and imply a pause...BUT WHO CARES NOW 😄 
gillyspy commented 3 years ago

@juliangarnier are you still actively developing?

in case you are wondering this feature does not really change any of the current behaviour but makes promises a more reasonable use-case here.

juliangarnier commented 3 years ago

Nice!

Yes I am, v3.3.0 is coming along nicely, but there are still a lot to do.

Can you rebase this PR on the v3.3.0 branch ? I'm adding tests to 3.3, it would be nice if you can update tests/promises.test.js to reflect the changes of this PR.

Thanks