Skip to content

Fix race condition in animation resolution #1108

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

Merged
merged 7 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 21 additions & 3 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,20 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
}
}

// Execute a callback after the wrapper function has been called n times.
// This is used to defer the resolution until a transition has resovled *and*
// the frame has completed. If it's not done this way, then we get a race
// condition in which the animation might resolve before a transition is complete
// or vice versa.
function callbackOnNthTime(cb, n) {
var cnt = 0;
return function() {
if(cb && ++cnt === n) {
return cb();
}
};
}

return new Promise(function(resolve, reject) {
function discardExistingFrames() {
if(trans._frameQueue.length === 0) {
Expand Down Expand Up @@ -2264,7 +2278,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
// loop (which may exist as a result of a *different* .animate call)
// still resolves or rejecdts this .animate call's promise. once it's
// complete.
nextFrame.onComplete = resolve;
nextFrame.onComplete = callbackOnNthTime(resolve, 2);
Copy link
Contributor Author

@rreusser rreusser Nov 9, 2016

Choose a reason for hiding this comment

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

this must now be executed in two places before the transition is registered as having completed. That prevents it from resolving before the transition is actually complete. The transition duration is clipped to no greater than the frame duration, but this PR exists because that still didn't ensure that the promise resolved after both had completed, which made ordering difficult to rely upon.

nextFrame.onInterrupt = reject;
}

Expand Down Expand Up @@ -2302,7 +2316,6 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
// Execute the callback and unset it to ensure it doesn't
// accidentally get called twice
trans._currentFrame.onComplete();
trans._currentFrame.onComplete = null;
}

var newFrame = trans._currentFrame = trans._frameQueue.shift();
Expand All @@ -2322,7 +2335,12 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
helpers.coerceTraceIndices(gd, newFrame.frame.traces),
newFrame.frameOpts,
newFrame.transitionOpts
);
).then(function() {
if(newFrame.onComplete) {
newFrame.onComplete();
}

});

gd.emit('plotly_animatingframe', {
name: newFrame.name,
Expand Down
9 changes: 6 additions & 3 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,9 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
var aborted = false;

function executeTransitions() {

gd.emit('plotly_transitioning', []);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Subtle, but I didn't like that this was executed asynchronously with the transition actually starting, hence the move.


return new Promise(function(resolve) {
// This flag is used to disabled things like autorange:
gd._transitioning = true;
Expand Down Expand Up @@ -1828,13 +1831,13 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)

var seq = [plots.previousPromises, interruptPreviousTransitions, prepareTransitions, executeTransitions];


var transitionStarting = Lib.syncOrAsync(seq, gd);

if(!transitionStarting || !transitionStarting.then) transitionStarting = Promise.resolve();
if(!transitionStarting || !transitionStarting.then) {
transitionStarting = Promise.resolve();
}

return transitionStarting.then(function() {
gd.emit('plotly_transitioning', []);
return gd;
});
};
Expand Down
85 changes: 85 additions & 0 deletions test/jasmine/tests/animate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,88 @@ describe('Test animate API', function() {
});
});
});

describe('layout animation', function() {
'use strict';

var gd;
var dur = 30;

beforeEach(function(done) {
gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});

afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});

it('redraws after a layout animation', function(done) {
var redraws = 0;
gd.on('plotly_redraw', function() {redraws++;});

Plotly.animate(gd,
{layout: {'xaxis.range': [0, 1]}},
{frame: {redraw: true, duration: dur}, transition: {duration: dur}}
).then(function() {
// Something is delayed a single tick, it seems, so the redraw
// isn't triggered until next tick:
expect(redraws).toBe(1);
Copy link
Contributor

@etpinard etpinard Nov 10, 2016

Choose a reason for hiding this comment

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

So, if I get this right, before this PR, redraws === 2 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not quite, but similar. Sometimes it was zero because the promise resolved before both the frame and the transition were registered as complete. Now the promise doesn't resolve until both are done—which shouldn't be more than a couple ms difference, but that's enough to mess up the ordering.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep that makes sense. Thanks for the info!

}).catch(fail).then(done);
});

it('forces a relayout after layout animations', function(done) {
var relayouts = 0;
var restyles = 0;
var redraws = 0;
gd.on('plotly_relayout', function() {relayouts++;});
gd.on('plotly_restyle', function() {restyles++;});
gd.on('plotly_redraw', function() {redraws++;});

Plotly.animate(gd,
{layout: {'xaxis.range': [0, 1]}},
{frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
// Something is delayed a single tick, it seems, so the redraw
// isn't triggered until next tick:
expect(relayouts).toBe(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice test 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. I removed the requestAnimationFrame around this, so the comment is now incorrect. 😭

Copy link
Contributor Author

@rreusser rreusser Nov 8, 2016

Choose a reason for hiding this comment

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

@etpinard removed this comment and added a couple other very minor animation tweaks that drop callbacks if the plot is purged before they're executed—which turns out to be a great way to break the tests one out of every hundred times 😬

(The extra fix was mainly added here to get it to run circle-ci again without the failing enterprise thing)

expect(restyles).toBe(0);
expect(redraws).toBe(0);
}).catch(fail).then(done);
});

it('triggers plotly_animated after a single layout animation', function(done) {
var animateds = 0;
gd.on('plotly_animated', function() {animateds++;});

Plotly.animate(gd, [
{layout: {'xaxis.range': [0, 1]}},
], {frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
// Wait a bit just to be sure:
setTimeout(function() {
expect(animateds).toBe(1);
done();
}, dur);
});
});

it('triggers plotly_animated after a multi-step layout animation', function(done) {
var animateds = 0;
gd.on('plotly_animated', function() {animateds++;});

Plotly.animate(gd, [
{layout: {'xaxis.range': [0, 1]}},
{layout: {'xaxis.range': [2, 4]}},
], {frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
// Wait a bit just to be sure:
setTimeout(function() {
expect(animateds).toBe(1);
done();
}, dur);
});
});
});