From 6c31c87ee76195e254ff5735104673d30648ee07 Mon Sep 17 00:00:00 2001 From: WilsonLe Date: Tue, 23 Jul 2024 16:12:03 -0400 Subject: [PATCH] introduce room timeline reference reuse --- lib/src/models/timeline_chunk.dart | 13 +++++++++++++ lib/src/room.dart | 25 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/src/models/timeline_chunk.dart b/lib/src/models/timeline_chunk.dart index a1e7002d0..7334eeb1c 100644 --- a/lib/src/models/timeline_chunk.dart +++ b/lib/src/models/timeline_chunk.dart @@ -7,4 +7,17 @@ class TimelineChunk { List events; 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 e0453883f..9e0f0dc83 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1412,6 +1412,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 @@ -1460,14 +1462,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) {