Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a585e2

Browse files
committed
Merge pull request plotly#226 from clvmetrics/always-return-promises
Added promise returns to Plotly.plot
2 parents 65167a3 + 79679ee commit 9a585e2

File tree

2 files changed

+172
-7
lines changed

2 files changed

+172
-7
lines changed

src/plot_api/plot_api.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Plotly.plot = function(gd, data, layout, config) {
5959
Events.init(gd);
6060

6161
var okToPlot = Events.triggerHandler(gd, 'plotly_beforeplot', [data, layout, config]);
62-
if(okToPlot===false) return;
62+
if(okToPlot===false) return Promise.reject();
6363

6464
// if there's no data or layout, and this isn't yet a plotly plot
6565
// container, log a warning to help plotly.js users debug
@@ -112,7 +112,7 @@ Plotly.plot = function(gd, data, layout, config) {
112112
// signal to drag handler that after everything else is done
113113
// we need to replot, because something has changed
114114
gd._replotPending = true;
115-
return;
115+
return Promise.reject();
116116
} else {
117117
// we're going ahead with a replot now
118118
gd._replotPending = false;
@@ -1581,7 +1581,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
15811581
}
15821582
else {
15831583
console.log('restyle fail',astr,val,traces);
1584-
return new Promise.reject();
1584+
return Promise.reject();
15851585
}
15861586

15871587
if(Object.keys(aobj).length) gd.changed = true;
@@ -2104,7 +2104,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
21042104
gd = getGraphDiv(gd);
21052105

21062106
if(gd.framework && gd.framework.isPolar) {
2107-
return new Promise.resolve(gd);
2107+
return Promise.resolve(gd);
21082108
}
21092109

21102110
var layout = gd.layout,
@@ -2122,7 +2122,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
21222122
else if(Lib.isPlainObject(astr)) aobj = astr;
21232123
else {
21242124
console.log('relayout fail',astr,val);
2125-
return new Promise.reject();
2125+
return Promise.reject();
21262126
}
21272127

21282128
if(Object.keys(aobj).length) gd.changed = true;

test/jasmine/tests/plot_promise_test.js

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Plotly = require('@src/plotly');
2+
var Events = require('@src/lib/events');
23
var createGraphDiv = require('../assets/create_graph_div');
34
var destroyGraphDiv = require('../assets/destroy_graph_div');
45

@@ -29,6 +30,64 @@ describe('Plotly.___ methods', function() {
2930
});
3031
});
3132

33+
describe('Plotly.plot promise', function() {
34+
var gd,
35+
promise,
36+
promiseRejected = false;
37+
38+
beforeEach(function(done) {
39+
var data = [{ x: [1,2,3], y: [4,5,6] }];
40+
41+
gd = createGraphDiv();
42+
43+
Events.init(gd);
44+
45+
gd.on('plotly_beforeplot', function() {
46+
return false;
47+
});
48+
49+
promise = Plotly.plot(gd, data, {});
50+
51+
promise.then(null, function(){
52+
promiseRejected = true;
53+
done();
54+
});
55+
});
56+
57+
afterEach(destroyGraphDiv);
58+
59+
it('should be rejected when plotly_beforeplot event handlers return false', function() {
60+
expect(promiseRejected).toBe(true);
61+
});
62+
});
63+
64+
describe('Plotly.plot promise', function() {
65+
var gd,
66+
promise,
67+
promiseRejected = false;
68+
69+
beforeEach(function(done) {
70+
var data = [{ x: [1,2,3], y: [4,5,6] }];
71+
72+
gd = createGraphDiv();
73+
74+
gd._dragging = true;
75+
76+
promise = Plotly.plot(gd, data, {});
77+
78+
promise.then(null, function(){
79+
promiseRejected = true;
80+
done();
81+
});
82+
});
83+
84+
afterEach(destroyGraphDiv);
85+
86+
it('should reject the promise when graph is being dragged', function() {
87+
expect(promiseRejected).toBe(true);
88+
});
89+
});
90+
3291
describe('Plotly.redraw promise', function() {
3392
var promise,
3493
promiseGd;
@@ -272,17 +331,42 @@ describe('Plotly.___ methods', function() {
272331
});
273332
});
274333

