@@ -63,6 +63,23 @@ class MessageListMessageItem extends MessageListMessageBaseItem {
63
63
});
64
64
}
65
65
66
+ /// The status of outstanding or recent fetch requests from a [MessageListView] .
67
+ enum FetchingStatus {
68
+ /// The model hasn't successfully completed a `fetchInitial` request
69
+ /// (since its last reset, if any).
70
+ unfetched,
71
+
72
+ /// The model made a successful `fetchInitial` request,
73
+ /// and has no outstanding requests or backoff.
74
+ idle,
75
+
76
+ /// The model has an active `fetchOlder` request.
77
+ fetchOlder,
78
+
79
+ /// The model is in a backoff period from a failed `fetchOlder` request.
80
+ fetchOlderCoolingDown,
81
+ }
82
+
66
83
/// The sequence of messages in a message list, and how to display them.
67
84
///
68
85
/// This comprises much of the guts of [MessageListView] .
@@ -94,8 +111,7 @@ mixin _MessageSequence {
94
111
///
95
112
/// This allows the UI to distinguish "still working on fetching messages"
96
113
/// from "there are in fact no messages here".
97
- bool get fetched => _fetched;
98
- bool _fetched = false ;
114
+ bool get fetched => _status != FetchingStatus .unfetched;
99
115
100
116
/// Whether we know we have the oldest messages for this narrow.
101
117
///
@@ -112,8 +128,7 @@ mixin _MessageSequence {
112
128
/// the same response each time.
113
129
///
114
130
/// See also [fetchOlderCoolingDown] .
115
- bool get fetchingOlder => _fetchingOlder;
116
- bool _fetchingOlder = false ;
131
+ bool get fetchingOlder => _status == FetchingStatus .fetchOlder;
117
132
118
133
/// Whether [fetchOlder] had a request error recently.
119
134
///
@@ -126,8 +141,9 @@ mixin _MessageSequence {
126
141
/// when a [fetchOlder] request succeeds.
127
142
///
128
143
/// See also [fetchingOlder] .
129
- bool get fetchOlderCoolingDown => _fetchOlderCoolingDown;
130
- bool _fetchOlderCoolingDown = false ;
144
+ bool get fetchOlderCoolingDown => _status == FetchingStatus .fetchOlderCoolingDown;
145
+
146
+ FetchingStatus _status = FetchingStatus .unfetched;
131
147
132
148
BackoffMachine ? _fetchOlderCooldownBackoffMachine;
133
149
@@ -302,10 +318,8 @@ mixin _MessageSequence {
302
318
generation += 1 ;
303
319
messages.clear ();
304
320
middleMessage = 0 ;
305
- _fetched = false ;
306
321
_haveOldest = false ;
307
- _fetchingOlder = false ;
308
- _fetchOlderCoolingDown = false ;
322
+ _status = FetchingStatus .unfetched;
309
323
_fetchOlderCooldownBackoffMachine = null ;
310
324
contents.clear ();
311
325
items.clear ();
@@ -490,6 +504,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
490
504
// TODO(#82): fetch from a given message ID as anchor
491
505
assert (! fetched && ! haveOldest && ! fetchingOlder && ! fetchOlderCoolingDown);
492
506
assert (messages.isEmpty && contents.isEmpty);
507
+ assert (_status == FetchingStatus .unfetched);
493
508
// TODO schedule all this in another isolate
494
509
final generation = this .generation;
495
510
final result = await getMessages (store.connection,
@@ -513,7 +528,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
513
528
_addMessage (message);
514
529
// Now [middleMessage] is the last message (the one just added).
515
530
}
516
- _fetched = true ;
531
+ assert (_status == FetchingStatus .unfetched);
532
+ _status = FetchingStatus .idle;
517
533
_haveOldest = result.foundOldest;
518
534
notifyListeners ();
519
535
}
@@ -560,7 +576,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
560
576
// We only intend to send "with" in [fetchInitial]; see there.
561
577
|| (narrow as TopicNarrow ).with_ == null );
562
578
assert (messages.isNotEmpty);
563
- _fetchingOlder = true ;
579
+ assert (_status == FetchingStatus .idle);
580
+ _status = FetchingStatus .fetchOlder;
564
581
notifyListeners ();
565
582
final generation = this .generation;
566
583
bool hasFetchError = false ;
@@ -598,17 +615,18 @@ class MessageListView with ChangeNotifier, _MessageSequence {
598
615
_haveOldest = result.foundOldest;
599
616
} finally {
600
617
if (this .generation == generation) {
601
- _fetchingOlder = false ;
618
+ assert (_status == FetchingStatus .fetchOlder) ;
602
619
if (hasFetchError) {
603
- assert (! fetchOlderCoolingDown);
604
- _fetchOlderCoolingDown = true ;
620
+ _status = FetchingStatus .fetchOlderCoolingDown;
605
621
unawaited ((_fetchOlderCooldownBackoffMachine ?? = BackoffMachine ())
606
622
.wait ().then ((_) {
607
623
if (this .generation != generation) return ;
608
- _fetchOlderCoolingDown = false ;
624
+ assert (_status == FetchingStatus .fetchOlderCoolingDown);
625
+ _status = FetchingStatus .idle;
609
626
notifyListeners ();
610
627
}));
611
628
} else {
629
+ _status = FetchingStatus .idle;
612
630
_fetchOlderCooldownBackoffMachine = null ;
613
631
}
614
632
notifyListeners ();
0 commit comments