From fc8625d30e78b5dcc87c0c767d225500903cf9ba Mon Sep 17 00:00:00 2001 From: Sorunome Date: Thu, 21 May 2020 14:52:14 +0000 Subject: [PATCH] clear timeline events cache on limited updates --- lib/src/timeline.dart | 8 ++++++++ test/timeline_test.dart | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 9b765f8..9cb0762 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -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 sub; + StreamSubscription roomSub; StreamSubscription 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(); } diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 28c006f..e2cda2e 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -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); + }); }); }