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