Skip to content

Commit cc76f2a

Browse files
committed
unreads [nfc]: Use for/in style instead of forEach
Sometimes we get feedback from a lint rule -- https://dart.dev/tools/linter-rules/avoid_function_literals_in_foreach_calls -- whose documentation recommends using for loops instead of forEach. That rule didn't fire on these instances; shrug; but this file has had a mix of for loops and forEach loops, and it seems nicer to consistently use one or the other. Just in case there was a performance bug in a Map.forEach implementation, I tested mark-as-read (by scrolling through the web app) to see if we might get a free performance benefit there, where the norm on my device (with a profile build) has been to take about 14ms per event: zulip#304 (comment) But, unsurprisingly, it stayed at around 14ms as before.
1 parent a7f7fff commit cc76f2a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

lib/model/unreads.dart

+21-12
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,23 @@ class Unreads extends ChangeNotifier {
287287
.add(messageId);
288288
}
289289
}
290-
newlyUnreadInStreams.forEach((incomingStreamId, incomingTopics) {
291-
incomingTopics.forEach((incomingTopic, incomingMessageIds) {
290+
for (
291+
final MapEntry(key: incomingStreamId, value: incomingTopics)
292+
in newlyUnreadInStreams.entries
293+
) {
294+
for (
295+
final MapEntry(key: incomingTopic, value: incomingMessageIds)
296+
in incomingTopics.entries
297+
) {
292298
_addAllInStreamTopic(incomingMessageIds..sort(), incomingStreamId, incomingTopic);
293-
});
294-
});
295-
newlyUnreadInDms.forEach((incomingDmNarrow, incomingMessageIds) {
299+
}
300+
}
301+
for (
302+
final MapEntry(key: incomingDmNarrow, value: incomingMessageIds)
303+
in newlyUnreadInDms.entries
304+
) {
296305
_addAllInDm(incomingMessageIds..sort(), incomingDmNarrow);
297-
});
306+
}
298307
}
299308
}
300309
notifyListeners();
@@ -328,21 +337,21 @@ class Unreads extends ChangeNotifier {
328337
// TODO use efficient model lookups
329338
void _slowRemoveAllInStreams(Set<int> idsToRemove) {
330339
final newlyEmptyStreams = [];
331-
streams.forEach((streamId, topics) {
340+
for (final MapEntry(key: streamId, value: topics) in streams.entries) {
332341
final newlyEmptyTopics = [];
333-
topics.forEach((topic, messageIds) {
342+
for (final MapEntry(key: topic, value: messageIds) in topics.entries) {
334343
messageIds.removeWhere((id) => idsToRemove.contains(id));
335344
if (messageIds.isEmpty) {
336345
newlyEmptyTopics.add(topic);
337346
}
338-
});
347+
}
339348
for (final topic in newlyEmptyTopics) {
340349
topics.remove(topic);
341350
}
342351
if (topics.isEmpty) {
343352
newlyEmptyStreams.add(streamId);
344353
}
345-
});
354+
}
346355
for (final streamId in newlyEmptyStreams) {
347356
streams.remove(streamId);
348357
}
@@ -387,12 +396,12 @@ class Unreads extends ChangeNotifier {
387396
// TODO use efficient model lookups
388397
void _slowRemoveAllInDms(Set<int> idsToRemove) {
389398
final newlyEmptyDms = [];
390-
dms.forEach((dmNarrow, messageIds) {
399+
for (final MapEntry(key: dmNarrow, value: messageIds) in dms.entries) {
391400
messageIds.removeWhere((id) => idsToRemove.contains(id));
392401
if (messageIds.isEmpty) {
393402
newlyEmptyDms.add(dmNarrow);
394403
}
395-
});
404+
}
396405
for (final dmNarrow in newlyEmptyDms) {
397406
dms.remove(dmNarrow);
398407
}

0 commit comments

Comments
 (0)