Skip to content

Added promise returns to Plotly.___ #77

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 2 commits into from
Dec 7, 2015
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
41 changes: 27 additions & 14 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ Plotly.plot = function(gd, data, layout, config) {
// even if everything we did was synchronous, return a promise
// so that the caller doesn't care which route we took
return (donePlotting && donePlotting.then) ?
donePlotting : Promise.resolve();
donePlotting : Promise.resolve(gd);
};

// Get the container div: we store all variables for this plot as
Expand Down Expand Up @@ -926,6 +926,7 @@ Plotly.redraw = function(gd) {
gd.calcdata = undefined;
return Plotly.plot(gd).then(function () {
gd.emit('plotly_redraw');
return gd;
});
};

Expand Down Expand Up @@ -1361,12 +1362,14 @@ Plotly.extendTraces = function extendTraces (gd, update, indices, maxPoints) {
return target.splice(0, target.length - maxPoints);
});

Plotly.redraw(gd);
var promise = Plotly.redraw(gd);

var undoArgs = [gd, undo.update, indices, undo.maxPoints];
if (Plotly.Queue) {
Plotly.Queue.add(gd, Plotly.prependTraces, undoArgs, extendTraces, arguments);
}

return promise;
};

Plotly.prependTraces = function prependTraces (gd, update, indices, maxPoints) {
Expand All @@ -1388,12 +1391,14 @@ Plotly.prependTraces = function prependTraces (gd, update, indices, maxPoints)
return target.splice(maxPoints, target.length);
});

Plotly.redraw(gd);
var promise = Plotly.redraw(gd);

var undoArgs = [gd, undo.update, indices, undo.maxPoints];
if (Plotly.Queue) {
Plotly.Queue.add(gd, Plotly.extendTraces, undoArgs, prependTraces, arguments);
}

return promise;
};

/**
Expand Down Expand Up @@ -1436,9 +1441,9 @@ Plotly.addTraces = function addTraces (gd, traces, newIndices) {
// if the user didn't define newIndices, they just want the traces appended
// i.e., we can simply redraw and be done
if (typeof newIndices === 'undefined') {
Plotly.redraw(gd);
var promise = Plotly.redraw(gd);
if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return;
return promise;
}

// make sure indices is property defined
Expand All @@ -1462,8 +1467,9 @@ Plotly.addTraces = function addTraces (gd, traces, newIndices) {
// this requires some extra work that moveTraces will do
if (Plotly.Queue) Plotly.Queue.startSequence(gd);
if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
Plotly.moveTraces(gd, currentIndices, newIndices);
var promise = Plotly.moveTraces(gd, currentIndices, newIndices);
if (Plotly.Queue) Plotly.Queue.stopSequence(gd);
return promise;
};

/**
Expand Down Expand Up @@ -1502,9 +1508,11 @@ Plotly.deleteTraces = function deleteTraces (gd, indices) {
traces.push(deletedTrace);
}

Plotly.redraw(gd);
var promise = Plotly.redraw(gd);

if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);

return promise;
};

/**
Expand Down Expand Up @@ -1599,9 +1607,11 @@ Plotly.moveTraces = function moveTraces (gd, currentIndices, newIndices) {

gd.data = newData;

Plotly.redraw(gd);
var promise = Plotly.redraw(gd);

if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);

return promise;
};

// -----------------------------------------------------
Expand Down Expand Up @@ -1640,7 +1650,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
}
else {
console.log('restyle fail',astr,val,traces);
return;
return new Promise.reject();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

}

if(Object.keys(aobj).length) gd.changed = true;
Expand Down Expand Up @@ -2115,8 +2125,8 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
if(!plotDone || !plotDone.then) plotDone = Promise.resolve();

return plotDone.then(function() {
gd.emit('plotly_restyle',
Plotly.Lib.extendDeep([], [redoit, traces]));
gd.emit('plotly_restyle', Plotly.Lib.extendDeep([], [redoit, traces]));
return gd;
});
};

Expand Down Expand Up @@ -2161,7 +2171,9 @@ function swapXYData(trace) {
Plotly.relayout = function relayout(gd, astr, val) {
gd = getGraphDiv(gd);

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

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

if(Object.keys(aobj).length) gd.changed = true;
Expand Down Expand Up @@ -2484,11 +2496,12 @@ Plotly.relayout = function relayout(gd, astr, val) {

var plotDone = Plotly.Lib.syncOrAsync(seq, gd);

if(!plotDone || !plotDone.then) plotDone = Promise.resolve();
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);

return plotDone.then(function() {
gd.emit('plotly_relayout',
Plotly.Lib.extendDeep({}, redoit));
return gd;
});
};

Expand Down
Loading