334+
describe('Plotly.restyle promise', function() {
335+
var promise,
336+
promiseRejected = false;
337+
338+
beforeEach(function(done) {
339+
var data = [{ x: [1,2,3], y: [4,5,6] }],
340+
initialDiv = createGraphDiv();
341+
342+
Plotly.plot(initialDiv, data, {});
343+
344+
promise = Plotly.restyle(initialDiv, undefined, '');
345+
346+
promise.then(null, function(){
347+
promiseRejected = true;
348+
done();
349+
});
350+
});
351+
afterEach(destroyGraphDiv);
352+
353+
it('should be rejected when the attribute is missing', function() {
354+
expect(promiseRejected).toBe(true);
355+
});
356+
});
357+
275358
describe('Plotly.relayout promise', function() {
276359
var promise,
277360
promiseGd;
278361

279362
beforeEach(function(done) {
280363
var data = [{ x: [1,2,3], y: [4,5,6] }],
364+
layout = {hovermode:'closest'},
281365
initialDiv = createGraphDiv();
282366

283-
Plotly.plot(initialDiv, data, {});
367+
Plotly.plot(initialDiv, data, layout);
284368

285-
promise = Plotly.relayout(initialDiv, 'title', 'Promise test!');
369+
promise = Plotly.relayout(initialDiv, 'hovermode', false);
286370

287371
promise.then(function(gd){
288372
promiseGd = gd;
@@ -299,4 +383,85 @@ describe('Plotly.___ methods', function() {
299383
});
300384
});
301385

386+
describe('Plotly.relayout promise', function() {
387+
var promise,
388+
promiseGd;
389+
390+
beforeEach(function(done) {
391+
var data = [{ x: [1,2,3], y: [4,5,6] }],
392+
layout = {hovermode:'closest'},
393+
initialDiv = createGraphDiv();
394+
395+
Plotly.plot(initialDiv, data, layout);
396+
397+
promise = Plotly.relayout(initialDiv, 'hovermode', false);
398+
399+
promise.then(function(gd){
400+
promiseGd = gd;
401+
done();
402+
});
403+
});
404+
afterEach(destroyGraphDiv);
405+
406+
it('should be returned with the graph div as an argument', function() {
407+
expect(promiseGd).toBeDefined();
408+
expect(typeof promiseGd).toBe('object');
409+
expect(promiseGd.data).toBeDefined();
410+
expect(promiseGd.layout).toBeDefined();
411+
});
412+
});
413+
414+
describe('Plotly.relayout promise', function() {
415+
var promise,
416+
promiseGd;
417+
418+
beforeEach(function(done) {
419+
var data = [{ x: [1,2,3], y: [4,5,6] }],
420+
layout = {hovermode:'closest'},
421+
initialDiv = createGraphDiv();
422+
423+
Plotly.plot(initialDiv, data, layout);
424+
425+
initialDiv.framework = { isPolar: true };
426+
promise = Plotly.relayout(initialDiv, 'hovermode', false);
427+
428+
promise.then(function(gd){
429+
promiseGd = gd;
430+
done();
431+
});
432+
});
433+
afterEach(destroyGraphDiv);
434+
435+
it('should be returned with the graph div unchanged when the framework is polar', function() {
436+
expect(promiseGd).toBeDefined();
437+
expect(typeof promiseGd).toBe('object');
438+
expect(promiseGd.changed).toBeFalsy();
439+
});
440+
});
441+
442+
describe('Plotly.relayout promise', function() {
443+
var promise,
444+
promiseRejected = false;
445+
446+
beforeEach(function(done) {
447+
var data = [{ x: [1,2,3], y: [4,5,6] }],
448+
layout = {hovermode:'closest'},
449+
initialDiv = createGraphDiv();
450+
451+
Plotly.plot(initialDiv, data, layout);
452+
453+
promise = Plotly.relayout(initialDiv, undefined, false);
454+
455+
promise.then(null, function(){
456+
promiseRejected = true;
457+
done();
458+
});
459+
});
460+
afterEach(destroyGraphDiv);
461+
462+
it('should be rejected when the attribute is missing', function() {
463+
expect(promiseRejected).toBe(true);
464+
});
465+
});
466+
302467
});

0 commit comments

Comments
 (0)