-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Allow binding to multiple observers #1702
Conversation
Generated by 🚫 Danger |
I'm in for variadic parameters and if the benchmarks prove that the overload impact isn't significant I guess it's fine :) |
Hi @freak4pc , I think that adding these extensions to generic observer is dangerous. The name of this thing is This unfortunately can't cover all of the cases properly and creates a lot of confusion. E.g. If you use If you I think it might sense to have this for It might make sense to hold these kinds of changes from single to variadic until next major version since it is public interface breakage. |
If that is the case, there should be a test case to cover it IMO - My impression was this is fine since the entire test suite passed. Anyways I think binding to many has the underlying meaning of using a shared resource. I don't mind having an array-only variation, without variadic, and I think that works well since it fits both scenarios. Regarding stateful, yeah you're right this would probably need a |
The fact that test case passes isn't the evidence that the code works, it only means that there is the lack of evidence that it doesn't work :) Ok, I've checked out the code again. If you change the implementation to be public func bind<O: ObserverType>(to observers: O...) -> Disposable where O.E == E {
return self.subscribe { event in
observers.map { $0(event) }
}
} and ditch the array version then you don't have any multiple subscription issues, we don't add any additional methods and just improve the existing one. It still breaks the public interface, so if we want to pull this in, just to be safe, maybe we should wait until RxSwift 5.0. |
You mean |
@kzaher Made the changes required, let me know if makes sense for you. I had to add array overloads, since Arrays don't support splatting yet (so it wouldn't be able to reuse the Optional vs. non-Optional uses). I made them private since you wanted to avoid Array-style bindings, but TBH I think it would be better to make them public and have both options open. LMK your thoughts on this. I also extended this to Relays, but I just noticed - could it be there are no tests for any kind of relays at this point? P.S Even though there is an interface change - in practice the end-user interface doesn't change, since they can still bind to a single |
I believe they are below your code. |
Oh, sorry, duh. Blacked out there :) |
@kzaher Added the tests, ready for you |
Push passed but PR didn't - not sure if related 🤔 |
|
||
func testBindToOptionalPublishRelay() { | ||
var events: [Recorded<Event<Int?>>] = [] | ||
|
||
let relay = PublishRelay<Int?>() | ||
|
||
_ = relay.subscribe{ event in | ||
_ = relay.subscribe { event in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not testing the variadic case, is there a need for this test and the non variadic ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As written below, in my though process, yes -
I believe our tests should always test that the existing interface and the old interface works (e.g. binding to a single one vs. binding to a variadic list).
I've addressed your feedback @kzaher. I believe we should test the single and variadic tests separately to ensure reliability and backward-compatibility. Let me know if you'd like this to be done otherwise. |
BTW lets merge #1924 first, would make it easier to rebase this. |
As expected this is conflicted. Taking care of this RN. |
Ok, I redid the work on top of the RxRelay work. This should be good to go as well. |
@kzaher CI is green whenever you're ready to give this another review. |
This is based on a PR in RxSwiftExt: RxSwiftCommunity/RxSwiftExt#166
As well as a discussion here: RxSwiftCommunity/RxSwiftExt#168
I wondered if you'd be open to adding something like this to the main repo as it seems useful, and would also save us from a few external overloads, possibly.
It's also possible having separate overloads for
bind(to: Observer)
,bind(to: Observer...)
,bind(to: [Observer])
but I don't think there would be a significant performance difference in that case. (edit: Running theBenchmarks
tests confirms that)If this makes sense to you, I'll add everything needed including tests etc. This maintains backward compatibility.