Skip to content

Commit 8295a0a

Browse files
committed
msglist [nfc]: Use an enum for fetched/fetching/backoff state
This makes the relationships between these flags clearer. It will also simplify some upcoming refactors that change their semantics.
1 parent 86e2512 commit 8295a0a

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

lib/model/message_list.dart

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ class MessageListMessageItem extends MessageListMessageBaseItem {
6363
});
6464
}
6565

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+
6683
/// The sequence of messages in a message list, and how to display them.
6784
///
6885
/// This comprises much of the guts of [MessageListView].
@@ -94,8 +111,7 @@ mixin _MessageSequence {
94111
///
95112
/// This allows the UI to distinguish "still working on fetching messages"
96113
/// from "there are in fact no messages here".
97-
bool get fetched => _fetched;
98-
bool _fetched = false;
114+
bool get fetched => _status != FetchingStatus.unfetched;
99115

100116
/// Whether we know we have the oldest messages for this narrow.
101117
///
@@ -112,8 +128,7 @@ mixin _MessageSequence {
112128
/// the same response each time.
113129
///
114130
/// See also [fetchOlderCoolingDown].
115-
bool get fetchingOlder => _fetchingOlder;
116-
bool _fetchingOlder = false;
131+
bool get fetchingOlder => _status == FetchingStatus.fetchOlder;
117132

118133
/// Whether [fetchOlder] had a request error recently.
119134
///
@@ -126,8 +141,9 @@ mixin _MessageSequence {
126141
/// when a [fetchOlder] request succeeds.
127142
///
128143
/// 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;
131147

132148
BackoffMachine? _fetchOlderCooldownBackoffMachine;
133149

@@ -302,10 +318,8 @@ mixin _MessageSequence {
302318
generation += 1;
303319
messages.clear();
304320
middleMessage = 0;
305-
_fetched = false;
306321
_haveOldest = false;
307-
_fetchingOlder = false;
308-
_fetchOlderCoolingDown = false;
322+
_status = FetchingStatus.unfetched;
309323
_fetchOlderCooldownBackoffMachine = null;
310324
contents.clear();
311325
items.clear();
@@ -490,6 +504,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
490504
// TODO(#82): fetch from a given message ID as anchor
491505
assert(!fetched && !haveOldest && !fetchingOlder && !fetchOlderCoolingDown);
492506
assert(messages.isEmpty && contents.isEmpty);
507+
assert(_status == FetchingStatus.unfetched);
493508
// TODO schedule all this in another isolate
494509
final generation = this.generation;
495510
final result = await getMessages(store.connection,
@@ -513,7 +528,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
513528
_addMessage(message);
514529
// Now [middleMessage] is the last message (the one just added).
515530
}
516-
_fetched = true;
531+
assert(_status == FetchingStatus.unfetched);
532+
_status = FetchingStatus.idle;
517533
_haveOldest = result.foundOldest;
518534
notifyListeners();
519535
}
@@ -560,7 +576,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
560576
// We only intend to send "with" in [fetchInitial]; see there.
561577
|| (narrow as TopicNarrow).with_ == null);
562578
assert(messages.isNotEmpty);
563-
_fetchingOlder = true;
579+
assert(_status == FetchingStatus.idle);
580+
_status = FetchingStatus.fetchOlder;
564581
notifyListeners();
565582
final generation = this.generation;
566583
bool hasFetchError = false;
@@ -598,17 +615,18 @@ class MessageListView with ChangeNotifier, _MessageSequence {
598615
_haveOldest = result.foundOldest;
599616
} finally {
600617
if (this.generation == generation) {
601-
_fetchingOlder = false;
618+
assert(_status == FetchingStatus.fetchOlder);
602619
if (hasFetchError) {
603-
assert(!fetchOlderCoolingDown);
604-
_fetchOlderCoolingDown = true;
620+
_status = FetchingStatus.fetchOlderCoolingDown;
605621
unawaited((_fetchOlderCooldownBackoffMachine ??= BackoffMachine())
606622
.wait().then((_) {
607623
if (this.generation != generation) return;
608-
_fetchOlderCoolingDown = false;
624+
assert(_status == FetchingStatus.fetchOlderCoolingDown);
625+
_status = FetchingStatus.idle;
609626
notifyListeners();
610627
}));
611628
} else {
629+
_status = FetchingStatus.idle;
612630
_fetchOlderCooldownBackoffMachine = null;
613631
}
614632
notifyListeners();

0 commit comments

Comments
 (0)