fix(auth): add sort of mutex on onAuthChanges callback trigger

This commit is contained in:
Hugo Pointcheval 2022-11-16 17:16:41 -05:00
parent b033b97917
commit 3d7af98ac9
Signed by: hugo
GPG Key ID: A9E8E9615379254F

View File

@ -44,6 +44,8 @@ class AuthenticationRepositoryImpl<T extends Object>
final OnAuthChange<T>? _onAccountChanges;
bool _pause = false; // Semaphore
AuthenticationRepositoryImpl({
required AuthenticationCacheDataSource<T> authenticationCacheDataSource,
required AuthenticationRemoteDataSource authenticationRemoteDataSource,
@ -120,6 +122,7 @@ class AuthenticationRepositoryImpl<T extends Object>
}) =>
Result.tryCatchAsync<Account, AppException, AppException>(
() async {
_pause = true;
final account = await _authenticationRemoteDataSource.signUp(
email: email,
password: password,
@ -135,9 +138,13 @@ class AuthenticationRepositoryImpl<T extends Object>
(error) async => error,
);
}
_pause = false;
return account;
},
(error) => error,
(error) {
_pause = false;
return error;
},
);
@override
@ -200,7 +207,7 @@ class AuthenticationRepositoryImpl<T extends Object>
@override
Stream<FutureResult<AccountWrapper<T>>> streamAccount() =>
_authenticationRemoteDataSource.streamAccount().map((account) async {
if (_onAccountChanges.isNotNull) {
if (_onAccountChanges.isNotNull && !_pause) {
final dataResult = await _onAccountChanges!.call(account);
return dataResult.map((data) => AccountWrapperModel(account, data));
}