Closed khellang closed 4 years ago
:thumbsup: I'm not opposed to a well-documented extension method here to make this concept easier to understand.
For the sake of argument, is it worth considering that often the use of Publish().RefCount() is a code smell? And perhaps we shouldn't encourage its usage by making it too easy to use?
The issue is that many people overuse subjects to begin with, and it seems that the next step along their journey to better reactive programming is to replace their usage of subjects with Publish().RefCount()
, which is just as bad, if not worse.
Devs (especially those new to Rx) seem to use RefCount
without realizing that the side effects they are sharing may occur again, if the reference count drops down to 0 and then back to 1.
Furthermore, in many cases you can still use Publish
without RefCount
, if you really need to, by using the overload of Publish
that accepts a selector
function. It's still using a Subject
internally to get the job done, but at least it doesn't mess with the temperature of the query as a whole. Only the selector
function is hot. This also may encourage better reactive designs because it's more of a functional approach. Devs often use RefCount
as a result of breaking apart their queries into many types, and then gluing them back together again. I believe this is a bad approach to reactive programming. It's much more like OOP -- with events.
Anyway, if you're going to introduce a Share
operator, then perhaps it's imperative that you also offer a selector
overload, and encourage its use over the other overloads. Devs will probably continue to avoid using it because it does require a bit more mental gymnastics, and forces us out of a strictly OOP design, but at least they'll have the option.
Now that there is a timeout version of refCount
, at least there is some value in refCount
. Otherwise, I'm not convinced we should over-encourage the usage of it and I'd probably remove share()
from RxJava if it didn't break some important user's codebase.
Closing due to inactivity.
Doing
.Publish().RefCount()
is a pretty common combination.I see the ports to Java, Scala, Groovy, JS, Kotlin and PHP has this alias already; http://reactivex.io/documentation/operators/refcount.html
Is it worth adding it? I can send a PR you're interested.