From deb32ff778b7b2c50dd5268a796de90f8f0dab1f Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Fri, 29 Nov 2019 17:29:08 -0500 Subject: [PATCH 1/5] Plots.resize: reject old promises --- src/plots/plots.js | 3 +++ test/jasmine/tests/plots_test.js | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/plots/plots.js b/src/plots/plots.js index ea831406ad2..9766423033d 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -81,6 +81,9 @@ plots.resize = function(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); gd._redrawTimer = setTimeout(function() { diff --git a/test/jasmine/tests/plots_test.js b/test/jasmine/tests/plots_test.js index 443cdb9ad3e..525eea8dc28 100644 --- a/test/jasmine/tests/plots_test.js +++ b/test/jasmine/tests/plots_test.js @@ -412,6 +412,27 @@ describe('Test Plots', function() { .then(done); }); }); + + describe('returns Promises', function() { + afterEach(destroyGraphDiv); + + it('should reject or 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)); + return Promise.all(p); + }) + .catch(failTest) + .then(done); + }); + }); }); describe('Plots.purge', function() { From 040d6acebcb2352c6fa900b21e7f30e70d0f4bcc Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Wed, 4 Dec 2019 14:34:16 -0500 Subject: [PATCH 2/5] Plots.resize: resolve old promises --- src/plots/plots.js | 11 +++++++---- test/jasmine/tests/plots_test.js | 9 +++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/plots/plots.js b/src/plots/plots.js index 9766423033d..52e0af4568d 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -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 @@ -108,6 +108,9 @@ plots.resize = function(gd) { }); }, 100); }); + + if(resolveLastResize) resolveLastResize(p); + return p; }; diff --git a/test/jasmine/tests/plots_test.js b/test/jasmine/tests/plots_test.js index 525eea8dc28..7f2a938465e 100644 --- a/test/jasmine/tests/plots_test.js +++ b/test/jasmine/tests/plots_test.js @@ -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); }) From 5ee431edc89b69c38cfabb008cf84b35b14f2a8f Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Wed, 4 Dec 2019 18:48:56 -0500 Subject: [PATCH 3/5] Plots.resize: only resolve if a new request hasn't been queued --- src/plots/plots.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/plots/plots.js b/src/plots/plots.js index 52e0af4568d..a3c22957458 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -104,7 +104,8 @@ 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) resolve(gd); }); }, 100); }); From 5e71cffe386bba63078e735476ba1c57b0189163 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Thu, 5 Dec 2019 11:17:34 -0500 Subject: [PATCH 4/5] Plots.resize: delete old promise resolver if present --- src/plots/plots.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/plots/plots.js b/src/plots/plots.js index a3c22957458..3e912716eab 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -105,7 +105,10 @@ plots.resize = function(gd) { Registry.call('relayout', gd, {autosize: true}).then(function() { gd.changed = oldchanged; // Only resolve if a new call hasn't been made! - if(gd._resolveResize === resolve) resolve(gd); + if(gd._resolveResize === resolve) { + delete gd._resolveResize; + resolve(gd); + } }); }, 100); }); From 14f518f964fd4e619da4ac2c50d395bcb3f5550a Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Thu, 5 Dec 2019 12:01:56 -0500 Subject: [PATCH 5/5] Plots.resize: improve test --- test/jasmine/tests/plots_test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/jasmine/tests/plots_test.js b/test/jasmine/tests/plots_test.js index 7f2a938465e..8c3c3388fed 100644 --- a/test/jasmine/tests/plots_test.js +++ b/test/jasmine/tests/plots_test.js @@ -421,11 +421,18 @@ describe('Test Plots', function() { 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); });