[Timeline] Better HistoryRequest in Timeline

This commit is contained in:
Christian Pauly 2019-09-26 09:30:07 +00:00
parent 97034743fe
commit 6a81fbc0aa
2 changed files with 18 additions and 2 deletions

View file

@ -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<void> 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<void> 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);

View file

@ -43,6 +43,14 @@ class Timeline {
StreamSubscription<EventUpdate> sub;
Future<void> 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);
}