fixed stalled requests after app resume
This commit is contained in:
@@ -23,6 +23,9 @@ class ChatBloc
|
||||
final ChatListBloc? _chatListBloc;
|
||||
|
||||
String? _pollingToken;
|
||||
// A loop parked in its request survives stop+start for the same token
|
||||
// (pause/resume); the generation retires it so only one loop polls.
|
||||
int _pollGeneration = 0;
|
||||
int _backoffMs = 0;
|
||||
int _lastKnownMessageId = 0;
|
||||
bool _appResumed = true;
|
||||
@@ -265,23 +268,25 @@ class ChatBloc
|
||||
_pollingToken = token;
|
||||
_backoffMs = 0;
|
||||
_lastKnownMessageId = _maxMessageId(innerState?.chatResponse);
|
||||
unawaited(_pollLoop(token));
|
||||
unawaited(_pollLoop(token, ++_pollGeneration));
|
||||
}
|
||||
|
||||
void _stopLongPoll() {
|
||||
_pollingToken = null;
|
||||
_pollGeneration++;
|
||||
_backoffMs = 0;
|
||||
}
|
||||
|
||||
Future<void> _pollLoop(String token) async {
|
||||
while (_pollingToken == token && !isClosed) {
|
||||
Future<void> _pollLoop(String token, int generation) async {
|
||||
bool active() => generation == _pollGeneration && !isClosed;
|
||||
while (active()) {
|
||||
try {
|
||||
final response = await LongPollChat(
|
||||
chatToken: token,
|
||||
lastKnownMessageId: _lastKnownMessageId,
|
||||
).run();
|
||||
|
||||
if (_pollingToken != token || isClosed) return;
|
||||
if (!active()) return;
|
||||
_backoffMs = 0;
|
||||
|
||||
if (response == null) continue;
|
||||
@@ -305,7 +310,7 @@ class ChatBloc
|
||||
_chatListBloc?.markRoomAsRead(token, _lastKnownMessageId);
|
||||
}
|
||||
} on Object catch (e) {
|
||||
if (_pollingToken != token || isClosed) return;
|
||||
if (!active()) return;
|
||||
log('LongPoll error for $token: $e');
|
||||
_backoffMs = _backoffMs == 0 ? 2000 : math.min(_backoffMs * 2, 30000);
|
||||
await Future.delayed(Duration(milliseconds: _backoffMs));
|
||||
|
||||
Reference in New Issue
Block a user