Skip to content

Commit 6bbca94

Browse files
authored
Reduce re-renderings (#467)
1 parent 11cac4d commit 6bbca94

File tree

8 files changed

+62
-114
lines changed

8 files changed

+62
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { withStyles } from '@material-ui/styles'
1+
import { makeStyles } from '@material-ui/styles'
22
import classnames from 'classnames'
3+
import { observer } from 'mobx-react'
34
import PropTypes from 'prop-types'
45
import React from 'react'
56

6-
const styles = (/* theme */) => ({
7+
const useStyles = makeStyles((/* theme */) => ({
78
block: {
89
position: 'absolute',
910
minHeight: '100%',
@@ -20,15 +21,19 @@ const styles = (/* theme */) => ({
2021
borderRight: `2px solid #333`,
2122
// borderRight: `2px solid ${theme.palette.divider}`,
2223
},
23-
})
24+
}))
2425

25-
function Block({ classes, offset, children, width, leftBorder, rightBorder }) {
26+
function Block({ block, model, children }) {
27+
const classes = useStyles()
2628
return (
2729
<div
28-
style={{ left: `${offset}px`, width: `${width}px` }}
30+
style={{
31+
left: `${block.offsetPx - model.offsetPx}px`,
32+
width: `${block.widthPx}px`,
33+
}}
2934
className={classnames(classes.block, {
30-
[classes.leftBorder]: leftBorder,
31-
[classes.rightBorder]: rightBorder,
35+
[classes.leftBorder]: block.isLeftEndOfDisplayedRegion,
36+
[classes.rightBorder]: block.isRightEndOfDisplayedRegion,
3237
})}
3338
>
3439
{children}
@@ -38,19 +43,14 @@ function Block({ classes, offset, children, width, leftBorder, rightBorder }) {
3843

3944
Block.defaultProps = {
4045
children: undefined,
41-
leftBorder: false,
42-
rightBorder: false,
4346
}
4447
Block.propTypes = {
45-
classes: PropTypes.objectOf(PropTypes.string).isRequired,
46-
offset: PropTypes.number.isRequired,
47-
width: PropTypes.number.isRequired,
48+
model: PropTypes.shape().isRequired,
49+
block: PropTypes.shape().isRequired,
4850
children: PropTypes.oneOfType([
4951
PropTypes.node,
5052
PropTypes.arrayOf(PropTypes.node),
5153
]),
52-
leftBorder: PropTypes.bool,
53-
rightBorder: PropTypes.bool,
5454
}
5555

56-
export default withStyles(styles)(Block)
56+
export default observer(Block)

packages/linear-genome-view/src/BasicTrack/components/BlockBasedTrack.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getConf } from '@gmod/jbrowse-core/configuration'
22
import { observer, PropTypes as MobxPropTypes } from 'mobx-react'
3+
import { getParent } from 'mobx-state-tree'
34
import PropTypes from 'prop-types'
45
import React from 'react'
56
import Track from './Track'
@@ -12,7 +13,11 @@ function BlockBasedTrack(props) {
1213
{model.trackMessageComponent ? (
1314
<model.trackMessageComponent model={model} />
1415
) : (
15-
<TrackBlocks {...props} blockState={model.blockState} />
16+
<TrackBlocks
17+
{...props}
18+
viewModel={getParent(getParent(model))}
19+
blockState={model.blockState}
20+
/>
1621
)}
1722
{children}
1823
</Track>

packages/linear-genome-view/src/BasicTrack/components/TrackBlocks.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,15 @@ const ElidedBlockMarker = withStyles(styles)(function ElidedBlockMarker({
4949
)
5050
})
5151

52-
function TrackBlocks({ classes, model, offsetPx, bpPerPx, blockState }) {
52+
function TrackBlocks({ classes, model, viewModel, blockState }) {
5353
const { blockDefinitions } = model
5454
return (
5555
<div data-testid="Block" className={classes.trackBlocks}>
5656
{blockDefinitions.map(block => {
5757
if (block instanceof ContentBlock) {
5858
const state = blockState.get(block.key)
5959
return (
60-
<Block
61-
leftBorder={block.isLeftEndOfDisplayedRegion}
62-
rightBorder={block.isRightEndOfDisplayedRegion}
63-
start={block.start}
64-
end={block.end}
65-
refName={block.refName}
66-
width={block.widthPx}
67-
key={block.key}
68-
offset={block.offsetPx - offsetPx}
69-
bpPerPx={bpPerPx}
70-
>
60+
<Block key={block.offsetPx} block={block} model={viewModel}>
7161
{state && state.reactComponent ? (
7262
<state.reactComponent model={state} />
7363
) : null}
@@ -90,7 +80,7 @@ function TrackBlocks({ classes, model, offsetPx, bpPerPx, blockState }) {
9080
<ElidedBlockMarker
9181
key={block.key}
9282
width={block.widthPx}
93-
offset={block.offsetPx - offsetPx}
83+
offset={block.offsetPx - viewModel.offsetPx}
9484
/>
9585
)
9686
}
@@ -102,10 +92,9 @@ function TrackBlocks({ classes, model, offsetPx, bpPerPx, blockState }) {
10292

10393
TrackBlocks.propTypes = {
10494
classes: ReactPropTypes.objectOf(ReactPropTypes.string).isRequired,
105-
offsetPx: ReactPropTypes.number.isRequired,
106-
bpPerPx: ReactPropTypes.number.isRequired,
10795
blockState: PropTypes.observableMap.isRequired,
10896
model: PropTypes.observableObject.isRequired,
97+
viewModel: PropTypes.observableObject.isRequired,
10998
}
11099

111100
export default withStyles(styles)(observer(TrackBlocks))

packages/linear-genome-view/src/LinearGenomeView/components/LinearGenomeView.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,7 @@ function LinearGenomeView(props) {
408408
bpPerPx={bpPerPx}
409409
model={model}
410410
>
411-
<ScaleBar
412-
style={{
413-
gridColumn: 'blocks',
414-
gridRow: 'scale-bar',
415-
}}
416-
height={32}
417-
bpPerPx={bpPerPx}
418-
blocks={staticBlocks}
419-
offsetPx={offsetPx}
420-
horizontallyFlipped={model.horizontallyFlipped}
421-
/>
411+
<ScaleBar model={model} height={32} />
422412
</Rubberband>
423413

424414
{model.hideHeader ? (

packages/linear-genome-view/src/LinearGenomeView/components/Ruler.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { withStyles } from '@material-ui/styles'
1+
import { makeStyles } from '@material-ui/styles'
2+
import { observer } from 'mobx-react'
23
import React, { Fragment } from 'react'
34
import ReactPropTypes from 'prop-types'
45
import { PropTypes } from '@gmod/jbrowse-core/mst-types'
@@ -77,7 +78,7 @@ export function* makeTicks(
7778
}
7879
}
7980

80-
const styles = (/* theme */) => ({
81+
const useStyles = makeStyles((/* theme */) => ({
8182
majorTickLabel: {
8283
fontSize: '11px',
8384
// fill: theme.palette.text.primary,
@@ -103,18 +104,11 @@ const styles = (/* theme */) => ({
103104
fillOpacity: 0.75,
104105
filter: 'url(#dilate)',
105106
},
106-
})
107+
}))
107108

108109
function Ruler(props) {
109-
const {
110-
region,
111-
bpPerPx,
112-
flipped,
113-
major,
114-
minor,
115-
showRefNameLabel,
116-
classes,
117-
} = props
110+
const { region, bpPerPx, flipped, major, minor, showRefNameLabel } = props
111+
const classes = useStyles()
118112
const ticks = []
119113
const labels = []
120114
for (const tick of makeTicks(region, bpPerPx, major, minor)) {
@@ -189,7 +183,6 @@ function Ruler(props) {
189183
}
190184

191185
Ruler.propTypes = {
192-
classes: ReactPropTypes.objectOf(ReactPropTypes.string).isRequired,
193186
region: PropTypes.Region.isRequired,
194187
bpPerPx: ReactPropTypes.number.isRequired,
195188
flipped: ReactPropTypes.bool,
@@ -203,4 +196,4 @@ Ruler.defaultProps = {
203196
minor: true,
204197
}
205198

206-
export default withStyles(styles)(Ruler)
199+
export default observer(Ruler)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { withStyles } from '@material-ui/core'
22
import React from 'react'
3+
import { observer, PropTypes as MobxPropTypes } from 'mobx-react'
34
import PropTypes from 'prop-types'
45
import Block from '../../BasicTrack/components/Block'
56

@@ -14,7 +15,7 @@ const styles = (/* theme */) => ({
1415
background: '#555',
1516
// background: theme.palette.background.default,
1617
overflow: 'hidden',
17-
height: '100%',
18+
height: 32,
1819
},
1920
refLabel: {
2021
fontSize: '16px',
@@ -37,48 +38,26 @@ function findBlockContainingLeftSideOfView(offsetPx, blockSet) {
3738
return undefined
3839
}
3940

40-
function ScaleBar({
41-
classes,
42-
style,
43-
height,
44-
blocks,
45-
offsetPx,
46-
bpPerPx,
47-
horizontallyFlipped,
48-
}) {
49-
const finalStyle = Object.assign({}, style, {
50-
height: `${height}px`,
51-
})
52-
41+
function ScaleBar({ classes, model, height }) {
5342
const blockContainingLeftEndOfView = findBlockContainingLeftSideOfView(
54-
offsetPx,
55-
blocks,
43+
model.offsetPx,
44+
model.staticBlocks,
5645
)
5746

5847
return (
59-
<div style={finalStyle} className={classes.scaleBar}>
60-
{blocks.map(block => {
48+
<div className={classes.scaleBar}>
49+
{model.staticBlocks.map(block => {
6150
return (
62-
<Block
63-
leftBorder={block.isLeftEndOfDisplayedRegion}
64-
rightBorder={block.isRightEndOfDisplayedRegion}
65-
refName={block.refName}
66-
start={block.start}
67-
end={block.end}
68-
width={block.widthPx}
69-
key={block.key}
70-
offset={block.offsetPx - offsetPx}
71-
bpPerPx={bpPerPx}
72-
>
51+
<Block key={block.offsetPx} block={block} model={model}>
7352
<svg height={height} width={block.widthPx}>
7453
<Ruler
7554
region={block}
7655
showRefNameLabel={
7756
!!block.isLeftEndOfDisplayedRegion &&
7857
block !== blockContainingLeftEndOfView
7958
}
80-
bpPerPx={bpPerPx}
81-
flipped={horizontallyFlipped}
59+
bpPerPx={model.bpPerPx}
60+
flipped={model.horizontallyFlipped}
8261
/>
8362
</svg>
8463
</Block>
@@ -95,20 +74,11 @@ function ScaleBar({
9574
}
9675
ScaleBar.defaultProps = {
9776
style: {},
98-
blocks: [],
99-
horizontallyFlipped: false,
10077
}
10178
ScaleBar.propTypes = {
79+
model: MobxPropTypes.objectOrObservableObject.isRequired,
10280
classes: PropTypes.objectOf(PropTypes.string).isRequired,
103-
style: PropTypes.objectOf(PropTypes.any),
10481
height: PropTypes.number.isRequired,
105-
blocks: PropTypes.shape({
106-
map: PropTypes.func.isRequired,
107-
getBlocks: PropTypes.func.isRequired,
108-
}),
109-
bpPerPx: PropTypes.number.isRequired,
110-
offsetPx: PropTypes.number.isRequired,
111-
horizontallyFlipped: PropTypes.bool,
11282
}
11383

114-
export default withStyles(styles)(ScaleBar)
84+
export default withStyles(styles)(observer(ScaleBar))

0 commit comments

Comments
 (0)