Skip to content

Commit 7c100f5

Browse files
committed
Merge pull request #77 from plotly/patch-promise-returns
Added promise returns to Plotly.___
2 parents 085cc15 + 26a6102 commit 7c100f5

File tree

3 files changed

+437
-139
lines changed

3 files changed

+437
-139
lines changed

src/plot_api/plot_api.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ Plotly.plot = function(gd, data, layout, config) {
356356
// even if everything we did was synchronous, return a promise
357357
// so that the caller doesn't care which route we took
358358
return (donePlotting && donePlotting.then) ?
359-
donePlotting : Promise.resolve();
359+
donePlotting : Promise.resolve(gd);
360360
};
361361

362362
// Get the container div: we store all variables for this plot as
@@ -926,6 +926,7 @@ Plotly.redraw = function(gd) {
926926
gd.calcdata = undefined;
927927
return Plotly.plot(gd).then(function () {
928928
gd.emit('plotly_redraw');
929+
return gd;
929930
});
930931
};
931932

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

1364-
Plotly.redraw(gd);
1365+
var promise = Plotly.redraw(gd);
13651366

13661367
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
13671368
if (Plotly.Queue) {
13681369
Plotly.Queue.add(gd, Plotly.prependTraces, undoArgs, extendTraces, arguments);
13691370
}
1371+
1372+
return promise;
13701373
};
13711374

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

1391-
Plotly.redraw(gd);
1394+
var promise = Plotly.redraw(gd);
13921395

13931396
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
13941397
if (Plotly.Queue) {
13951398
Plotly.Queue.add(gd, Plotly.extendTraces, undoArgs, prependTraces, arguments);
13961399
}
1400+
1401+
return promise;
13971402
};
13981403

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

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

14691475
/**
@@ -1502,9 +1508,11 @@ Plotly.deleteTraces = function deleteTraces (gd, indices) {
15021508
traces.push(deletedTrace);
15031509
}
15041510

1505-
Plotly.redraw(gd);
1511+
var promise = Plotly.redraw(gd);
15061512

15071513
if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
1514+
1515+
return promise;
15081516
};
15091517

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

16001608
gd.data = newData;
16011609

1602-
Plotly.redraw(gd);
1610+
var promise = Plotly.redraw(gd);
16031611

16041612
if (Plotly.Queue) Plotly.Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
1613+
1614+
return promise;
16051615
};
16061616

16071617
// -----------------------------------------------------
@@ -1640,7 +1650,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
16401650
}
16411651
else {
16421652
console.log('restyle fail',astr,val,traces);
1643-
return;
1653+
return new Promise.reject();
16441654
}
16451655

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

21172127
return plotDone.then(function() {
2118-
gd.emit('plotly_restyle',
2119-
Plotly.Lib.extendDeep([], [redoit, traces]));
2128+
gd.emit('plotly_restyle', Plotly.Lib.extendDeep([], [redoit, traces]));
2129+
return gd;
21202130
});
21212131
};
21222132

@@ -2161,7 +2171,9 @@ function swapXYData(trace) {
21612171
Plotly.relayout = function relayout(gd, astr, val) {
21622172
gd = getGraphDiv(gd);
21632173

2164-
if(gd.framework && gd.framework.isPolar) return;
2174+
if(gd.framework && gd.framework.isPolar) {
2175+
return new Promise.resolve(gd);
2176+
}
21652177

21662178
var layout = gd.layout,
21672179
fullLayout = gd._fullLayout,
@@ -2178,7 +2190,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
21782190
else if(Plotly.Lib.isPlainObject(astr)) aobj = astr;
21792191
else {
21802192
console.log('relayout fail',astr,val);
2181-
return;
2193+
return new Promise.reject();
21822194
}
21832195

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

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

2487-
if(!plotDone || !plotDone.then) plotDone = Promise.resolve();
2499+
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);
24882500

24892501
return plotDone.then(function() {
24902502
gd.emit('plotly_relayout',
24912503
Plotly.Lib.extendDeep({}, redoit));
2504+
return gd;
24922505
});
24932506
};
24942507

0 commit comments

Comments
 (0)