Skip to content

Commit

Permalink
unreads: Enhance performance in _isPresentInStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
Khader-1 committed Aug 9, 2024
1 parent 276971b commit c71160e
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/model/unreads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class Unreads extends ChangeNotifier {
final bool isRead = event.flags.contains(MessageFlag.read);
assert(() {
if (!oldUnreadsMissing && !event.messageIds.every((messageId) {
final isUnreadLocally = _slowIsPresentInDms(messageId) || _slowIsPresentInStreams(messageId);
final isUnreadLocally = _slowIsPresentInDms(messageId) || _isPresentInStreams(messageId);
return isUnreadLocally == !isRead;
})) {
// If this happens, then either:
Expand Down Expand Up @@ -335,7 +335,7 @@ class Unreads extends ChangeNotifier {
case UpdateMessageFlagsAddEvent():
mentions.addAll(
event.messages.where(
(messageId) => _slowIsPresentInStreams(messageId) || _slowIsPresentInDms(messageId),
(messageId) => _isPresentInStreams(messageId) || _slowIsPresentInDms(messageId),
),
);

Expand Down Expand Up @@ -434,13 +434,8 @@ class Unreads extends ChangeNotifier {
notifyListeners();
}

// TODO use efficient lookups
bool _slowIsPresentInStreams(int messageId) {
return streams.values.any(
(topics) => topics.values.any(
(messageIds) => messageIds.contains(messageId),
),
);
bool _isPresentInStreams(int messageId) {
return _reverseStreamsLookup.containsKey(messageId);
}

void _addLastInStreamTopic(int messageId, int streamId, String topic) {
Expand Down

0 comments on commit c71160e

Please sign in to comment.