clear timeline events cache on limited updates

This commit is contained in:
Sorunome 2020-05-21 14:52:14 +00:00 committed by Christian Pauly
parent 998ee66650
commit fc8625d30e
2 changed files with 23 additions and 0 deletions

View file

@ -26,6 +26,7 @@ import 'dart:async';
import 'event.dart';
import 'room.dart';
import 'sync/event_update.dart';
import 'sync/room_update.dart';
typedef onTimelineUpdateCallback = void Function();
typedef onTimelineInsertCallback = void Function(int insertID);
@ -41,6 +42,7 @@ class Timeline {
final onTimelineInsertCallback onInsert;
StreamSubscription<EventUpdate> sub;
StreamSubscription<RoomUpdate> roomSub;
StreamSubscription<String> sessionIdReceivedSub;
bool _requestingHistoryLock = false;
@ -77,6 +79,11 @@ class Timeline {
Timeline({this.room, this.events, this.onUpdate, this.onInsert}) {
sub ??= room.client.onEvent.stream.listen(_handleEventUpdate);
// if the timeline is limited we want to clear our events cache
// as r.limitedTimeline can be "null" sometimes, we need to check for == true
// as after receiving a limited timeline room update new events are expected
// to be received via the onEvent stream, it is unneeded to call sortAndUpdate
roomSub ??= room.client.onRoomUpdate.stream.where((r) => r.id == room.id && r.limitedTimeline == true).listen((r) => events.clear());
sessionIdReceivedSub ??=
room.onSessionKeyReceived.stream.listen(_sessionKeyReceived);
}
@ -84,6 +91,7 @@ class Timeline {
/// Don't forget to call this before you dismiss this object!
void cancelSubscriptions() {
sub?.cancel();
roomSub?.cancel();
sessionIdReceivedSub?.cancel();
}

View file

@ -26,7 +26,9 @@ import 'package:test/test.dart';
import 'package:famedlysdk/src/client.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/timeline.dart';
import 'package:famedlysdk/src/user.dart';
import 'package:famedlysdk/src/sync/event_update.dart';
import 'package:famedlysdk/src/sync/room_update.dart';
import 'fake_matrix_api.dart';
void main() {
@ -240,5 +242,18 @@ void main() {
expect(timeline.events[8].eventId, '1143273582443PhrSn:example.org');
expect(room.prev_batch, 't47409-4357353_219380_26003_2265');
});
test('Clear cache on limited timeline', () async {
client.onRoomUpdate.add(RoomUpdate(
id: roomID,
membership: Membership.join,
notification_count: 0,
highlight_count: 0,
limitedTimeline: true,
prev_batch: 'blah',
));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events.isEmpty, true);
});
});
}