From e6977be4f44a2108c2432aff6d85e806b17a0d20 Mon Sep 17 00:00:00 2001 From: Jared Cleghorn Date: Mon, 14 Jan 2019 12:10:14 -0600 Subject: [PATCH] Fix conditions for plot being refreshed Previously, chaning the revision prop alone was not sufficient to cause the plot to be refreshed, contrary to the behavior indicated in the REAMDE. Fix this so that the revision prop can be used to force the plot to be refreshed. Resolves: #59 --- src/factory.js | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/factory.js b/src/factory.js index c207fcb..c82c627 100644 --- a/src/factory.js +++ b/src/factory.js @@ -84,30 +84,34 @@ export default function plotComponentFactory(Plotly) { } componentWillUpdate(nextProps) { - if ( - nextProps.revision !== void 0 && - nextProps.revision === this.props.revision - ) { - // if revision is set and unchanged, do nothing - return; + var doNothing = false; + + // If revision is set and unchanged, do nothing. + if ((nextProps.revision !== void 0) + && (nextProps.revision === this.props.revision)) { + doNothing = true; + } + + const numPrevFrames = (this.props.frames && this.props.frames.length) + ? this.props.frames.length + : 0; + const numNextFrames = (nextProps.frames && nextProps.frames.length) + ? nextProps.frames.length + : 0; + + /* If none of data, layout, or config have changed identity and the + number of elements in frames has not changed, do nothing. This + prevents infinite loops when the component is re-rendered after + onUpdate. frames *always* changes identity, so check its length + insead. */ + if ((nextProps.layout === this.props.layout) + && (nextProps.data === this.props.data) + && (nextProps.config === this.props.config) + && (numNextFrames === numPrevFrames)) { + doNothing = true; } - const numPrevFrames = - this.props.frames && this.props.frames.length - ? this.props.frames.length - : 0; - const numNextFrames = - nextProps.frames && nextProps.frames.length - ? nextProps.frames.length - : 0; - if ( - nextProps.layout === this.props.layout && - nextProps.data === this.props.data && - nextProps.config === this.props.config && - numNextFrames === numPrevFrames - ) { - // prevent infinite loops when component is re-rendered after onUpdate - // frames *always* changes identity so fall back to check length only :( + if (doNothing) { return; }