@@ -64,6 +64,23 @@ class MessageListMessageItem extends MessageListMessageBaseItem {
64
64
});
65
65
}
66
66
67
+ /// The status of outstanding or recent fetch requests from a [MessageListView] .
68
+ enum FetchingStatus {
69
+ /// The model hasn't successfully completed a `fetchInitial` request
70
+ /// (since its last reset, if any).
71
+ unfetched,
72
+
73
+ /// The model made a successful `fetchInitial` request,
74
+ /// and has no outstanding requests or backoff.
75
+ idle,
76
+
77
+ /// The model has an active `fetchOlder` request.
78
+ fetchOlder,
79
+
80
+ /// The model is in a backoff period from a failed `fetchOlder` request.
81
+ fetchOlderCoolingDown,
82
+ }
83
+
67
84
/// The sequence of messages in a message list, and how to display them.
68
85
///
69
86
/// This comprises much of the guts of [MessageListView] .
@@ -95,8 +112,7 @@ mixin _MessageSequence {
95
112
///
96
113
/// This allows the UI to distinguish "still working on fetching messages"
97
114
/// from "there are in fact no messages here".
98
- bool get fetched => _fetched;
99
- bool _fetched = false ;
115
+ bool get fetched => _status != FetchingStatus .unfetched;
100
116
101
117
/// Whether we know we have the oldest messages for this narrow.
102
118
///
@@ -113,8 +129,7 @@ mixin _MessageSequence {
113
129
/// the same response each time.
114
130
///
115
131
/// See also [fetchOlderCoolingDown] .
116
- bool get fetchingOlder => _fetchingOlder;
117
- bool _fetchingOlder = false ;
132
+ bool get fetchingOlder => _status == FetchingStatus .fetchOlder;
118
133
119
134
/// Whether [fetchOlder] had a request error recently.
120
135
///
@@ -127,8 +142,9 @@ mixin _MessageSequence {
127
142
/// when a [fetchOlder] request succeeds.
128
143
///
129
144
/// See also [fetchingOlder] .
130
- bool get fetchOlderCoolingDown => _fetchOlderCoolingDown;
131
- bool _fetchOlderCoolingDown = false ;
145
+ bool get fetchOlderCoolingDown => _status == FetchingStatus .fetchOlderCoolingDown;
146
+
147
+ FetchingStatus _status = FetchingStatus .unfetched;
132
148
133
149
BackoffMachine ? _fetchOlderCooldownBackoffMachine;
134
150
@@ -303,10 +319,8 @@ mixin _MessageSequence {
303
319
generation += 1 ;
304
320
messages.clear ();
305
321
middleMessage = 0 ;
306
- _fetched = false ;
307
322
_haveOldest = false ;
308
- _fetchingOlder = false ;
309
- _fetchOlderCoolingDown = false ;
323
+ _status = FetchingStatus .unfetched;
310
324
_fetchOlderCooldownBackoffMachine = null ;
311
325
contents.clear ();
312
326
items.clear ();
@@ -520,6 +534,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
520
534
// TODO(#82): fetch from a given message ID as anchor
521
535
assert (! fetched && ! haveOldest && ! fetchingOlder && ! fetchOlderCoolingDown);
522
536
assert (messages.isEmpty && contents.isEmpty);
537
+ assert (_status == FetchingStatus .unfetched);
523
538
// TODO schedule all this in another isolate
524
539
final generation = this .generation;
525
540
final result = await getMessages (store.connection,
@@ -543,7 +558,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
543
558
_addMessage (message);
544
559
// Now [middleMessage] is the last message (the one just added).
545
560
}
546
- _fetched = true ;
561
+ assert (_status == FetchingStatus .unfetched);
562
+ _status = FetchingStatus .idle;
547
563
_haveOldest = result.foundOldest;
548
564
notifyListeners ();
549
565
}
@@ -590,7 +606,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
590
606
// We only intend to send "with" in [fetchInitial]; see there.
591
607
|| (narrow as TopicNarrow ).with_ == null );
592
608
assert (messages.isNotEmpty);
593
- _fetchingOlder = true ;
609
+ assert (_status == FetchingStatus .idle);
610
+ _status = FetchingStatus .fetchOlder;
594
611
notifyListeners ();
595
612
final generation = this .generation;
596
613
bool hasFetchError = false ;
@@ -628,17 +645,18 @@ class MessageListView with ChangeNotifier, _MessageSequence {
628
645
_haveOldest = result.foundOldest;
629
646
} finally {
630
647
if (this .generation == generation) {
631
- _fetchingOlder = false ;
648
+ assert (_status == FetchingStatus .fetchOlder) ;
632
649
if (hasFetchError) {
633
- assert (! fetchOlderCoolingDown);
634
- _fetchOlderCoolingDown = true ;
650
+ _status = FetchingStatus .fetchOlderCoolingDown;
635
651
unawaited ((_fetchOlderCooldownBackoffMachine ?? = BackoffMachine ())
636
652
.wait ().then ((_) {
637
653
if (this .generation != generation) return ;
638
- _fetchOlderCoolingDown = false ;
654
+ assert (_status == FetchingStatus .fetchOlderCoolingDown);
655
+ _status = FetchingStatus .idle;
639
656
notifyListeners ();
640
657
}));
641
658
} else {
659
+ _status = FetchingStatus .idle;
642
660
_fetchOlderCooldownBackoffMachine = null ;
643
661
}
644
662
notifyListeners ();
0 commit comments