refactor: Implement on history received in timeline
In order to make delta updates for GUIs displaying the timeline events easier, the onHistoryReceived callback has been introduced.
This commit is contained in:
parent
e30c0f7fa2
commit
01d13e7e2c
|
|
@ -983,7 +983,8 @@ class Room {
|
|||
/// 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(
|
||||
/// Returns the actual count of received timeline events.
|
||||
Future<int> requestHistory(
|
||||
{int historyCount = defaultHistoryCount,
|
||||
void Function()? onHistoryReceived}) async {
|
||||
final prev_batch = this.prev_batch;
|
||||
|
|
@ -1044,6 +1045,8 @@ class Room {
|
|||
} else {
|
||||
await loadFn();
|
||||
}
|
||||
|
||||
return resp.chunk?.length ?? 0;
|
||||
}
|
||||
|
||||
/// Sets this room as a direct chat for this user if not already.
|
||||
|
|
@ -1131,12 +1134,13 @@ class Room {
|
|||
/// Creates a timeline from the store. Returns a [Timeline] object. If you
|
||||
/// just want to update the whole timeline on every change, use the [onUpdate]
|
||||
/// callback. For updating only the parts that have changed, use the
|
||||
/// [onChange], [onRemove] and the [onInsert] callbacks.
|
||||
/// [onChange], [onRemove], [onInsert] and the [onHistoryReceived] callbacks.
|
||||
Future<Timeline> getTimeline({
|
||||
void Function(int index)? onChange,
|
||||
void Function(int index)? onRemove,
|
||||
void Function(int insertID)? onInsert,
|
||||
void Function()? onUpdate,
|
||||
void Function(int count)? onHistoryReceived,
|
||||
}) async {
|
||||
await postLoad();
|
||||
var events;
|
||||
|
|
@ -1166,6 +1170,7 @@ class Room {
|
|||
onRemove: onRemove,
|
||||
onInsert: onInsert,
|
||||
onUpdate: onUpdate,
|
||||
onHistoryReceived: onHistoryReceived,
|
||||
);
|
||||
if (client.database == null) {
|
||||
await requestHistory(historyCount: 10);
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ class Timeline {
|
|||
final void Function(int index)? onChange;
|
||||
final void Function(int index)? onInsert;
|
||||
final void Function(int index)? onRemove;
|
||||
final void Function(int count)? onHistoryReceived;
|
||||
|
||||
StreamSubscription<EventUpdate>? sub;
|
||||
StreamSubscription<SyncUpdate>? roomSub;
|
||||
|
|
@ -86,19 +87,16 @@ class Timeline {
|
|||
);
|
||||
if (eventsFromStore != null && eventsFromStore.isNotEmpty) {
|
||||
events.addAll(eventsFromStore);
|
||||
final startIndex = events.length - eventsFromStore.length;
|
||||
final endIndex = events.length;
|
||||
for (var i = startIndex; i < endIndex; i++) {
|
||||
onInsert?.call(i);
|
||||
}
|
||||
onHistoryReceived?.call(eventsFromStore.length);
|
||||
} else {
|
||||
Logs().v('No more events found in the store. Request from server...');
|
||||
await room.requestHistory(
|
||||
final count = await room.requestHistory(
|
||||
historyCount: historyCount,
|
||||
onHistoryReceived: () {
|
||||
_collectHistoryUpdates = true;
|
||||
},
|
||||
);
|
||||
onHistoryReceived?.call(count);
|
||||
}
|
||||
} finally {
|
||||
_collectHistoryUpdates = false;
|
||||
|
|
@ -114,6 +112,7 @@ class Timeline {
|
|||
this.onChange,
|
||||
this.onInsert,
|
||||
this.onRemove,
|
||||
this.onHistoryReceived,
|
||||
}) : events = events ?? [] {
|
||||
sub = room.client.onEvent.stream.listen(_handleEventUpdate);
|
||||
|
||||
|
|
@ -331,11 +330,10 @@ class Timeline {
|
|||
} else {
|
||||
index = events.firstIndexWhereNotError;
|
||||
events.insert(index, newEvent);
|
||||
onInsert?.call(index);
|
||||
}
|
||||
|
||||
addAggregatedEvent(newEvent);
|
||||
|
||||
onInsert?.call(index);
|
||||
}
|
||||
}
|
||||
if (update && !_collectHistoryUpdates) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue