Skip to content

Commit

Permalink
Merge pull request #4392 from plotly/resize-reject
Browse files Browse the repository at this point in the history
Plots.resize: promises should always be resolved
  • Loading branch information
antoinerg authored Dec 5, 2019
2 parents 9484b7e + 14f518f commit 0be6afd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ plots.redrawText = function(gd) {
plots.resize = function(gd) {
gd = Lib.getGraphDiv(gd);

return new Promise(function(resolve, reject) {
var resolveLastResize;
var p = new Promise(function(resolve, reject) {
if(!gd || Lib.isHidden(gd)) {
reject(new Error('Resize must be passed a displayed plot div element.'));
}

if(gd._redrawTimer) clearTimeout(gd._redrawTimer);
if(gd._resolveResize) resolveLastResize = gd._resolveResize;
gd._resolveResize = resolve;

gd._redrawTimer = setTimeout(function() {
// return if there is nothing to resize or is hidden
Expand All @@ -101,10 +104,17 @@ plots.resize = function(gd) {

Registry.call('relayout', gd, {autosize: true}).then(function() {
gd.changed = oldchanged;
resolve(gd);
// Only resolve if a new call hasn't been made!
if(gd._resolveResize === resolve) {
delete gd._resolveResize;
resolve(gd);
}
});
}, 100);
});

if(resolveLastResize) resolveLastResize(p);
return p;
};


Expand Down
25 changes: 25 additions & 0 deletions test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,31 @@ describe('Test Plots', function() {
.then(done);
});
});

describe('returns Promises', function() {
afterEach(destroyGraphDiv);

it('should resolve them all', function(done) {
gd = createGraphDiv();
var p = [];
Plotly.newPlot(gd, [{y: [5, 2, 5]}])
.then(function() {
gd.style.width = '500px';
gd.style.height = '500px';
p.push(Plotly.Plots.resize(gd));
p.push(Plotly.Plots.resize(gd));
p.push(Plotly.Plots.resize(gd));
return Promise.all(p);
})
.then(function(v) {
// Make sure they all resolve to the same value
expect(v[0]).toEqual(v[1]);
expect(v[1]).toEqual(v[2]);
})
.catch(failTest)
.then(done);
});
});
});

describe('Plots.purge', function() {
Expand Down

0 comments on commit 0be6afd

Please sign in to comment.