tryInitialiseStream method
Initializes the authentication state stream and data synchronization.
Sets up listeners for user authentication changes and manages the data stream.
Implementation
Future<void> tryInitialiseStream() async {
_log.info('Initialising TurboAuthSyncService stream..');
try {
_userSubscription ??= FirebaseAuth.instance.userChanges().listen(
(user) async {
final userId = user?.uid;
if (userId != null) {
cachedUserId = userId;
await onAuth?.call(user!);
_subscription ??= (await stream(user!)).listen(
(value) async {
await onData(value, user);
},
onError: (error, stackTrace) {
_log.error(
'Stream error occurred inside of stream!',
error: error,
stackTrace: stackTrace,
);
// Convert error to TurboFirestoreException if needed
final exception = error is TurboFirestoreException
? error
: TurboFirestoreException.fromFirestoreException(
error,
stackTrace,
);
// Call onError handler
onError(exception);
_tryRetry();
},
onDone: () => onDone(_nrOfRetry, _maxNrOfRetry),
);
} else {
cachedUserId = null;
await _subscription?.cancel();
_subscription = null;
await onData(null, null);
}
},
);
} catch (error, stack) {
_log.error('Stream error occurred while setting up stream!',
error: error, stackTrace: stack);
// Convert error to TurboFirestoreException if needed
final exception = error is TurboFirestoreException
? error
: TurboFirestoreException.fromFirestoreException(
error,
stack,
);
// Call onError handler
onError(exception);
_tryRetry();
}
}