@@ -355,7 +355,7 @@ class Unreads extends ChangeNotifier {
355
355
} else {
356
356
final messageIdsSet = Set .of (event.messages);
357
357
mentions.removeAll (messageIdsSet);
358
- _slowRemoveAllInStreams (messageIdsSet);
358
+ _removeAllInStreams (messageIdsSet);
359
359
_slowRemoveAllInDms (messageIdsSet);
360
360
}
361
361
case UpdateMessageFlagsRemoveEvent ():
@@ -458,29 +458,18 @@ class Unreads extends ChangeNotifier {
458
458
messageIds.map ((messageId) => MapEntry (messageId, messageInfo)));
459
459
}
460
460
461
- // TODO use efficient model lookups
462
- void _slowRemoveAllInStreams (Set <int > idsToRemove) {
463
- final newlyEmptyStreams = < int > [];
464
- for (final MapEntry (key: streamId, value: topics) in streams.entries) {
465
- final newlyEmptyTopics = < String > [];
466
- for (final MapEntry (key: topic, value: messageIds) in topics.entries) {
467
- messageIds.removeWhere ((id) => idsToRemove.contains (id));
468
- if (messageIds.isEmpty) {
469
- newlyEmptyTopics.add (topic);
461
+ void _removeAllInStreams (Set <int > idsToRemove) {
462
+ for (var messageId in idsToRemove) {
463
+ final info = _reverseStreamsLookup.remove (messageId);
464
+ if (info != null && streams[info.streamId]? [info.topic] != null ) {
465
+ streams[info.streamId]! [info.topic]! .remove (messageId);
466
+ if (streams[info.streamId]! [info.topic]! .isEmpty) {
467
+ streams[info.streamId]! .remove (info.topic);
468
+ }
469
+ if (streams[info.streamId]! .isEmpty) {
470
+ streams.remove (info.streamId);
470
471
}
471
472
}
472
- for (final topic in newlyEmptyTopics) {
473
- topics.remove (topic);
474
- }
475
- if (topics.isEmpty) {
476
- newlyEmptyStreams.add (streamId);
477
- }
478
- }
479
- for (final streamId in newlyEmptyStreams) {
480
- streams.remove (streamId);
481
- }
482
- for (var messageId in idsToRemove) {
483
- _reverseStreamsLookup.remove (messageId);
484
473
}
485
474
}
486
475
0 commit comments