Skip to content

Commit

Permalink
Plots.resize: resolve old promises
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerg committed Dec 4, 2019
1 parent deb32ff commit 040d6ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +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._rejectResize) gd._rejectResize();
gd._rejectResize = reject;

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 @@ -108,6 +108,9 @@ plots.resize = function(gd) {
});
}, 100);
});

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


Expand Down
9 changes: 3 additions & 6 deletions test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,13 @@ describe('Test Plots', function() {
describe('returns Promises', function() {
afterEach(destroyGraphDiv);

it('should reject or resolve them all', function(done) {
it('should resolve them all', function(done) {
gd = createGraphDiv();
var p = [];
Plotly.newPlot(gd, [{y: [5, 2, 5]}])
.then(function() {
// First call should get rejected
p.push(Plotly.Plots.resize(gd).catch(function() {
return Promise.resolve(true);
}));
// because we call the function again within 100ms
p.push(Plotly.Plots.resize(gd));
p.push(Plotly.Plots.resize(gd));
p.push(Plotly.Plots.resize(gd));
return Promise.all(p);
})
Expand Down

0 comments on commit 040d6ac

Please sign in to comment.