fix: The initial sync waiting for a long time in some cases

This commit is contained in:
Nicolas Werner 2022-08-30 14:07:11 +02:00
parent 1aa645b31c
commit 163ad0de6b
2 changed files with 12 additions and 6 deletions

View File

@ -1439,7 +1439,8 @@ class Client extends MatrixApi {
'Successfully connected as ${userID.localpart} with ${homeserver.toString()}',
);
final syncFuture = _sync();
/// Timeout of 0, so that we don't see a spinner for 30 seconds.
final syncFuture = _sync(timeout: Duration.zero);
if (waitForFirstSync) {
await syncFuture;
}
@ -1499,8 +1500,11 @@ class Client extends MatrixApi {
return _sync();
}
Future<void> _sync() {
final currentSync = _currentSync ??= _innerSync().whenComplete(() {
/// Pass a timeout to set how long the server waits before sending an empty response.
/// (Corresponds to the timeout param on the /sync request.)
Future<void> _sync({Duration? timeout}) {
final currentSync =
_currentSync ??= _innerSync(timeout: timeout).whenComplete(() {
_currentSync = null;
if (_backgroundSync && isLogged() && !_disposed) {
_sync();
@ -1522,7 +1526,9 @@ class Client extends MatrixApi {
return;
}
Future<void> _innerSync() async {
/// Pass a timeout to set how long the server waits before sending an empty response.
/// (Corresponds to the timeout param on the /sync request.)
Future<void> _innerSync({Duration? timeout}) async {
await _retryDelay;
_retryDelay = Future.delayed(Duration(seconds: syncErrorTimeoutSec));
if (!isLogged() || _disposed || _aborted) return;
@ -1536,7 +1542,7 @@ class Client extends MatrixApi {
final syncRequest = sync(
filter: syncFilterId,
since: prevBatch,
timeout: prevBatch != null ? 30000 : null,
timeout: timeout?.inMilliseconds ?? 30000,
setPresence: syncPresence,
).then((v) => Future<SyncUpdate?>.value(v)).catchError((e) {
syncError = e;

View File

@ -1850,7 +1850,7 @@ class FakeMatrixApi extends BaseClient {
},
'/client/v3/sync?filter=%7B%22room%22%3A%7B%22include_leave%22%3Atrue%2C%22timeline%22%3A%7B%22limit%22%3A10%7D%7D%7D&timeout=0':
(var req) => archiveSyncResponse,
'/client/v3/sync?filter=1234': (var req) => syncResponse,
'/client/v3/sync?filter=1234&timeout=0': (var req) => syncResponse,
'/client/v3/sync?filter=1234&since=1234&full_state=false&set_presence=unavailable&timeout=15':
(var req) => syncResponse,
'/client/v3/register/available?username=testuser': (var req) =>