@@ -73,33 +73,10 @@ type Props = {
7373 line : BusLineType ,
7474 now : moment ,
7575 openMap : ( ) => any ,
76- }
77-
78- type State = {
79- schedule : ?BusScheduleType ,
80- scheduledMoments : Array < FancyBusTimeListType > ,
81- currentMoments : FancyBusTimeListType ,
82- stopTitleTimePairs : Array < [ string , moment ] > ,
83- }
84-
85- export class BusLine extends React . Component < void , Props , State > {
86- state = {
87- schedule : null ,
88- scheduledMoments : [ ] ,
89- currentMoments : [ ] ,
90- stopTitleTimePairs : [ ] ,
91- firstUpdate : true ,
92- }
93-
94- componentWillMount ( ) {
95- this . setStateFromProps ( this . props )
96- }
76+ } ;
9777
98- componentWillReceiveProps ( nextProps : Props ) {
99- this . setStateFromProps ( nextProps )
100- }
101-
102- shouldComponentUpdate ( nextProps : Props , nextState : State ) {
78+ export class BusLine extends React . Component < void , Props , void > {
79+ shouldComponentUpdate ( nextProps : Props ) {
10380 // We won't check the time in shouldComponentUpdate, because we really
10481 // only care if the bus information has changed, and this is called after
10582 // setStateFromProps runs.
@@ -111,21 +88,7 @@ export class BusLine extends React.Component<void, Props, State> {
11188 )
11289 }
11390
114- setStateFromProps = ( nextProps : Props ) => {
115- if (
116- this . props . now . isSame ( nextProps . now , 'minute' ) &&
117- ! this . state . firstUpdate
118- ) {
119- return
120- }
121-
122- const { line, now} = nextProps
123-
124- const schedule = getScheduleForNow ( line . schedules , now )
125- if ( ! schedule ) {
126- return
127- }
128-
91+ generateScheduleInfo = ( schedules : Array < BusScheduleType > , now: moment) => {
12992 const parseTimes = timeset => timeset . map ( parseTime ( now ) )
13093 const scheduledMoments : Array < FancyBusTimeListType > = schedule . times . map (
13194 parseTimes ,
@@ -141,23 +104,17 @@ export class BusLine extends React.Component<void, Props, State> {
141104 currentMoments ,
142105 )
143106
144- this . setState ( ( ) => ( {
145- schedule,
107+ return {
146108 scheduledMoments ,
147109 currentMoments ,
148110 stopTitleTimePairs ,
149- firstUpdate : false ,
150- } ) )
111+ }
151112 }
152113
153114 render() {
154115 const { line , now } = this.props
155- const {
156- schedule ,
157- scheduledMoments ,
158- currentMoments ,
159- stopTitleTimePairs ,
160- } = this . state
116+
117+ const schedule = getScheduleForNow(line.schedules, now)
161118
162119 // grab the colors (with fallbacks) via _.get
163120 const barColor = get(barColors, line.line, c.black)
@@ -175,6 +132,12 @@ export class BusLine extends React.Component<void, Props, State> {
175132 )
176133 }
177134
135+ const {
136+ scheduledMoments ,
137+ currentMoments ,
138+ stopTitleTimePairs ,
139+ } = this.generateScheduleInfo(schedule, now)
140+
178141 const timesIndex = scheduledMoments.indexOf(currentMoments)
179142 const isLastBus = timesIndex === scheduledMoments.length - 1
180143 const subtitle = makeSubtitle({ now , moments : currentMoments , isLastBus } )
0 commit comments