diff --git a/lib/src/models/timeline_chunk.dart b/lib/src/models/timeline_chunk.dart index 7cdaef10f..7334eeb1c 100644 --- a/lib/src/models/timeline_chunk.dart +++ b/lib/src/models/timeline_chunk.dart @@ -5,9 +5,19 @@ class TimelineChunk { String nextBatch; List events; - TimelineChunk({ - required this.events, - this.prevBatch = '', - this.nextBatch = '', - }); + TimelineChunk( + {required this.events, this.prevBatch = '', this.nextBatch = ''}); + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + if (other is! TimelineChunk) return false; + + // Compare the lists of event ids regardless of order + final thisEventIds = events.map((e) => e.eventId).toSet(); + final otherEventIds = other.events.map((e) => e.eventId).toSet(); + + return thisEventIds.length == otherEventIds.length && + thisEventIds.containsAll(otherEventIds); + } } diff --git a/lib/src/room.dart b/lib/src/room.dart index 10b9db302..511714276 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1473,6 +1473,8 @@ class Room { /// Is the room archived bool get isArchived => membership == Membership.leave; + Timeline? _timeline; + /// 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 @@ -1522,15 +1524,21 @@ class Room { } } - final timeline = Timeline( - room: this, - chunk: chunk, - onChange: onChange, - onRemove: onRemove, - onInsert: onInsert, - onNewEvent: onNewEvent, - onUpdate: onUpdate, - ); + Timeline timeline; + if (_timeline != null && _timeline!.chunk == chunk) { + timeline = _timeline!; + chunk = _timeline!.chunk; + } else { + timeline = Timeline( + room: this, + chunk: chunk, + onChange: onChange, + onRemove: onRemove, + onInsert: onInsert, + onNewEvent: onNewEvent, + onUpdate: onUpdate); + _timeline = timeline; + } // Fetch all users from database we have got here. if (eventContextId == null) {