Skip to content

Commit cece587

Browse files
committed
msglist [nfc]: Pull out a _setStatus method
This tightens up a bit the logic for maintaining the fetching status, and hopefully makes it a bit easier to read.
1 parent cb832ad commit cece587

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

lib/model/message_list.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,20 @@ class MessageListView with ChangeNotifier, _MessageSequence {
493493
}
494494
}
495495

496+
void _setStatus(FetchingStatus value, {FetchingStatus? was}) {
497+
assert(was == null || _status == was);
498+
_status = value;
499+
if (!fetched) return;
500+
notifyListeners();
501+
}
502+
496503
/// Fetch messages, starting from scratch.
497504
Future<void> fetchInitial() async {
498505
// TODO(#80): fetch from anchor firstUnread, instead of newest
499506
// TODO(#82): fetch from a given message ID as anchor
500507
assert(!fetched && !haveOldest && !busyFetchingMore);
501508
assert(messages.isEmpty && contents.isEmpty);
502-
assert(_status == FetchingStatus.unstarted);
503-
_status = FetchingStatus.fetchInitial;
509+
_setStatus(FetchingStatus.fetchInitial, was: FetchingStatus.unstarted);
504510
// TODO schedule all this in another isolate
505511
final generation = this.generation;
506512
final result = await getMessages(store.connection,
@@ -524,10 +530,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
524530
_addMessage(message);
525531
// Now [middleMessage] is the last message (the one just added).
526532
}
527-
assert(_status == FetchingStatus.fetchInitial);
528-
_status = FetchingStatus.idle;
529533
_haveOldest = result.foundOldest;
530-
notifyListeners();
534+
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchInitial);
531535
}
532536

533537
/// Update [narrow] for the result of a "with" narrow (topic permalink) fetch.
@@ -578,9 +582,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
578582
// We only intend to send "with" in [fetchInitial]; see there.
579583
|| (narrow as TopicNarrow).with_ == null);
580584
assert(messages.isNotEmpty);
581-
assert(_status == FetchingStatus.idle);
582-
_status = FetchingStatus.fetchingMore;
583-
notifyListeners();
585+
_setStatus(FetchingStatus.fetchingMore, was: FetchingStatus.idle);
584586
final generation = this.generation;
585587
bool hasFetchError = false;
586588
try {
@@ -617,21 +619,17 @@ class MessageListView with ChangeNotifier, _MessageSequence {
617619
_haveOldest = result.foundOldest;
618620
} finally {
619621
if (this.generation == generation) {
620-
assert(_status == FetchingStatus.fetchingMore);
621622
if (hasFetchError) {
622-
_status = FetchingStatus.backoff;
623+
_setStatus(FetchingStatus.backoff, was: FetchingStatus.fetchingMore);
623624
unawaited((_fetchBackoffMachine ??= BackoffMachine())
624625
.wait().then((_) {
625626
if (this.generation != generation) return;
626-
assert(_status == FetchingStatus.backoff);
627-
_status = FetchingStatus.idle;
628-
notifyListeners();
627+
_setStatus(FetchingStatus.idle, was: FetchingStatus.backoff);
629628
}));
630629
} else {
631-
_status = FetchingStatus.idle;
630+
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchingMore);
632631
_fetchBackoffMachine = null;
633632
}
634-
notifyListeners();
635633
}
636634
}
637635
}

0 commit comments

Comments
 (0)