@@ -493,14 +493,20 @@ class MessageListView with ChangeNotifier, _MessageSequence {
493
493
}
494
494
}
495
495
496
+ void _setStatus (FetchingStatus value, {FetchingStatus ? was}) {
497
+ assert (was == null || _status == was);
498
+ _status = value;
499
+ if (! fetched) return ;
500
+ notifyListeners ();
501
+ }
502
+
496
503
/// Fetch messages, starting from scratch.
497
504
Future <void > fetchInitial () async {
498
505
// TODO(#80): fetch from anchor firstUnread, instead of newest
499
506
// TODO(#82): fetch from a given message ID as anchor
500
507
assert (! fetched && ! haveOldest && ! busyFetchingMore);
501
508
assert (messages.isEmpty && contents.isEmpty);
502
- assert (_status == FetchingStatus .unstarted);
503
- _status = FetchingStatus .fetchInitial;
509
+ _setStatus (FetchingStatus .fetchInitial, was: FetchingStatus .unstarted);
504
510
// TODO schedule all this in another isolate
505
511
final generation = this .generation;
506
512
final result = await getMessages (store.connection,
@@ -524,10 +530,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
524
530
_addMessage (message);
525
531
// Now [middleMessage] is the last message (the one just added).
526
532
}
527
- assert (_status == FetchingStatus .fetchInitial);
528
- _status = FetchingStatus .idle;
529
533
_haveOldest = result.foundOldest;
530
- notifyListeners ( );
534
+ _setStatus ( FetchingStatus .idle, was : FetchingStatus .fetchInitial );
531
535
}
532
536
533
537
/// Update [narrow] for the result of a "with" narrow (topic permalink) fetch.
@@ -578,9 +582,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
578
582
// We only intend to send "with" in [fetchInitial]; see there.
579
583
|| (narrow as TopicNarrow ).with_ == null );
580
584
assert (messages.isNotEmpty);
581
- assert (_status == FetchingStatus .idle);
582
- _status = FetchingStatus .fetchingMore;
583
- notifyListeners ();
585
+ _setStatus (FetchingStatus .fetchingMore, was: FetchingStatus .idle);
584
586
final generation = this .generation;
585
587
bool hasFetchError = false ;
586
588
try {
@@ -617,21 +619,17 @@ class MessageListView with ChangeNotifier, _MessageSequence {
617
619
_haveOldest = result.foundOldest;
618
620
} finally {
619
621
if (this .generation == generation) {
620
- assert (_status == FetchingStatus .fetchingMore);
621
622
if (hasFetchError) {
622
- _status = FetchingStatus .backoff ;
623
+ _setStatus ( FetchingStatus .backoff, was : FetchingStatus .fetchingMore) ;
623
624
unawaited ((_fetchBackoffMachine ?? = BackoffMachine ())
624
625
.wait ().then ((_) {
625
626
if (this .generation != generation) return ;
626
- assert (_status == FetchingStatus .backoff);
627
- _status = FetchingStatus .idle;
628
- notifyListeners ();
627
+ _setStatus (FetchingStatus .idle, was: FetchingStatus .backoff);
629
628
}));
630
629
} else {
631
- _status = FetchingStatus .idle ;
630
+ _setStatus ( FetchingStatus .idle, was : FetchingStatus .fetchingMore) ;
632
631
_fetchBackoffMachine = null ;
633
632
}
634
- notifyListeners ();
635
633
}
636
634
}
637
635
}
0 commit comments