From 6a81fbc0aac314a31b4f617606782c989d697f52 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 26 Sep 2019 09:30:07 +0000 Subject: [PATCH] [Timeline] Better HistoryRequest in Timeline --- lib/src/Room.dart | 12 ++++++++++-- lib/src/Timeline.dart | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 9feb029..c037999 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -174,6 +174,10 @@ class Room { this.roomAccountData = const {}, }); + /// The default count of how much events should be requested when requesting the + /// history of this room. + static const int DefaultHistoryCount = 100; + /// Calculates the displayname. First checks if there is a name, then checks for a canonical alias and /// then generates a name from the heroes. String get displayname { @@ -445,8 +449,11 @@ class Room { return res; } - /// Request more previous events from the server. - Future requestHistory({int historyCount = 100}) async { + /// Request more previous events from the server. [historyCount] defines how much events should + /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before** + /// the historical events will be published in the onEvent stream. + Future requestHistory( + {int historyCount = DefaultHistoryCount, onHistoryReceived}) async { final dynamic resp = await client.connection.jsonRequest( type: HTTPType.GET, action: @@ -454,6 +461,7 @@ class Room { if (resp is ErrorResponse) return; + if (onHistoryReceived != null) onHistoryReceived(); prev_batch = resp["end"]; client.store?.storeRoomPrevBatch(this); diff --git a/lib/src/Timeline.dart b/lib/src/Timeline.dart index aeb89df..2db6cdf 100644 --- a/lib/src/Timeline.dart +++ b/lib/src/Timeline.dart @@ -43,6 +43,14 @@ class Timeline { StreamSubscription sub; + Future requestHistory({int historyCount = Room.DefaultHistoryCount}) { + return room.requestHistory( + historyCount: historyCount, + onHistoryReceived: () { + if (room.prev_batch.isEmpty || room.prev_batch == null) events = []; + }); + } + Timeline({this.room, this.events, this.onUpdate, this.onInsert}) { sub ??= room.client.connection.onEvent.stream.listen(_handleEventUpdate); }