Skip to content

Commit b1f97c6

Browse files
gnpricechrisbobbe
authored andcommitted
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 3be9377 commit b1f97c6

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
@@ -64,6 +64,23 @@ class MessageListMessageItem extends MessageListMessageBaseItem {
6464
});
6565
}
6666

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

101117
/// Whether we know we have the oldest messages for this narrow.
102118
///
@@ -113,8 +129,7 @@ mixin _MessageSequence {
113129
/// the same response each time.
114130
///
115131
/// See also [fetchOlderCoolingDown].
116-
bool get fetchingOlder => _fetchingOlder;
117-
bool _fetchingOlder = false;
132+
bool get fetchingOlder => _status == FetchingStatus.fetchOlder;
118133

119134
/// Whether [fetchOlder] had a request error recently.
120135
///
@@ -127,8 +142,9 @@ mixin _MessageSequence {
127142
/// when a [fetchOlder] request succeeds.
128143
///
129144
/// 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;
132148

133149
BackoffMachine? _fetchOlderCooldownBackoffMachine;
134150

@@ -303,10 +319,8 @@ mixin _MessageSequence {
303319
generation += 1;
304320
messages.clear();
305321
middleMessage = 0;
306-
_fetched = false;
307322
_haveOldest = false;
308-
_fetchingOlder = false;
309-
_fetchOlderCoolingDown = false;
323+
_status = FetchingStatus.unfetched;
310324
_fetchOlderCooldownBackoffMachine = null;
311325
contents.clear();
312326
items.clear();
@@ -520,6 +534,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
520534
// TODO(#82): fetch from a given message ID as anchor
521535
assert(!fetched && !haveOldest && !fetchingOlder && !fetchOlderCoolingDown);
522536
assert(messages.isEmpty && contents.isEmpty);
537+
assert(_status == FetchingStatus.unfetched);
523538
// TODO schedule all this in another isolate
524539
final generation = this.generation;
525540
final result = await getMessages(store.connection,
@@ -543,7 +558,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
543558
_addMessage(message);
544559
// Now [middleMessage] is the last message (the one just added).
545560
}
546-
_fetched = true;
561+
assert(_status == FetchingStatus.unfetched);
562+
_status = FetchingStatus.idle;
547563
_haveOldest = result.foundOldest;
548564
notifyListeners();
549565
}
@@ -590,7 +606,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
590606
// We only intend to send "with" in [fetchInitial]; see there.
591607
|| (narrow as TopicNarrow).with_ == null);
592608
assert(messages.isNotEmpty);
593-
_fetchingOlder = true;
609+
assert(_status == FetchingStatus.idle);
610+
_status = FetchingStatus.fetchOlder;
594611
notifyListeners();
595612
final generation = this.generation;
596613
bool hasFetchError = false;
@@ -628,17 +645,18 @@ class MessageListView with ChangeNotifier, _MessageSequence {
628645
_haveOldest = result.foundOldest;
629646
} finally {
630647
if (this.generation == generation) {
631-
_fetchingOlder = false;
648+
assert(_status == FetchingStatus.fetchOlder);
632649
if (hasFetchError) {
633-
assert(!fetchOlderCoolingDown);
634-
_fetchOlderCoolingDown = true;
650+
_status = FetchingStatus.fetchOlderCoolingDown;
635651
unawaited((_fetchOlderCooldownBackoffMachine ??= BackoffMachine())
636652
.wait().then((_) {
637653
if (this.generation != generation) return;
638-
_fetchOlderCoolingDown = false;
654+
assert(_status == FetchingStatus.fetchOlderCoolingDown);
655+
_status = FetchingStatus.idle;
639656
notifyListeners();
640657
}));
641658
} else {
659+
_status = FetchingStatus.idle;
642660
_fetchOlderCooldownBackoffMachine = null;
643661
}
644662
notifyListeners();

0 commit comments

Comments
 (0)