@@ -95,10 +95,30 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) {
9595 const self = this ;
9696 initialWindowSize = initialWindowSize || 20 ;
9797
98- // given an EventTimeline, and an event index within it, initialise our
98+ // given an EventTimeline, find the event we were looking for, and initialise our
9999 // fields so that the event in question is in the middle of the window.
100- const initFields = function ( timeline , eventIndex ) {
101- const endIndex = Math . min ( timeline . getEvents ( ) . length ,
100+ const initFields = function ( timeline ) {
101+ let eventIndex ;
102+
103+ const events = timeline . getEvents ( ) ;
104+
105+ if ( ! initialEventId ) {
106+ // we were looking for the live timeline: initialise to the end
107+ eventIndex = events . length ;
108+ } else {
109+ for ( let i = 0 ; i < events . length ; i ++ ) {
110+ if ( events [ i ] . getId ( ) == initialEventId ) {
111+ eventIndex = i ;
112+ break ;
113+ }
114+ }
115+
116+ if ( eventIndex === undefined ) {
117+ throw new Error ( "getEventTimeline result didn't include requested event" ) ;
118+ }
119+ }
120+
121+ const endIndex = Math . min ( events . length ,
102122 eventIndex + Math . ceil ( initialWindowSize / 2 ) ) ;
103123 const startIndex = Math . max ( 0 , endIndex - initialWindowSize ) ;
104124 self . _start = new TimelineIndex ( timeline , startIndex - timeline . getBaseIndex ( ) ) ;
@@ -110,24 +130,19 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) {
110130 // we already have the data we need, which is important to keep room-switching
111131 // feeling snappy.
112132 //
113- // TODO: ideally we'd spot getEventTimeline returning a resolved promise and
114- // skip straight to the find-event loop.
115133 if ( initialEventId ) {
116- return this . _client . getEventTimeline ( this . _timelineSet , initialEventId )
117- . then ( function ( tl ) {
118- // make sure that our window includes the event
119- for ( let i = 0 ; i < tl . getEvents ( ) . length ; i ++ ) {
120- if ( tl . getEvents ( ) [ i ] . getId ( ) == initialEventId ) {
121- initFields ( tl , i ) ;
122- return ;
123- }
124- }
125- throw new Error ( "getEventTimeline result didn't include requested event" ) ;
126- } ) ;
134+ const prom = this . _client . getEventTimeline ( this . _timelineSet , initialEventId ) ;
135+
136+ const promState = prom . inspect ( ) ;
137+ if ( promState . state == 'fulfilled' ) {
138+ initFields ( promState . value ) ;
139+ return q ( ) ;
140+ } else {
141+ return prom . then ( initFields ) ;
142+ }
127143 } else {
128- // start with the most recent events
129144 const tl = this . _timelineSet . getLiveTimeline ( ) ;
130- initFields ( tl , tl . getEvents ( ) . length ) ;
145+ initFields ( tl ) ;
131146 return q ( ) ;
132147 }
133148} ;
0 commit comments