fix: Limited timeline clears too much events

This fixes the bug that the
limited timeline flag also
clears all events from the
current SyncUpdate in an
open timeline.
This commit is contained in:
Krille Fear 2021-11-23 09:06:30 +01:00
parent 5da0180f1f
commit 9cbe1099e5
1 changed files with 10 additions and 4 deletions

View File

@ -120,13 +120,12 @@ class Timeline {
this.onRemove,
}) : events = events ?? [] {
sub = room.client.onEvent.stream.listen(_handleEventUpdate);
// If the timeline is limited we want to clear our events cache
roomSub = room.client.onSync.stream
.where((sync) => sync.rooms?.join?[room.id]?.timeline?.limited == true)
.listen((_) {
this.events.clear();
aggregatedEvents.clear();
});
.listen(_removeEventsNotInThisSync);
sessionIdReceivedSub =
room.onSessionKeyReceived.stream.listen(_sessionKeyReceived);
@ -136,6 +135,13 @@ class Timeline {
}
}
/// Removes all entries from [events] which are not in this SyncUpdate.
void _removeEventsNotInThisSync(SyncUpdate sync) {
final newSyncEvents = sync.rooms?.join?[room.id]?.timeline?.events ?? [];
final keepEventIds = newSyncEvents.map((e) => e.eventId);
events.removeWhere((e) => !keepEventIds.contains(e.eventId));
}
/// Don't forget to call this before you dismiss this object!
void cancelSubscriptions() {
sub?.cancel();