Skip to content

Commit

Permalink
store [nfc]: Pull _maybeReportToUserTransientError out of poll method
Browse files Browse the repository at this point in the history
This method has gotten awfully long and tangly.  Here's a first step
on giving it some more organized structure.
  • Loading branch information
gnprice committed Dec 23, 2024
1 parent afbd44b commit d52dedd
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1008,28 +1008,10 @@ class UpdateMachine {
}
static BackoffMachine? __unexpectedErrorBackoffMachine;

/// This controls when we start to report transient errors to the user when
/// polling.
///
/// At the 6th failure, the expected time elapsed since the first failure
/// will be 1.55 seocnds.
static const transientFailureCountNotifyThreshold = 5;

void poll() async {
assert(!_disposed);
try {
BackoffMachine? backoffMachine;
int accumulatedTransientFailureCount = 0;

/// This only reports transient errors after reaching
/// a pre-defined threshold of retries.
void maybeReportToUserTransientError(Object error) {
accumulatedTransientFailureCount++;
if (accumulatedTransientFailureCount > transientFailureCountNotifyThreshold) {
_reportToUserErrorConnectingToServer(error);
}
}

while (true) {
if (_debugLoopSignal != null) {
await _debugLoopSignal!.future;
Expand Down Expand Up @@ -1086,7 +1068,7 @@ class UpdateMachine {
assert(debugLog('Transient error polling event queue for $store: $e\n'
'Backing off, then will retry…'));
if (shouldReportToUser) {
maybeReportToUserTransientError(e);
_maybeReportToUserTransientError(e);
}
await (backoffMachine ??= BackoffMachine()).wait();
if (_disposed) return;
Expand Down Expand Up @@ -1114,7 +1096,7 @@ class UpdateMachine {
store.isLoading = false;
// Dismiss existing errors, if any.
reportErrorToUserBriefly(null);
accumulatedTransientFailureCount = 0;
_accumulatedTransientFailureCount = 0;

final events = result.events;
for (final event in events) {
Expand Down Expand Up @@ -1191,6 +1173,24 @@ class UpdateMachine {
}
}

/// This controls when we start to report transient errors to the user when
/// polling.
///
/// At the 6th failure, the expected time elapsed since the first failure
/// will be 1.55 seocnds.
static const transientFailureCountNotifyThreshold = 5;

int _accumulatedTransientFailureCount = 0;

/// This only reports transient errors after reaching
/// a pre-defined threshold of retries.
void _maybeReportToUserTransientError(Object error) {
_accumulatedTransientFailureCount++;
if (_accumulatedTransientFailureCount > transientFailureCountNotifyThreshold) {
_reportToUserErrorConnectingToServer(error);
}
}

void _reportToUserErrorConnectingToServer(Object error) {
final localizations = GlobalLocalizations.zulipLocalizations;
reportErrorToUserBriefly(
Expand Down

0 comments on commit d52dedd

Please sign in to comment.