Airstream icon indicating copy to clipboard operation
Airstream copied to clipboard

Add support for Either[Throwable, *]

Open ajaychandran opened this issue 3 years ago • 4 comments

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.

ajaychandran avatar Apr 06 '21 16:04 ajaychandran

This would require adding the kind-projector plugin though.

ajaychandran avatar Apr 10 '21 14:04 ajaychandran

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[_].

raquo avatar May 02 '21 03:05 raquo

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).

ajaychandran avatar May 02 '21 04:05 ajaychandran

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 .

raquo avatar May 02 '21 06:05 raquo

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.

raquo avatar Dec 05 '23 08:12 raquo