RxSwiftCommunity / RxSwiftExt

A collection of Rx operators & tools not found in the core RxSwift distribution
MIT License
1.32k stars 213 forks source link

Sweeten binds between observers and observables #244

Closed sebastianvarela closed 4 years ago

sebastianvarela commented 4 years ago

Name and description

After several years working with ReactiveSwift now we have a long-term project that use RxSwift. The first thing I miss is the binding operator for observables/observer.

When someone want to bind an observer to an observable have to do something like this:

observable
    .bind(to: observer)
    .disposed(by: disposeBag)

I made an extension that create two operators to link observer-observable-disposebag:

func += <T: DisposableContainer>(container: T, disposable: Disposable) -> T
func <~ <Destination: ObserverType, Source: ObservableConvertibleType>(observer: Destination, observable: Source) -> Disposable where Source.Element == Destination.Element
func ~> <Destination: ObserverType, Source: ObservableConvertibleType>(observable: Source, observer: Destination) -> Disposable where Source.Element == Destination.Element

Motivation for inclusion

When you use MVVM, for example, and you have a lot of binding inside a func, this operator improve the legibility of the code by removing all the boilerplate.

Example of use

disposeBag += observer ~> observable

or

disposeBag += observable <~ observer

You can also use a closure or a func to just observe an observable by using:

disposeBag += observable ~> { value in
    // Do something with value
}

or

func updateValue(value: Any) {
    // Do something with value
}
disposeBag += observable ~> updateValue
M0rtyMerr commented 4 years ago

I am strongly against this proposal. It doesn’t bring any value, just allows to implement things in another way. It’s obviously just a wrapper around existing methods. They will be confusing for a new user. They are not express “drive/bind/emit” behavior of different traits. This “sweetness” is subjective. I don’t think this these operators are more readable. As I know RxSwift already had similar operators long time ago, but they were removed. Operators should not be added just because of personal preferences or because they are defined in ReactiveSwift. It’s a separate project. You can declare these operators locally, but I don’t see any pros in contributing them to community. Even in Ext. Sincerely

Sent with GitHawk

jdisho commented 4 years ago

I see the point and I agree with you @MortyMerr. However, in my opinion, the way you wrote this message seems harsh and unwelcoming. Even though I didn't make this proposal, I felt uncomfortable reading your answer. 😅😅

M0rtyMerr commented 4 years ago

@jdisho lol 😅 My message is just about arguments 😀 What do you want me to do with it? 🤗Add an emoji after each sentence? 😊

jdisho commented 4 years ago

I think it would be great to show some kindness and appreciation, at least for the time invested to make a proposal (as an issue or pull-request). 😊

Sent with GitHawk

freak4pc commented 4 years ago

Leaving the sentiments and appreciation for the code aside, I also agree that whatever value this PR would bring is not universal enough to share in this repo (personal opinion of course).

Custom operators are a touchy subject, too many people are entirely against it, and they're very easy to make. So I think people who are for this usually just add them, so it doesn't justify adding here regardless of the time put into making the suggestion.

jdisho commented 4 years ago

I suppose we can close this issue.