Skip to content

Commit 8593e1a

Browse files
committed
message [nfc]: Pull out some helpers for reconcileMessages; add TODO(zulip#1798)
1 parent 21ac9c6 commit 8593e1a

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/model/message.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,23 @@ class MessageStoreImpl extends HasChannelStore with MessageStore, _OutboxMessage
400400

401401
void reconcileMessages(List<Message> messages) {
402402
assert(!_disposed);
403-
// What to do when some of the just-fetched messages are already known?
404-
// This is common and normal: in particular it happens when one message list
405-
// overlaps another, e.g. a stream and a topic within it.
403+
for (int i = 0; i < messages.length; i++) {
404+
final message = messages[i];
405+
406+
messages[i] = this.messages.update(message.id,
407+
ifAbsent: () => _reconcileUnrecognizedMessage(message),
408+
(current) => _reconcileRecognizedMessage(current, message));
409+
}
410+
}
411+
412+
Message _reconcileUnrecognizedMessage(Message incoming) {
413+
return _stripMatchFields(incoming);
414+
}
415+
416+
Message _reconcileRecognizedMessage(Message current, Message incoming) {
417+
// This message is one we already know about. This is common and normal:
418+
// in particular it happens when one message list overlaps another,
419+
// e.g. a stream and a topic within it.
406420
//
407421
// Most often, the just-fetched message will look just like the one we
408422
// already have. But they can differ: message fetching happens out of band
@@ -415,12 +429,8 @@ class MessageStoreImpl extends HasChannelStore with MessageStore, _OutboxMessage
415429
// we won't hear those events again; the only way to wind up with an
416430
// updated message is to use the version we have, that already reflects
417431
// those events' changes. So we always stick with the version we have.
418-
for (int i = 0; i < messages.length; i++) {
419-
final message = messages[i];
420-
messages[i] = this.messages.putIfAbsent(message.id, () {
421-
return _stripMatchFields(message);
422-
});
423-
}
432+
// TODO(#1798) consider unsubscribed channels
433+
return current;
424434
}
425435

426436
Message _stripMatchFields(Message message) {

0 commit comments

Comments
 (0)