Skip to content

Commit 7558042

Browse files
gnpricechrisbobbe
authored andcommitted
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 b75f168 commit 7558042

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
@@ -523,14 +523,20 @@ class MessageListView with ChangeNotifier, _MessageSequence {
523523
}
524524
}
525525

526+
void _setStatus(FetchingStatus value, {FetchingStatus? was}) {
527+
assert(was == null || _status == was);
528+
_status = value;
529+
if (!fetched) return;
530+
notifyListeners();
531+
}
532+
526533
/// Fetch messages, starting from scratch.
527534
Future<void> fetchInitial() async {
528535
// TODO(#80): fetch from anchor firstUnread, instead of newest
529536
// TODO(#82): fetch from a given message ID as anchor
530537
assert(!fetched && !haveOldest && !busyFetchingMore);
531538
assert(messages.isEmpty && contents.isEmpty);
532-
assert(_status == FetchingStatus.unstarted);
533-
_status = FetchingStatus.fetchInitial;
539+
_setStatus(FetchingStatus.fetchInitial, was: FetchingStatus.unstarted);
534540
// TODO schedule all this in another isolate
535541
final generation = this.generation;
536542
final result = await getMessages(store.connection,
@@ -554,10 +560,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
554560
_addMessage(message);
555561
// Now [middleMessage] is the last message (the one just added).
556562
}
557-
assert(_status == FetchingStatus.fetchInitial);
558-
_status = FetchingStatus.idle;
559563
_haveOldest = result.foundOldest;
560-
notifyListeners();
564+
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchInitial);
561565
}
562566

563567
/// Update [narrow] for the result of a "with" narrow (topic permalink) fetch.
@@ -608,9 +612,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
608612
// We only intend to send "with" in [fetchInitial]; see there.
609613
|| (narrow as TopicNarrow).with_ == null);
610614
assert(messages.isNotEmpty);
611-
assert(_status == FetchingStatus.idle);
612-
_status = FetchingStatus.fetchingMore;
613-
notifyListeners();
615+
_setStatus(FetchingStatus.fetchingMore, was: FetchingStatus.idle);
614616
final generation = this.generation;
615617
bool hasFetchError = false;
616618
try {
@@ -647,21 +649,17 @@ class MessageListView with ChangeNotifier, _MessageSequence {
647649
_haveOldest = result.foundOldest;
648650
} finally {
649651
if (this.generation == generation) {
650-
assert(_status == FetchingStatus.fetchingMore);
651652
if (hasFetchError) {
652-
_status = FetchingStatus.backoff;
653+
_setStatus(FetchingStatus.backoff, was: FetchingStatus.fetchingMore);
653654
unawaited((_fetchBackoffMachine ??= BackoffMachine())
654655
.wait().then((_) {
655656
if (this.generation != generation) return;
656-
assert(_status == FetchingStatus.backoff);
657-
_status = FetchingStatus.idle;
658-
notifyListeners();
657+
_setStatus(FetchingStatus.idle, was: FetchingStatus.backoff);
659658
}));
660659
} else {
661-
_status = FetchingStatus.idle;
660+
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchingMore);
662661
_fetchBackoffMachine = null;
663662
}
664-
notifyListeners();
665663
}
666664
}
667665
}

0 commit comments

Comments
 (0)