Skip to content

Commit ba54b24

Browse files
chrisbobbesirpengi
authored andcommitted
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; not sure why. But anyway 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 f113946 commit ba54b24

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/model/unreads.dart

+15-12
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,17 @@ class Unreads extends ChangeNotifier {
287287
.add(messageId);
288288
}
289289
}
290-
newlyUnreadInStreams.forEach((incomingStreamId, incomingTopics) {
291-
incomingTopics.forEach((incomingTopic, incomingMessageIds) {
290+
for (final MapEntry(key: incomingStreamId, value: incomingTopics)
291+
in newlyUnreadInStreams.entries) {
292+
for (final MapEntry(key: incomingTopic, value: incomingMessageIds)
293+
in incomingTopics.entries) {
292294
_addAllInStreamTopic(incomingMessageIds..sort(), incomingStreamId, incomingTopic);
293-
});
294-
});
295-
newlyUnreadInDms.forEach((incomingDmNarrow, incomingMessageIds) {
295+
}
296+
}
297+
for (final MapEntry(key: incomingDmNarrow, value: incomingMessageIds)
298+
in newlyUnreadInDms.entries) {
296299
_addAllInDm(incomingMessageIds..sort(), incomingDmNarrow);
297-
});
300+
}
298301
}
299302
}
300303
notifyListeners();
@@ -328,21 +331,21 @@ class Unreads extends ChangeNotifier {
328331
// TODO use efficient model lookups
329332
void _slowRemoveAllInStreams(Set<int> idsToRemove) {
330333
final newlyEmptyStreams = [];
331-
streams.forEach((streamId, topics) {
334+
for (final MapEntry(key: streamId, value: topics) in streams.entries) {
332335
final newlyEmptyTopics = [];
333-
topics.forEach((topic, messageIds) {
336+
for (final MapEntry(key: topic, value: messageIds) in topics.entries) {
334337
messageIds.removeWhere((id) => idsToRemove.contains(id));
335338
if (messageIds.isEmpty) {
336339
newlyEmptyTopics.add(topic);
337340
}
338-
});
341+
}
339342
for (final topic in newlyEmptyTopics) {
340343
topics.remove(topic);
341344
}
342345
if (topics.isEmpty) {
343346
newlyEmptyStreams.add(streamId);
344347
}
345-
});
348+
}
346349
for (final streamId in newlyEmptyStreams) {
347350
streams.remove(streamId);
348351
}
@@ -387,12 +390,12 @@ class Unreads extends ChangeNotifier {
387390
// TODO use efficient model lookups
388391
void _slowRemoveAllInDms(Set<int> idsToRemove) {
389392
final newlyEmptyDms = [];
390-
dms.forEach((dmNarrow, messageIds) {
393+
for (final MapEntry(key: dmNarrow, value: messageIds) in dms.entries) {
391394
messageIds.removeWhere((id) => idsToRemove.contains(id));
392395
if (messageIds.isEmpty) {
393396
newlyEmptyDms.add(dmNarrow);
394397
}
395-
});
398+
}
396399
for (final dmNarrow in newlyEmptyDms) {
397400
dms.remove(dmNarrow);
398401
}

0 commit comments

Comments
 (0)