@@ -165,6 +165,7 @@ export enum RoomEvent {
165165 LocalEchoUpdated = "Room.localEchoUpdated" ,
166166 Timeline = "Room.timeline" ,
167167 TimelineReset = "Room.timelineReset" ,
168+ TimelineRefresh = "RoomEvent.TimelineRefresh" ,
168169 historyImportedWithinTimeline = "Room.historyImportedWithinTimeline" ,
169170}
170171
@@ -174,6 +175,7 @@ type EmittedEvents = RoomEvent
174175 | ThreadEvent . NewReply
175176 | RoomEvent . Timeline
176177 | RoomEvent . TimelineReset
178+ | RoomEvent . TimelineRefresh
177179 | RoomEvent . historyImportedWithinTimeline
178180 | MatrixEventEvent . BeforeRedaction ;
179181
@@ -195,6 +197,7 @@ export type RoomEventHandlerMap = {
195197 markerEvent : MatrixEvent ,
196198 room : Room ,
197199 ) => void ;
200+ [ RoomEvent . TimelineRefresh ] : ( room : Room , eventTimelineSet : EventTimelineSet ) => void ;
198201 [ ThreadEvent . New ] : ( thread : Thread , toStartOfTimeline : boolean ) => void ;
199202} & ThreadHandlerMap & MatrixEventHandlerMap ;
200203
@@ -926,6 +929,98 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
926929 } ) ;
927930 }
928931
932+ // TODO
933+ public async refreshLiveTimeline ( ) : Promise < void > {
934+ const liveTimelineBefore = this . getLiveTimeline ( ) ;
935+ const forwardPaginationToken = liveTimelineBefore . getPaginationToken ( EventTimeline . FORWARDS ) ;
936+ const eventsBefore = liveTimelineBefore . getEvents ( ) ;
937+ const mostRecentEventInTimeline = eventsBefore [ eventsBefore . length - 1 ] ;
938+
939+ // Empty out all of `this.timelineSets` but still keeps the same
940+ // `timelineSet` references around so the React code updates properly
941+ // and doesn't ignore the room events we emit because it checks that the
942+ // `timelineSet` references are the same. We need the `timelineSet`
943+ // empty so that the `client.getEventTimeline(...)` call later, will
944+ // call `/context` and create a new timeline instead of returning the
945+ // same one.
946+ this . resetLiveTimeline ( null , null ) ;
947+ //this.resetLiveTimeline(forwardPaginationToken, forwardPaginationToken);
948+
949+ // Get a reference to the emptied out `timelineSet`
950+ //
951+ // TODO: Do we want to use `this.getLiveTimeline().getTimelineSet()` instead?
952+ // I think it's the same thing but what's more right?
953+ const timelineSet = this . getUnfilteredTimelineSet ( ) ;
954+
955+ console . log ( `refreshLiveTimeline: after resetLiveTimeline timelineSets=${ this . getTimelineSets ( ) . length } getUnfilteredTimelineSet=` , timelineSet . getTimelines ( ) . length , timelineSet . getTimelines ( ) . map ( ( timeline ) => {
956+ return timeline . getEvents ( ) . length ;
957+ } ) ) ;
958+
959+ // Use `client.getEventTimeline(...)` to construct a new timeline from a
960+ // `/context` response state and events for the most recent event before
961+ // we reset everything. The `timelineSet` needs to be empty in order for
962+ // this function to call `/context`
963+ const newTimeline = await this . client . getEventTimeline ( timelineSet , mostRecentEventInTimeline . getId ( ) ) ;
964+ console . log ( 'refreshLiveTimeline: after getEventTimeline' , timelineSet . getTimelines ( ) . length , timelineSet . getTimelines ( ) . map ( ( timeline ) => {
965+ return timeline . getEvents ( ) . length ;
966+ } ) ) ;
967+ // Set the pagination token back to the live sync token instead of using
968+ // the /context historical token so that it matches the next response from `/sync`
969+ // and we can properly continue the timeline.
970+ newTimeline . setPaginationToken ( forwardPaginationToken , EventTimeline . FORWARDS ) ;
971+
972+ timelineSet . setLiveTimeline ( newTimeline ) ;
973+ //timelineSet.getLiveTimeline().setNeighbouringTimeline(newTimeline, EventTimeline.FORWARDS);
974+ // Fixup `this.oldstate` so that `scrollback` has the pagination tokens available
975+ this . fixUpLegacyTimelineFields ( ) ;
976+
977+ // TODO: Set timelineNeedsRefresh = false
978+
979+ // const liveTimeline = this.getLiveTimeline();
980+ // liveTimeline.setPaginationToken(forwardPaginationToken, EventTimeline.BACKWARDS);
981+ // liveTimeline.setPaginationToken(forwardPaginationToken, EventTimeline.FORWARDS);
982+ // await new Promise((resolve) => {
983+ // setTimeout(async () => {
984+ // await this.client.scrollback(this, 30);
985+ // resolve(null);
986+ // }, 2000);
987+ // });
988+ //await this.client.scrollback(this, 30);
989+ this . emit ( RoomEvent . TimelineRefresh , this , timelineSet ) ;
990+
991+ // const timelineSet = new EventTimelineSet(this, this.opts);
992+
993+ // const liveTimeline = this.getLiveTimeline();
994+ // const events = liveTimeline.getEvents();
995+ // const mostRecentEventInTimeline = events[events.length - 1];
996+
997+ // // This will create a timeline with the given event and add it to the timelineSet
998+ // const newTimeline = await this.client.getEventTimeline(timelineSet, mostRecentEventInTimeline.getId());
999+
1000+ // console.log('refreshTimeline: timelineSet',
1001+ // timelineSet,
1002+ // timelineSet.getTimelines().map((timeline) => {
1003+ // return timeline.getEvents().length;
1004+ // }),
1005+ // );
1006+
1007+ // // Since timelineSets is a readonly property, clear it out this way
1008+ // this.timelineSets.length = 0;
1009+ // // Keep track of the new timelineSet
1010+ // this.timelineSets.push(timelineSet);
1011+ // // Set our new timeline as the liveTimeline
1012+ // timelineSet.setLiveTimeline(newTimeline);
1013+ // // Fixup `this.oldstate` so that `scrollback` has the pagination tokens available
1014+ // this.fixUpLegacyTimelineFields();
1015+
1016+
1017+ // //this.emit(RoomEvent.Timeline, event, this.room, Boolean(toStartOfTimeline), false, data);
1018+ // // setTimeout(() => {
1019+ // // this.emit(RoomEvent.TimelineReset, this, timelineSet, true);
1020+ // // }, 1000);
1021+ // this.emit(RoomEvent.TimelineRefresh, this, timelineSet);
1022+ }
1023+
9291024 /**
9301025 * Reset the live timeline of all timelineSets, and start new ones.
9311026 *
@@ -953,6 +1048,11 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
9531048 * @private
9541049 */
9551050 private fixUpLegacyTimelineFields ( ) : void {
1051+ console . log (
1052+ 'fixUpLegacyTimelineFields' ,
1053+ this . getLiveTimeline ( ) . getState ( EventTimeline . BACKWARDS ) ,
1054+ this . getLiveTimeline ( ) . getState ( EventTimeline . FORWARDS )
1055+ ) ;
9561056 // maintain this.timeline as a reference to the live timeline,
9571057 // and this.oldState and this.currentState as references to the
9581058 // state at the start and end of that timeline. These are more
0 commit comments