基于RxJava 2.1.12,记录RxJava的常用操作符及相关类。
public interface ObservableEmitter<T> extends Emitter<T>
public interface ObservableOnSubscribe {
void subscribe(@NonNull ObservableEmitter emitter) throws Exception;
}
public final class ObservableCreate<T> extends Observable<T>
@Override
protected void subscribeActual(Observer super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<T>(observer);
observer.onSubscribe(parent);
//...
source.subscribe(parent); //source是create操作符接受的ObservableOnSubscribe实现类,命令模式被调用的地方
//...
}
public final void subscribe(Observer super T> observer) {
//...
subscribeActual(observer); //Observable的抽象方法,由Observable操作符的实现类实现此方法
//....
}
public final class ObservableSubscribeOn<T> extends AbstractObservableWithUpstream<T, T>
@Override
public void subscribeActual(final Observer super T> s) {
final SubscribeOnObserver parent = new SubscribeOnObserver(s);
s.onSubscribe(parent);
parent.setDisposable(scheduler.scheduleDirect(new SubscribeTask(parent)));
}
static final class SubscribeOnObserver<T> extends AtomicReference<Disposable> implements Observer<T>, Disposable
public final class ObservableObserveOn<T> extends AbstractObservableWithUpstream<T, T>
@Override
protected void subscribeActual(Observer super T> observer) {
if (scheduler instanceof TrampolineScheduler) {
source.subscribe(observer);
} else {
Scheduler.Worker w = scheduler.createWorker();
source.subscribe(new ObserveOnObserver(observer, w, delayError, bufferSize));
}
}
static final class ObserveOnObserver<T> extends BasicIntQueueDisposable<T>
implements Observer<T>, Runnable
@Override
public void onNext(T t) {
//...
schedule();
}
void schedule() {
if (getAndIncrement() == 0) {
worker.schedule(this);
}
}
public final Observable map(Function super T, ? extends R> mapper)
public final class ObservableMap<T, U> extends AbstractObservableWithUpstream<T, U>
@Override
public void subscribeActual(Observer super U> t) {
source.subscribe(new MapObserver(t, function));
}
static final class MapObserver<T, U> extends BasicFuseableObserver<T, U>
@Override
public void onNext(T t) {
//...
U v;
v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
//...
actual.onNext(v);
}