requestHistory method
Future<int>
requestHistory({
- int historyCount = defaultHistoryCount,
- void onHistoryReceived()?,
- dynamic direction = Direction.b,
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.
Returns the actual count of received timeline events.
Implementation
Future<int> requestHistory(
{int historyCount = defaultHistoryCount,
void Function()? onHistoryReceived,
direction = Direction.b}) async {
final prev_batch = this.prev_batch;
final storeInDatabase = !isArchived;
if (prev_batch == null) {
throw 'Tried to request history without a prev_batch token';
}
final resp = await client.getRoomEvents(
id,
direction,
from: prev_batch,
limit: historyCount,
filter: jsonEncode(StateFilter(lazyLoadMembers: true).toJson()),
);
if (onHistoryReceived != null) onHistoryReceived();
this.prev_batch = resp.end;
Future<void> loadFn() async {
if (!((resp.chunk.isNotEmpty) && resp.end != null)) return;
await client.handleSync(
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: membership == Membership.join
? {
id: JoinedRoomUpdate(
state: resp.state,
timeline: TimelineUpdate(
limited: false,
events: direction == Direction.b
? resp.chunk
: resp.chunk.reversed.toList(),
prevBatch: direction == Direction.b
? resp.end
: resp.start,
),
)
}
: null,
leave: membership != Membership.join
? {
id: LeftRoomUpdate(
state: resp.state,
timeline: TimelineUpdate(
limited: false,
events: direction == Direction.b
? resp.chunk
: resp.chunk.reversed.toList(),
prevBatch: direction == Direction.b
? resp.end
: resp.start,
),
),
}
: null),
),
direction: Direction.b);
}
if (client.database != null) {
await client.database?.transaction(() async {
if (storeInDatabase) {
await client.database?.setRoomPrevBatch(resp.end!, id, client);
}
await loadFn();
});
} else {
await loadFn();
}
return resp.chunk.length;
}