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