arrow-kt / arrow-fx

Λrrow Fx is part of Λrrow, a functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
64 stars 15 forks source link

LiveDataK wrapper for arrow-effects-android #26

Open JorgeCastilloPrz opened 5 years ago

JorgeCastilloPrz commented 5 years ago

Let's start working on arrow-effects-android adding a LiveData wrapper (LiveDataK). Create the arrow module and add the wrapper along with the required type class instances and tests for it.

This is in the same line of other wrappers we already have for other effects integrations like the ones for RxJava (SingleK, ObservableK, FlowableK), or the ones from Reactor (MonoK, FluxK).

Arrow basically has some "effects" modules that provide arrow integration for third party library data types. The approach is giving those data types like Observable the "higher kind" capabilities.

To do that, we wrap the original data types and extend the wrapper from Kind<F, A>, being F the type of the data type. (as in ObservableK : Kind<ForObservableK, A>.

Then we encode the methods (combinators) we believe it should support mostly thinking about type class behaviors that they'll be able to provide. (map, flatMap, etc). Then we need to provide instances for those given supported type classes targeting the data type we're working on.

JorgeCastilloPrz commented 5 years ago

@Tylos will take this one. It's a good one for an Android dev to start getting in contact with the key arrow concepts (data types, typeclasses, instances of the type classes for the data type, testing laws over the data type).

raulraja commented 5 years ago

I think this would require Stream<F, A> to be properly modeled and if we want to include the different lifecycle methods in a similar fashion as to how it's done with Rx2 traditionally in Android. The difference is that our Stream works for Deferred, Observable, IO, etc.

JorgeCastilloPrz commented 5 years ago

@raulraja I think LiveData does not model anything in terms of lifecycle. It's essentially an Observable that just queries the observer before emitting (the observer must implement LifecycleOwner or something similar), like observer.isResumed(), observer.isStarted()and so on. So I'm not sure it requires modeling anything related to the lifecycle as a stream. We need to investigate a bit more.

pakoito commented 5 years ago

https://github.com/arrow-kt/arrow/pull/475

I did it already. tl;dr it's so slow it takes literal minutes to do tailrec. Unusable.

JorgeCastilloPrz commented 5 years ago

Can you ellaborate on how to proceed then? Is it a problem on how LiveData.switchMap is coded? can't we do any workarounds to have this wrapper in place?

pakoito commented 5 years ago

It's coded so it does excessive checking and they don't optimize composition. It's all intentional so not much we can do about it other than ask Google. I already did, and their response was this this constructs are not meant to be chained a lot, just once or twice.

The way to proceed is to make something better, even if it's less widespread, for the people who do care about performance.

JorgeCastilloPrz commented 5 years ago

Ok so, given that this new data type we potentially work on would be "equivalent" to LiveData in terms of public APIs, but efficient, I assume we would want it to work with Google's LifecycleOwner observers?. SDK activities and fragments are already LifecycleOwners so if we can work with that interface we'd not need to reencode a lot of things neither create our own Activity / Fragment parent classes (which is potentially problematic because of the classic enforced inheritance problems).

pakoito commented 5 years ago

We can transform LifecycleOwner to a Stream and race it against any operation chain, that's the future implementation. It's the same as using LifecycleOwner and takeUntil() in Rx that I spoke about a couple of years ago, it never went away, they just renamed the APIs and made them imperative :D

JorgeCastilloPrz commented 5 years ago

I see. Sounds good. Is android-ui blocked by arrow-streams then? Looking for ways to put some efforts on this for Q1, but not sure it's gonna be possible.

pakoito commented 5 years ago

@Cotel can fill you in better, it's his project!