Skip to content

Commit a53f7a2

Browse files
committed
async-friendly extend and prepend to graphs
1 parent c63e649 commit a53f7a2

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

components/dash-core-components/src/fragments/Graph.react.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ class PlotlyGraph extends Component {
218218
const clearState = props.clearState;
219219
const dataArray = props[dataKey];
220220

221+
let p = Promise.resolve();
222+
221223
dataArray.forEach(data => {
222224
let updateData, traceIndices, maxPoints;
223225
if (Array.isArray(data) && typeof data[0] === 'object') {
@@ -237,11 +239,16 @@ class PlotlyGraph extends Component {
237239
traceIndices = generateIndices(updateData);
238240
}
239241

240-
const gd = this.gd.current;
241-
return Plotly[plotlyFnKey](gd, updateData, traceIndices, maxPoints);
242+
p = p.then(() => {
243+
const gd = this.gd.current;
244+
return (
245+
gd &&
246+
Plotly[plotlyFnKey](gd, updateData, traceIndices, maxPoints)
247+
);
248+
});
242249
});
243250

244-
clearState(dataKey);
251+
p.then(() => clearState(dataKey));
245252
}
246253

247254
getConfig(config, responsive) {
@@ -393,16 +400,22 @@ class PlotlyGraph extends Component {
393400
}
394401

395402
componentDidMount() {
396-
this.plot(this.props);
403+
let p = this.plot(this.props);
397404
if (this.props.prependData) {
398-
this.mergeTraces(this.props, 'prependData', 'prependTraces');
405+
p = p.then(() =>
406+
this.mergeTraces(this.props, 'prependData', 'prependTraces')
407+
);
399408
}
400409
if (this.props.extendData) {
401-
this.mergeTraces(this.props, 'extendData', 'extendTraces');
410+
p = p.then(() =>
411+
this.mergeTraces(this.props, 'extendData', 'extendTraces')
412+
);
402413
}
403414

404415
if (this.props.prependData?.length || this.props.extendData?.length) {
405-
this.props._dashprivate_onFigureModified(this.props.figure);
416+
p.then(() =>
417+
this.props._dashprivate_onFigureModified(this.props.figure)
418+
);
406419
}
407420
}
408421

@@ -435,6 +448,8 @@ class PlotlyGraph extends Component {
435448
*/
436449
return;
437450
}
451+
452+
let p = Promise.resolve();
438453
if (
439454
this.props.mathjax !== nextProps.mathjax ||
440455
this.props.figure !== nextProps.figure ||
@@ -443,19 +458,25 @@ class PlotlyGraph extends Component {
443458
this.props._dashprivate_transformFigure !==
444459
nextProps._dashprivate_transformFigure
445460
) {
446-
this.plot(nextProps);
461+
p = this.plot(nextProps);
447462
}
448463

449464
if (this.props.prependData !== nextProps.prependData) {
450-
this.mergeTraces(nextProps, 'prependData', 'prependTraces');
465+
p = p.then(() =>
466+
this.mergeTraces(nextProps, 'prependData', 'prependTraces')
467+
);
451468
}
452469

453470
if (this.props.extendData !== nextProps.extendData) {
454-
this.mergeTraces(nextProps, 'extendData', 'extendTraces');
471+
p = p.then(() =>
472+
this.mergeTraces(nextProps, 'extendData', 'extendTraces')
473+
);
455474
}
456475

457476
if (this.props.prependData?.length || this.props.extendData?.length) {
458-
this.props._dashprivate_onFigureModified(this.props.figure);
477+
p.then(() =>
478+
this.props._dashprivate_onFigureModified(this.props.figure)
479+
);
459480
}
460481
}
461482

0 commit comments

Comments
 (0)