Skip to content

Commit 6ff889b

Browse files
gnpricechrisbobbe
authored andcommitted
msglist [nfc]: Introduce haveNewest in model, always true for now
1 parent 7558042 commit 6ff889b

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

lib/model/message_list.dart

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mixin _MessageSequence {
9494
///
9595
/// This may or may not represent all the message history that
9696
/// conceptually belongs in this message list.
97-
/// That information is expressed in [fetched] and [haveOldest].
97+
/// That information is expressed in [fetched], [haveOldest], [haveNewest].
9898
///
9999
/// See also [middleMessage], an index which divides this list
100100
/// into a top slice and a bottom slice.
@@ -121,11 +121,19 @@ mixin _MessageSequence {
121121

122122
/// Whether we know we have the oldest messages for this narrow.
123123
///
124-
/// (Currently we always have the newest messages for the narrow,
125-
/// once [fetched] is true, because we start from the newest.)
124+
/// See also [haveNewest].
126125
bool get haveOldest => _haveOldest;
127126
bool _haveOldest = false;
128127

128+
/// Whether we know we have the newest messages for this narrow.
129+
///
130+
/// (Currently this is always true once [fetched] is true,
131+
/// because we start from the newest.)
132+
///
133+
/// See also [haveOldest].
134+
bool get haveNewest => _haveNewest;
135+
bool _haveNewest = false;
136+
129137
/// Whether this message list is currently busy when it comes to
130138
/// fetching more messages.
131139
///
@@ -158,7 +166,7 @@ mixin _MessageSequence {
158166
/// before, between, or after the messages.
159167
///
160168
/// This information is completely derived from [messages] and
161-
/// the flags [haveOldest] and [busyFetchingMore].
169+
/// the flags [haveOldest], [haveNewest], and [busyFetchingMore].
162170
/// It exists as an optimization, to memoize that computation.
163171
///
164172
/// See also [middleItem], an index which divides this list
@@ -315,6 +323,7 @@ mixin _MessageSequence {
315323
messages.clear();
316324
middleMessage = 0;
317325
_haveOldest = false;
326+
_haveNewest = false;
318327
_status = FetchingStatus.unstarted;
319328
_fetchBackoffMachine = null;
320329
contents.clear();
@@ -534,7 +543,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
534543
Future<void> fetchInitial() async {
535544
// TODO(#80): fetch from anchor firstUnread, instead of newest
536545
// TODO(#82): fetch from a given message ID as anchor
537-
assert(!fetched && !haveOldest && !busyFetchingMore);
546+
assert(!fetched && !haveOldest && !haveNewest && !busyFetchingMore);
538547
assert(messages.isEmpty && contents.isEmpty);
539548
_setStatus(FetchingStatus.fetchInitial, was: FetchingStatus.unstarted);
540549
// TODO schedule all this in another isolate
@@ -561,6 +570,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
561570
// Now [middleMessage] is the last message (the one just added).
562571
}
563572
_haveOldest = result.foundOldest;
573+
_haveNewest = true; // TODO(#82)
564574
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchInitial);
565575
}
566576

@@ -715,8 +725,16 @@ class MessageListView with ChangeNotifier, _MessageSequence {
715725
if (!narrow.containsMessage(message) || !_messageVisible(message)) {
716726
return;
717727
}
718-
if (!fetched) {
719-
// TODO mitigate this fetch/event race: save message to add to list later
728+
if (!haveNewest) {
729+
// This message list's [messages] doesn't yet reach the new end
730+
// of the narrow's message history. (Either [fetchInitial] hasn't yet
731+
// completed, or if it has then it was in the middle of history and no
732+
// subsequent fetch has reached the end.)
733+
// So this still-newer message doesn't belong.
734+
// Leave it to be found by a subsequent fetch when appropriate.
735+
// TODO mitigate this fetch/event race: save message to add to list later,
736+
// in case the fetch that reaches the end is already ongoing and
737+
// didn't include this message.
720738
return;
721739
}
722740
// TODO insert in middle instead, when appropriate

test/model/message_list_test.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ void main() {
150150
checkNotifiedOnce();
151151
check(model)
152152
..messages.length.equals(kMessageListFetchBatchSize)
153-
..haveOldest.isFalse();
153+
..haveOldest.isFalse()
154+
..haveNewest.isTrue();
154155
checkLastRequest(
155156
narrow: narrow.apiEncode(),
156157
anchor: 'newest',
@@ -180,7 +181,8 @@ void main() {
180181
checkNotifiedOnce();
181182
check(model)
182183
..messages.length.equals(30)
183-
..haveOldest.isTrue();
184+
..haveOldest.isTrue()
185+
..haveNewest.isTrue();
184186
});
185187

186188
test('no messages found', () async {
@@ -194,7 +196,8 @@ void main() {
194196
check(model)
195197
..fetched.isTrue()
196198
..messages.isEmpty()
197-
..haveOldest.isTrue();
199+
..haveOldest.isTrue()
200+
..haveNewest.isTrue();
198201
});
199202

200203
// TODO(#824): move this test
@@ -417,6 +420,10 @@ void main() {
417420
check(model).messages.length.equals(30);
418421
});
419422

423+
test('while in mid-history', () async {
424+
}, skip: true, // TODO(#82): not yet possible to exercise this case
425+
);
426+
420427
test('before fetch', () async {
421428
final stream = eg.stream();
422429
await prepare(narrow: ChannelNarrow(stream.streamId));
@@ -2139,9 +2146,10 @@ void checkInvariants(MessageListView model) {
21392146
check(model)
21402147
..messages.isEmpty()
21412148
..haveOldest.isFalse()
2149+
..haveNewest.isFalse()
21422150
..busyFetchingMore.isFalse();
21432151
}
2144-
if (model.haveOldest) {
2152+
if (model.haveOldest && model.haveNewest) {
21452153
check(model).busyFetchingMore.isFalse();
21462154
}
21472155

@@ -2286,5 +2294,6 @@ extension MessageListViewChecks on Subject<MessageListView> {
22862294
Subject<int> get middleItem => has((x) => x.middleItem, 'middleItem');
22872295
Subject<bool> get fetched => has((x) => x.fetched, 'fetched');
22882296
Subject<bool> get haveOldest => has((x) => x.haveOldest, 'haveOldest');
2297+
Subject<bool> get haveNewest => has((x) => x.haveNewest, 'haveNewest');
22892298
Subject<bool> get busyFetchingMore => has((x) => x.busyFetchingMore, 'busyFetchingMore');
22902299
}

0 commit comments

Comments
 (0)