From b55b361f97461996eec01c6013c6617601c77b12 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Thu, 17 Dec 2020 22:16:55 +0200 Subject: [PATCH] Reject pending promises when animation is updated (#8184) * Reject pending promises when animation is updated * Add tests --- src/core/core.animation.js | 2 + src/core/core.animations.js | 2 + test/specs/core.animations.tests.js | 89 +++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/core/core.animation.js b/src/core/core.animation.js index f2dcff009..100784e59 100644 --- a/src/core/core.animation.js +++ b/src/core/core.animation.js @@ -46,6 +46,8 @@ export default class Animation { update(cfg, to, date) { const me = this; if (me._active) { + me._notify(false); + const currentValue = me._target[me._prop]; const elapsed = date - me._start; const remain = me._duration - elapsed; diff --git a/src/core/core.animations.js b/src/core/core.animations.js index b51463373..94d285950 100644 --- a/src/core/core.animations.js +++ b/src/core/core.animations.js @@ -131,6 +131,8 @@ export default class Animations { // So any new updates to the shared options are observed awaitAll(target.options.$animations, newOptions).then(() => { target.options = newOptions; + }, () => { + // rejected, noop }); } diff --git a/test/specs/core.animations.tests.js b/test/specs/core.animations.tests.js index 8e76788dc..e82b92a73 100644 --- a/test/specs/core.animations.tests.js +++ b/test/specs/core.animations.tests.js @@ -96,4 +96,93 @@ describe('Chart.animations', function() { done(); }, 300); }); + + it('should not assign shared options to target when animations are cancelled', function(done) { + const chart = { + draw: function() {}, + options: { + animation: { + debug: false + } + } + }; + const anims = new Chart.Animations(chart, {value: {duration: 100}, option: {duration: 200}}); + + const target = { + value: 1, + options: { + option: 2 + } + }; + const sharedOpts = {option: 10, $shared: true}; + + expect(anims.update(target, { + options: sharedOpts + })).toBeTrue(); + + expect(target.options !== sharedOpts).toBeTrue(); + + Chart.animator.start(chart); + + setTimeout(function() { + expect(Chart.animator.running(chart)).toBeTrue(); + Chart.animator.stop(chart); + expect(Chart.animator.running(chart)).toBeFalse(); + + setTimeout(function() { + expect(target.options === sharedOpts).toBeFalse(); + + Chart.animator.remove(chart); + done(); + }, 250); + }, 50); + }); + + + it('should assign final shared options to target after animations complete', function(done) { + const chart = { + draw: function() {}, + options: { + animation: { + debug: false + } + } + }; + const anims = new Chart.Animations(chart, {value: {duration: 100}, option: {duration: 200}}); + + const origOpts = {option: 2}; + const target = { + value: 1, + options: origOpts + }; + const sharedOpts = {option: 10, $shared: true}; + const sharedOpts2 = {option: 20, $shared: true}; + + expect(anims.update(target, { + options: sharedOpts + })).toBeTrue(); + + expect(target.options !== sharedOpts).toBeTrue(); + + Chart.animator.start(chart); + + setTimeout(function() { + expect(Chart.animator.running(chart)).toBeTrue(); + + expect(target.options === origOpts).toBeTrue(); + + expect(anims.update(target, { + options: sharedOpts2 + })).toBeUndefined(); + + expect(target.options === origOpts).toBeTrue(); + + setTimeout(function() { + expect(target.options === sharedOpts2).toBeTrue(); + + Chart.animator.remove(chart); + done(); + }, 250); + }, 50); + }); }); -- 2.47.3