From 51dbb90436dbcde947b803f84b95240df7e884e5 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 16 May 2024 19:08:43 +0200 Subject: [PATCH] fix: canRequestHistory doesn't reflect reality canRequestHistory could become false, even though you didn't load all events from the database yet. So you had to either rely on fetching until the requestHistory method throws or rely on other workarounds. This makes requestHistory not throw, when the prev_batch is null, but also makes canRequestHistory return sane values (but might require an extra request, that does nothing). --- lib/src/timeline.dart | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 5934bf35..1a629e6a 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -75,9 +75,13 @@ class Timeline { // even if /sync's complete while history is being proccessed. bool _collectHistoryUpdates = false; + // We confirmed, that there are no more events to load from the database. + bool _fetchedAllDatabaseEvents = false; + bool get canRequestHistory { if (events.isEmpty) return true; - return room.prev_batch != null && events.last.type != EventTypes.RoomCreate; + return !_fetchedAllDatabaseEvents || + (room.prev_batch != null && events.last.type != EventTypes.RoomCreate); } Future requestHistory( @@ -147,20 +151,26 @@ class Timeline { } } } else { + _fetchedAllDatabaseEvents = true; Logs().i('No more events found in the store. Request from server...'); + if (isFragmentedTimeline) { await getRoomEvents( historyCount: historyCount, direction: direction, ); } else { - await room.requestHistory( - historyCount: historyCount, - direction: direction, - onHistoryReceived: () { - _collectHistoryUpdates = true; - }, - ); + if (room.prev_batch == null) { + Logs().i('No more events to request from server...'); + } else { + await room.requestHistory( + historyCount: historyCount, + direction: direction, + onHistoryReceived: () { + _collectHistoryUpdates = true; + }, + ); + } } } } finally { @@ -291,6 +301,8 @@ class Timeline { if (chunk.nextBatch != '') { allowNewEvent = false; isFragmentedTimeline = true; + // fragmented timelines never read from the database. + _fetchedAllDatabaseEvents = true; } }