-
Notifications
You must be signed in to change notification settings - Fork 46
Description
I'm just in the middle of a major refactor which involves moving a lot of my Dartz code into FPDart. While going through your articles I found how amazing the TaskEither.tryCatch()
is at cleaning up my code (😙👌) .
But now I've come to my Streams and i'm drawing a bit of a blank on how to tidy them up and wonder if this is something that FPDart has a solution for? I'm currently returning Stream<Either<AlertsFailure, List<Alert>>>>
in something like this:
Stream<Either<AlertsFailure, List<Alert>>> watchAlerts() async* {
final userOption = await _auth.getSignedInUser();
final user = userOption.getOrElse(() => throw NotAuthenticatedError());
final alertsRef = _firestore.allUserAlerts(user.uid.getOrCrash());
yield* alertsRef.snapshots().map((query) {
if (query.docs.isEmpty) {
return const Left<AlertsFailure, List<Alert>>(AlertsFailure.noAlerts());
}
return Right<AlertsFailure, List<Alert>>(query.docs
.map(
(alert) => AlertModel.fromFirestore(alert).toDomain(),
)
.toList()
..sort((a, b) => b.dateCreated.compareTo(a.dateCreated)));
}).onErrorReturnWith((e, stack) {
if (e is FirebaseException && e.code.contains('permission-denied')) {
return const Left<AlertsFailure, List<Alert>>(
AlertsFailure.insufficientPermission());
} else {
return const Left<AlertsFailure, List<Alert>>(
AlertsFailure.serverError());
}
});
}
i'm trying to imagine something like TaskEither to wrap the snapshot()
but don't know how to handle errors from the stream. The other idea was that there might be some kind of StreamEither but no searching has brought up any examples.
This could be my FP noobish-ness or maybe this is actually a feature request... or maybe this isn't something FP even deals with and then my Stream is the right solution?