Skip to content
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

Refactor SpanGraph.UNSAFE_componentWillReceiveProps #613

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
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ describe('<SpanGraph>', () => {
}));
expect(canvasGraph.prop('items')).toEqual(items);
});

it('does not regenerate CanvasSpanGraph without new trace', () => {
const canvasGraph = wrapper.find(CanvasSpanGraph).first();
const items = canvasGraph.prop('items');

wrapper.instance().forceUpdate();

const newCanvasGraph = wrapper.find(CanvasSpanGraph).first();
const newItems = newCanvasGraph.prop('items');

expect(newItems).toBe(items);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import * as React from 'react';
import memoizeOne from 'memoize-one';

import CanvasSpanGraph from './CanvasSpanGraph';
import TickLabels from './TickLabels';
Expand All @@ -31,57 +32,38 @@ type SpanGraphProps = {
updateNextViewRangeTime: (nextUpdate: ViewRangeTimeUpdate) => void;
};

/**
* Store `items` in state so they are not regenerated every render. Otherwise,
* the canvas graph will re-render itself every time.
*/
type SpanGraphState = {
items: {
valueOffset: number;
valueWidth: number;
serviceName: string;
}[];
type SpanItem = {
valueOffset: number;
valueWidth: number;
serviceName: string;
};

function getItem(span: Span) {
function getItem(span: Span): SpanItem {
return {
valueOffset: span.relativeStartTime,
valueWidth: span.duration,
serviceName: span.process.serviceName,
};
}

export default class SpanGraph extends React.PureComponent<SpanGraphProps, SpanGraphState> {
state: SpanGraphState;
function getItems(trace: Trace): SpanItem[] {
return trace.spans.map(getItem);
}

const memoizedGetItems = memoizeOne(getItems);

export default class SpanGraph extends React.PureComponent<SpanGraphProps> {
Copy link
Member

@yurishkuro yurishkuro Aug 17, 2020

Choose a reason for hiding this comment

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

React.PureComponent<SpanGraphProps>

IsSpanGraphProps still relevant? Seems like it was only affecting constructor/callback.

Copy link
Member

Choose a reason for hiding this comment

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

ah, I guess it affects this.props in render()

static defaultProps = {
height: DEFAULT_HEIGHT,
};

constructor(props: SpanGraphProps) {
super(props);
const { trace } = props;
this.state = {
items: trace ? trace.spans.map(getItem) : [],
};
}

// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps: SpanGraphProps) {
const { trace } = nextProps;
if (this.props.trace !== trace) {
this.setState({
items: trace ? trace.spans.map(getItem) : [],
});
}
}

render() {
const { height, trace, viewRange, updateNextViewRangeTime, updateViewRangeTime } = this.props;
if (!trace) {
return <div />;
}
const { items } = this.state;

const items = memoizedGetItems(trace);
return (
<div className="ub-pb2 ub-px2">
<TickLabels numTicks={TIMELINE_TICK_INTERVAL} duration={trace.duration} />
Expand Down