Skip to content

Added promise returns to Plotly.plot #226

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 5 commits into from Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Plotly.plot = function(gd, data, layout, config) {
Events.init(gd);

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

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

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

if(gd.framework && gd.framework.isPolar) {
return new Promise.resolve(gd);
return Promise.resolve(gd);
}

var layout = gd.layout,
Expand All @@ -2122,7 +2122,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
else if(Lib.isPlainObject(astr)) aobj = astr;
else {
console.log('relayout fail',astr,val);
return new Promise.reject();
return Promise.reject();
}

if(Object.keys(aobj).length) gd.changed = true;
Expand Down
169 changes: 167 additions & 2 deletions test/jasmine/tests/plot_promise_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Plotly = require('@src/plotly');
var Events = require('@src/lib/events');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');

Expand Down Expand Up @@ -29,6 +30,64 @@ describe('Plotly.___ methods', function() {
});
});

describe('Plotly.plot promise', function() {
var gd,
promise,
promiseRejected = false;

beforeEach(function(done) {
var data = [{ x: [1,2,3], y: [4,5,6] }];

gd = createGraphDiv();

Events.init(gd);

gd.on('plotly_beforeplot', function() {
return false;
});

promise = Plotly.plot(gd, data, {});

promise.then(null, function(){
promiseRejected = true;
done();
});
});

afterEach(destroyGraphDiv);

it('should be rejected when plotly_beforeplot event handlers return false', function() {
expect(promiseRejected).toBe(true);
});
});

describe('Plotly.plot promise', function() {
var gd,
promise,
promiseRejected = false;

beforeEach(function(done) {
var data = [{ x: [1,2,3], y: [4,5,6] }];

gd = createGraphDiv();

gd._dragging = true;

promise = Plotly.plot(gd, data, {});

promise.then(null, function(){
promiseRejected = true;
done();
});
});

afterEach(destroyGraphDiv);

it('should reject the promise when graph is being dragged', function() {
expect(promiseRejected).toBe(true);
});
});

describe('Plotly.redraw promise', function() {
var promise,
promiseGd;
Expand Down Expand Up @@ -272,17 +331,42 @@ describe('Plotly.___ methods', function() {
});
});

describe('Plotly.restyle promise', function() {
var promise,
promiseRejected = false;

beforeEach(function(done) {
var data = [{ x: [1,2,3], y: [4,5,6] }],
initialDiv = createGraphDiv();

Plotly.plot(initialDiv, data, {});

promise = Plotly.restyle(initialDiv, undefined, '');

promise.then(null, function(){
promiseRejected = true;
done();
});
});
afterEach(destroyGraphDiv);

it('should be rejected when the attribute is missing', function() {
expect(promiseRejected).toBe(true);
});
});

describe('Plotly.relayout promise', function() {
var promise,
promiseGd;

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

Plotly.plot(initialDiv, data, {});
Plotly.plot(initialDiv, data, layout);

promise = Plotly.relayout(initialDiv, 'title', 'Promise test!');
promise = Plotly.relayout(initialDiv, 'hovermode', false);

promise.then(function(gd){
promiseGd = gd;
Expand All @@ -299,4 +383,85 @@ describe('Plotly.___ methods', function() {
});
});

describe('Plotly.relayout promise', function() {
var promise,
promiseGd;

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

Plotly.plot(initialDiv, data, layout);

promise = Plotly.relayout(initialDiv, 'hovermode', false);

promise.then(function(gd){
promiseGd = gd;
done();
});
});
afterEach(destroyGraphDiv);

it('should be returned with the graph div as an argument', function() {
expect(promiseGd).toBeDefined();
expect(typeof promiseGd).toBe('object');
expect(promiseGd.data).toBeDefined();
expect(promiseGd.layout).toBeDefined();
});
});

describe('Plotly.relayout promise', function() {
var promise,
promiseGd;

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

Plotly.plot(initialDiv, data, layout);

initialDiv.framework = { isPolar: true };
promise = Plotly.relayout(initialDiv, 'hovermode', false);

promise.then(function(gd){
promiseGd = gd;
done();
});
});
afterEach(destroyGraphDiv);

it('should be returned with the graph div unchanged when the framework is polar', function() {
expect(promiseGd).toBeDefined();
expect(typeof promiseGd).toBe('object');
expect(promiseGd.changed).toBeFalsy();
});
});

describe('Plotly.relayout promise', function() {
var promise,
promiseRejected = false;

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

Plotly.plot(initialDiv, data, layout);

promise = Plotly.relayout(initialDiv, undefined, false);

promise.then(null, function(){
promiseRejected = true;
done();
});
});
afterEach(destroyGraphDiv);

it('should be rejected when the attribute is missing', function() {
expect(promiseRejected).toBe(true);
});
});

});