Skip to content

Commit

Permalink
fix(fresh): wait for initial storage read before returning token (#96)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Angelov <[email protected]>
  • Loading branch information
lulupointu and felangel authored Jul 20, 2024
1 parent 8f11c4b commit 2157185
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/fresh/lib/src/fresh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ mixin FreshMixin<T> {
/// Returns the current token.
Future<T?> get token async {
if (_authenticationStatus != AuthenticationStatus.initial) return _token;
await authenticationStatus.first;
await authenticationStatus.firstWhere(
(status) => status != AuthenticationStatus.initial,
);
return _token;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/fresh/test/fresh_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>.delayed(Duration.zero);
return mockToken;
});
final freshController = FreshController<OAuth2Token>(tokenStorage);
final token = await freshController.token;
expect(token, mockToken);
});
});

group('revokeToken', () {
Expand Down

0 comments on commit 2157185

Please sign in to comment.