Skip to content

Commit f44489d

Browse files
committed
Raw refreshLiveTimeline function that seems to work
1 parent 2468690 commit f44489d

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

src/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5135,6 +5135,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
51355135
* @return {module:http-api.MatrixError} Rejects: with an error response.
51365136
*/
51375137
public scrollback(room: Room, limit = 30, callback?: Callback): Promise<Room> {
5138+
console.log('client: scrollback');
51385139
if (utils.isFunction(limit)) {
51395140
callback = limit as any as Callback; // legacy
51405141
limit = undefined;
@@ -5150,11 +5151,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
51505151
}
51515152

51525153
if (room.oldState.paginationToken === null) {
5154+
console.log('client: scrollabck already at the start.', room.oldState);
51535155
return Promise.resolve(room); // already at the start.
51545156
}
51555157
// attempt to grab more events from the store first
51565158
const numAdded = this.store.scrollback(room, limit).length;
51575159
if (numAdded === limit) {
5160+
console.log(`client: scrollback store contained everything we needed numAdded=${numAdded} limit=${limit}`);
51585161
// store contained everything we needed.
51595162
return Promise.resolve(room);
51605163
}
@@ -5181,6 +5184,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
51815184
const [timelineEvents, threadedEvents] = room.partitionThreadedEvents(matrixEvents);
51825185

51835186
this.processBeaconEvents(room, matrixEvents);
5187+
console.log('client: scrollback addEventsToTimeline', timelineEvents.length, room.getLiveTimeline());
51845188
room.addEventsToTimeline(timelineEvents, true, room.getLiveTimeline());
51855189
await this.processThreadEvents(room, threadedEvents, true);
51865190

@@ -5307,7 +5311,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
53075311
}
53085312

53095313
// Here we handle non-thread timelines only, but still process any thread events to populate thread summaries.
5314+
console.log('getEventTimeline: timeline count1', timelineSet.getTimelines().length);
53105315
let timeline = timelineSet.getTimelineForEvent(events[0].getId());
5316+
console.log('getEventTimeline: timeline count2', timelineSet.getTimelines().length);
53115317
if (timeline) {
53125318
timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(res.state.map(mapper));
53135319
} else {
@@ -5316,8 +5322,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
53165322
timeline.getState(EventTimeline.FORWARDS).paginationToken = res.end;
53175323
}
53185324

5325+
console.log('getEventTimeline: timeline before', timelineSet.getTimelines().length, timeline.getEvents().length);
5326+
53195327
const [timelineEvents, threadedEvents] = timelineSet.room.partitionThreadedEvents(events);
53205328
timelineSet.addEventsToTimeline(timelineEvents, true, timeline, res.start);
5329+
console.log('getEventTimeline: timeline after', timelineSet.getTimelines().length, timeline.getEvents().length);
53215330
// The target event is not in a thread but process the contextual events, so we can show any threads around it.
53225331
await this.processThreadEvents(timelineSet.room, threadedEvents, true);
53235332
this.processBeaconEvents(timelineSet.room, events);

src/models/event-timeline-set.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ export class EventTimelineSet extends TypedEventEmitter<EmittedEvents, EventTime
216216
return this.liveTimeline;
217217
}
218218

219+
/**
220+
* Get the live timeline for this room.
221+
*
222+
* @return {module:models/event-timeline~EventTimeline} live timeline
223+
*/
224+
public setLiveTimeline(timeline: EventTimeline): void {
225+
this.liveTimeline = timeline;
226+
}
227+
219228
/**
220229
* Return the timeline (if any) this event is in.
221230
* @param {String} eventId the eventId being sought

src/models/room.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)