Skip to content

Commit

Permalink
introduce room timeline reference reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsonLe committed Jul 23, 2024
1 parent e9ff20a commit 6c31c87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
13 changes: 13 additions & 0 deletions lib/src/models/timeline_chunk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ class TimelineChunk {
List<Event> 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);
}
}
25 changes: 17 additions & 8 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6c31c87

Please sign in to comment.