@@ -312,6 +312,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
312
312
///
313
313
/// This depends in particular on whether the message is muted in
314
314
/// one way or another.
315
+ ///
316
+ /// See also [_allMessagesVisible] .
315
317
bool _messageVisible (Message message) {
316
318
switch (narrow) {
317
319
case AllMessagesNarrow ():
@@ -332,6 +334,21 @@ class MessageListView with ChangeNotifier, _MessageSequence {
332
334
}
333
335
}
334
336
337
+ /// Whether [_messageVisible] is true for all possible messages.
338
+ ///
339
+ /// This is useful for an optimization.
340
+ bool get _allMessagesVisible {
341
+ switch (narrow) {
342
+ case AllMessagesNarrow ():
343
+ case StreamNarrow ():
344
+ return false ;
345
+
346
+ case TopicNarrow ():
347
+ case DmNarrow ():
348
+ return true ;
349
+ }
350
+ }
351
+
335
352
/// Fetch messages, starting from scratch.
336
353
Future <void > fetchInitial () async {
337
354
// TODO(#80): fetch from anchor firstUnread, instead of newest
@@ -380,7 +397,11 @@ class MessageListView with ChangeNotifier, _MessageSequence {
380
397
result.messages.removeLast ();
381
398
}
382
399
383
- _insertAllMessages (0 , result.messages.where (_messageVisible));
400
+ final fetchedMessages = _allMessagesVisible
401
+ ? result.messages // Avoid unnecessarily copying the list.
402
+ : result.messages.where (_messageVisible);
403
+
404
+ _insertAllMessages (0 , fetchedMessages);
384
405
_haveOldest = result.foundOldest;
385
406
} finally {
386
407
_fetchingOlder = false ;
0 commit comments