Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind observable to collection of observers #163

Closed
sgtsquiggs opened this issue Jul 8, 2018 · 6 comments
Closed

Bind observable to collection of observers #163

sgtsquiggs opened this issue Jul 8, 2018 · 6 comments

Comments

@sgtsquiggs
Copy link

Name and description

An extension on ObservableType to allow .bind(to:) with a collection of observers, and an extension on Collection (where Iterator.Element == Disposable) to allow .disposed(by:) with a collection of Disposables.

Motivation for inclusion

Useful when you have a single observable binding to a lot of observers. A big use case is when implementing forms and having a single observable bind to isEnabled on a multitude of controls.

Example of use

isEnabledSubject
  .bind(to: myCollectionOfTextFields.map({$0.rx.isEnabled}))
  .disposed(by: disposeBag)

Implementation here: https://gist.github.com/sgtsquiggs/0b1dc9dbd6fe51f107d4082ac7c448cd

@freak4pc
Copy link
Member

freak4pc commented Jul 9, 2018

Uhm this is actually very interesting :)
Let me ponder on this a bit - I think there are a few issues with this, as it requires creating collections of ObserverType(s)/Relays to bind into, which might be a bit niche but useful when creating these sorts of validations (e.g. many things are affected by a single value).

Still could be possibly useful. Any feedback from @RxSwiftCommunity/contributors would be super-helpful here.

The overload of disposing many Disposable(s) together might also be useful in general - But might be better to use CompositeDisposable for that, as it has proper locking etc?

@jegnux
Copy link
Member

jegnux commented Jul 9, 2018

I agree with @freak4pc
It's a an interesting addition, but we should indeed use CompositeDisposable instead. It even simplifies the implementation to only that:

extension ObservableType {
    func bind<O>(to observers: [O]) -> Disposable where O : ObserverType, Self.E == O.E {
        let shared = self.share()
        let disposables = observers.map(shared.bind(to:))
        return CompositeDisposable(disposables: disposables)
    }
}

@valerianb
Copy link

There was recently a PR on RxSwift to take care of disposables in one call:
ReactiveX/RxSwift#1676

@sgtsquiggs
Copy link
Author

Oh I wasn't aware of CompositeDisposable. I'll update my gist!

@freak4pc
Copy link
Member

freak4pc commented Jul 15, 2018

@valerianb that is also a cool PR (the .bag one) but doesn't solve the same thing exactly IMO.

You would still need to repeat the binding (a.bind(to: b), a.bind(to:c)), instead of a.bind(to: [b, c])). So when thinking about this the last few days I think this could be a great addition if you'd like to build it @sgtsquiggs and push a PR so I can review, with tests, etc.

@M0rtyMerr
Copy link
Member

Closing, cause issue was resolved with RxSwift 5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants