Skip to content

Commit dcaa3e5

Browse files
committed
msglist [nfc]: Maintain middleItem as a field
This new logic maintains `middleItem` according to its documented relationship with `middleMessage`. Because of the current definition of `middleMessage`, that produces the same result as the previous definition of `middleItem`. The key reasoning for why this logic works is: this touches all the code that modifies `items`, to ensure that code keeps `middleItem` up to date. And all the code which modifies `messages` (which is the only way to modify `middleMessage`) already calls `_reprocessAll` to compute `items` from scratch, except one site in `_addMessage`. Studying `_addMessage`, it also maintains `middleItem` correctly, though for that conclusion one needs the specifics of the definition of `middleMessage`. This change involves no new test code: all this logic is in scenarios well exercised by existing tests, and the invariant-checks introduced in the previous commit then effectively test this logic. To be sure of that, I also confirmed that commenting out any one of these updates to `middleItem` causes some tests to fail.
1 parent 13a20b1 commit dcaa3e5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lib/model/message_list.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ mixin _MessageSequence {
163163
/// Either the bottom slices of both [items] and [messages] are empty,
164164
/// or the first item in the bottom slice of [items] is a [MessageListMessageItem]
165165
/// for the first message in the bottom slice of [messages].
166-
int get middleItem => items.isEmpty ? 0 : items.length - 1;
166+
int middleItem = 0;
167167

168168
int _findMessageWithId(int messageId) {
169169
return binarySearchByKey(messages, messageId,
@@ -295,6 +295,7 @@ mixin _MessageSequence {
295295
_fetchOlderCooldownBackoffMachine = null;
296296
contents.clear();
297297
items.clear();
298+
middleItem = 0;
298299
}
299300

300301
/// Redo all computations from scratch, based on [messages].
@@ -334,6 +335,7 @@ mixin _MessageSequence {
334335
canShareSender = (prevMessageItem.message.senderId == message.senderId);
335336
}
336337
}
338+
if (index == middleMessage) middleItem = items.length;
337339
items.add(MessageListMessageItem(message, content,
338340
showSender: !canShareSender, isLastInBlock: true));
339341
}
@@ -344,6 +346,7 @@ mixin _MessageSequence {
344346
for (var i = 0; i < messages.length; i++) {
345347
_processMessage(i);
346348
}
349+
if (middleMessage == messages.length) middleItem = items.length;
347350
}
348351
}
349352

0 commit comments

Comments
 (0)