Skip to content

Commit cf502f8

Browse files
authored
Merge pull request #1578 from StoDevX/bus-lines-check-time
Check the current minute in BusLine's shouldComponentUpdate call
2 parents a74449f + c095b46 commit cf502f8

File tree

1 file changed

+15
-53
lines changed

1 file changed

+15
-53
lines changed

source/views/transportation/bus/bus-line.js

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {getScheduleForNow, getSetOfStopsForNow} from './lib'
66
import get from 'lodash/get'
77
import zip from 'lodash/zip'
88
import head from 'lodash/head'
9-
import isEqual from 'lodash/isEqual'
109
import last from 'lodash/last'
1110
import moment from 'moment-timezone'
1211
import * as c from '../../components/colors'
@@ -76,57 +75,20 @@ type Props = {
7675
openMap: () => any,
7776
}
7877

79-
type State = {
80-
schedule: ?BusScheduleType,
81-
scheduledMoments: Array<FancyBusTimeListType>,
82-
currentMoments: FancyBusTimeListType,
83-
stopTitleTimePairs: Array<[string, moment]>,
84-
}
85-
86-
export class BusLine extends React.Component<void, Props, State> {
87-
state = {
88-
schedule: null,
89-
scheduledMoments: [],
90-
currentMoments: [],
91-
stopTitleTimePairs: [],
92-
firstUpdate: true,
93-
}
94-
95-
componentWillMount() {
96-
this.setStateFromProps(this.props)
97-
}
98-
99-
componentWillReceiveProps(nextProps: Props) {
100-
this.setStateFromProps(nextProps)
101-
}
102-
103-
shouldComponentUpdate(nextProps: Props, nextState: State) {
78+
export class BusLine extends React.Component<void, Props, void> {
79+
shouldComponentUpdate(nextProps: Props) {
10480
// We won't check the time in shouldComponentUpdate, because we really
10581
// only care if the bus information has changed, and this is called after
10682
// setStateFromProps runs.
10783

10884
return (
85+
this.props.now.isSame(nextProps.now, 'minute') ||
10986
this.props.line !== nextProps.line ||
110-
this.props.openMap !== nextProps.openMap ||
111-
!isEqual(this.state.currentMoments, nextState.currentMoments)
87+
this.props.openMap !== nextProps.openMap
11288
)
11389
}
11490

115-
setStateFromProps = (nextProps: Props) => {
116-
if (
117-
this.props.now.isSame(nextProps.now, 'minute') &&
118-
!this.state.firstUpdate
119-
) {
120-
return
121-
}
122-
123-
const {line, now} = nextProps
124-
125-
const schedule = getScheduleForNow(line.schedules, now)
126-
if (!schedule) {
127-
return
128-
}
129-
91+
generateScheduleInfo = (schedule: BusScheduleType, now: moment) => {
13092
const parseTimes = timeset => timeset.map(parseTime(now))
13193
const scheduledMoments: Array<FancyBusTimeListType> = schedule.times.map(
13294
parseTimes,
@@ -142,23 +104,17 @@ export class BusLine extends React.Component<void, Props, State> {
142104
currentMoments,
143105
)
144106

145-
this.setState(() => ({
146-
schedule,
107+
return {
147108
scheduledMoments,
148109
currentMoments,
149110
stopTitleTimePairs,
150-
firstUpdate: false,
151-
}))
111+
}
152112
}
153113

154114
render() {
155115
const {line, now} = this.props
156-
const {
157-
schedule,
158-
scheduledMoments,
159-
currentMoments,
160-
stopTitleTimePairs,
161-
} = this.state
116+
117+
const schedule = getScheduleForNow(line.schedules, now)
162118

163119
// grab the colors (with fallbacks) via _.get
164120
const barColor = get(barColors, line.line, c.black)
@@ -176,6 +132,12 @@ export class BusLine extends React.Component<void, Props, State> {
176132
)
177133
}
178134

135+
const {
136+
scheduledMoments,
137+
currentMoments,
138+
stopTitleTimePairs,
139+
} = this.generateScheduleInfo(schedule, now)
140+
179141
const timesIndex = scheduledMoments.indexOf(currentMoments)
180142
const isLastBus = timesIndex === scheduledMoments.length - 1
181143
const subtitle = makeSubtitle({now, moments: currentMoments, isLastBus})

0 commit comments

Comments
 (0)