From 2157185aee95efcac4a7b4723f95d65fce80db4d Mon Sep 17 00:00:00 2001 From: lulupointu Date: Sat, 20 Jul 2024 20:47:14 +0200 Subject: [PATCH] fix(fresh): wait for initial storage read before returning token (#96) Co-authored-by: Felix Angelov --- packages/fresh/lib/src/fresh.dart | 4 +++- packages/fresh/test/fresh_test.dart | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/fresh/lib/src/fresh.dart b/packages/fresh/lib/src/fresh.dart index 43a8a58..fe5b8fa 100644 --- a/packages/fresh/lib/src/fresh.dart +++ b/packages/fresh/lib/src/fresh.dart @@ -107,7 +107,9 @@ mixin FreshMixin { /// Returns the current token. Future get token async { if (_authenticationStatus != AuthenticationStatus.initial) return _token; - await authenticationStatus.first; + await authenticationStatus.firstWhere( + (status) => status != AuthenticationStatus.initial, + ); return _token; } diff --git a/packages/fresh/test/fresh_test.dart b/packages/fresh/test/fresh_test.dart index bfb7fe7..106550c 100644 --- a/packages/fresh/test/fresh_test.dart +++ b/packages/fresh/test/fresh_test.dart @@ -67,6 +67,17 @@ void main() { final token = await freshController.token; expect(token, mockToken); }); + + test('waits for storage read to complete', () async { + final mockToken = MockToken(); + when(() => tokenStorage.read()).thenAnswer((_) async { + await Future.delayed(Duration.zero); + return mockToken; + }); + final freshController = FreshController(tokenStorage); + final token = await freshController.token; + expect(token, mockToken); + }); }); group('revokeToken', () {