diff --git a/lib/src/client.dart b/lib/src/client.dart index d05ca23b..0ada71b7 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -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 _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 _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 _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 _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.value(v)).catchError((e) { syncError = e; diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 285782e0..a4ddf78a 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -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) =>