Airstream
Airstream copied to clipboard
Add support for Either[Throwable, *]
This can be achieved with the following additions.
object EventStream {
def fromEither[A](ea: Either[Throwable, A]): EventStream[A] =
EventStream.fromCustomSource[A](_ == 1, (fa, fe, _, _) => ea.fold(fe, fa), _ => ())
}
implicit object SwitchEitherEventStream extends FlattenStrategy[Observable, Either[Throwable, *], EventStream] {
def flatten[A](parent: Observable[Either[Throwable, A]]): EventStream[A] =
new SwitchEventStream[Either[Throwable, A], A](parent, EventStream.fromEither)
}
This is particularly useful for decoding JSON using libraries like Circe.
This would require adding the kind-projector plugin though.
Would it really need a kind-projector plugin? Looks like type EitherThrowable[+A] = Either[Throwable, A]
will be enough.
Anyway... I don't want to overload the flatten
concept to include types that aren't observable-like (i.e. futures, promises, etc). Flattening in Airstream causes a transaction boundary, but there is no need for that in this case.
Either has a merge
method which is kinda sorta similar, but that concept is already used in EventStream.merge (which again, may cause a transaction boundary).
And, if we're adding this method we should probably also add a similar method for Try. We already have fromTry
factory, but no operator to map the Failure into observable error state.
The operator name we want here sounds something like mapToError
with implicit evidences for Either[Throwable, _]
and Try[_]
.
The operator name we want here sounds something like mapToError with implicit evidences for Either[Throwable, ] and Try[].
How about a specific operator for each datatype like mapEither
and mapTry
? This would be consistent with Airstream nomenclature (like contramapOpt
and contramapTry
in Observer
).
I was thinking mapTry[B] would be the best name for an operator that accepts an Try[A] => Try[B] project callback.
The error related operator(s) seem more like flatten to me. Aside from Either and Try, they should work on Observable[Throwable] and perhaps other custom result-like types too. In this regard it needs the flexibility of implicit evidences, similar to flatten.
--Nikita
On Sat., May 1, 2021, 9:44 p.m. Ajay Chandran, @.***> wrote:
The operator name we want here sounds something like mapToError with implicit evidences for Either[Throwable, ] and Try[].
How about a specific operator for each datatype like mapEither and mapTry? This would be consistent with Airstream nomenclature (like contramapOpt and contramapTry in Observer).
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/raquo/Airstream/issues/77#issuecomment-830739902, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAECBMFWIWZ7XXDEPDAMC53TLTKETANCNFSM42PBOXLA .
Airstream 17.0.0-M2 now has a few helpers for this, including EventStream.fromEither. As well as a bunch of mapSuccess / mapTry / mapFailure operators. Still need to document those. Flattening isn't designed for non-observable types, so that part is out.