-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Description
In the constructor of ObservableRefCount that takes ConnectableObservable<T> source as the argument, we set timeout to 0L. In that specific use case of ObservableRefCount, scheduler is never needed. It's only referenced in cancel() method but if timeout is 0, it won't be triggered at all because there is early return. I am wondering if we could avoid referencing Schedulers.trampoline() in that specific use case of ObservableRefCount to keep the dependency clear.
RxJava/src/main/java/io/reactivex/internal/operators/observable/ObservableRefCount.java
Lines 48 to 50 in 4a78cfc
| public ObservableRefCount(ConnectableObservable<T> source) { | |
| this(source, 1, 0L, TimeUnit.NANOSECONDS, Schedulers.trampoline()); | |
| } |
RxJava/src/main/java/io/reactivex/internal/operators/observable/ObservableRefCount.java
Lines 92 to 112 in 4a78cfc
| void cancel(RefConnection rc) { | |
| SequentialDisposable sd; | |
| synchronized (this) { | |
| if (connection == null || connection != rc) { | |
| return; | |
| } | |
| long c = rc.subscriberCount - 1; | |
| rc.subscriberCount = c; | |
| if (c != 0L || !rc.connected) { | |
| return; | |
| } | |
| if (timeout == 0L) { | |
| timeout(rc); | |
| return; | |
| } | |
| sd = new SequentialDisposable(); | |
| rc.timer = sd; | |
| } | |
| sd.replace(scheduler.scheduleDirect(rc, timeout, unit)); | |
| } |
The reasons are the following:
- In projects that don't depend on
Schedulersclass, if there is no reference toSchedulers, the wholeSchedulerscan be stripped out of the library after optimizations (e.g., proguard). With constructor that referencesSchedulers, the optimizer can't properly strip it out. In our quick test of our Android app, we were able to reduce the RxJava library size dependency from 51KB to 37KB (after optimization but before compression) by simply avoiding access toSchedulersinObservableRefCount. - In terms of modularity,
ObservableRefCountis just an operator so it by itself should probably not have dependency on what available pool of schedulers (Schedulers) there are. It should just know that there is someSchedulerthat could be passed toObservableRefCountwhenObservableRefCountneeds it.