From 5a79aa953673103ae78460f0cf501c166861d3fb Mon Sep 17 00:00:00 2001 From: Krille Date: Thu, 2 May 2024 15:43:26 +0200 Subject: [PATCH] refactor: make sure ensureNotSoftLoggedOut does not run multiple times --- lib/src/client.dart | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 2980bdfa..cbd36510 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1823,19 +1823,25 @@ class Client extends MatrixApi { return; } + Future? _handleSoftLogoutFuture; + Future _handleSoftLogout() async { final onSoftLogout = this.onSoftLogout; if (onSoftLogout == null) return; - onLoginStateChanged.add(LoginState.softLoggedOut); - try { - await onSoftLogout(this); - onLoginStateChanged.add(LoginState.loggedIn); - } catch (e, s) { - Logs().w('Unable to refresh session after soft logout', e, s); - await clear(); - rethrow; - } + _handleSoftLogoutFuture ??= () async { + onLoginStateChanged.add(LoginState.softLoggedOut); + try { + await onSoftLogout(this); + onLoginStateChanged.add(LoginState.loggedIn); + } catch (e, s) { + Logs().w('Unable to refresh session after soft logout', e, s); + await clear(); + rethrow; + } + }(); + await _handleSoftLogoutFuture; + _handleSoftLogoutFuture = null; } /// Checks if the token expires in under [expiresIn] time and calls the