Rxjava已经出来很长一段时间了,虽说也用了很长一段时间,但是总感觉有点缺那么点意思,其实就是不是很懂这玩意的原理到底是什么。所以最近想从源码的角度去找出,我想要的东西。记录下我的成果(PS:只是个人笔记,如有错误,希望指正)。
OK,切入正题,我们一般想要用Rxjava写一个功能的时候,会配上很多操作符,我这里先只是简单的写一个subscribe的代码,很简短。
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
}
}).subscribe(new Consumer() {
@Override
public void accept(String string) throws Exception {
}
});
这就是一段Rxjava的订阅代码,那好,让我带你走进源码(哈哈,基于Rxjava2)。先从subscribe开始。
@SchedulerSupport(SchedulerSupport.NONE)
public final Disposable subscribe(Consumer super T> onNext) {
return subscribe(onNext, Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION, Functions.emptyConsumer());
}
@SchedulerSupport(SchedulerSupport.NONE)
public final Disposable subscribe(Consumer super T> onNext, Consumer super Throwable> onError,
Action onComplete, Consumer super Disposable> onSubscribe) {
ObjectHelper.requireNonNull(onNext, "onNext is null");
ObjectHelper.requireNonNull(onError, "onError is null");
ObjectHelper.requireNonNull(onComplete, "onComplete is null");
ObjectHelper.requireNonNull(onSubscribe, "onSubscribe is null");
LambdaObserver ls = new LambdaObserver(onNext, onError, onComplete, onSubscribe);
subscribe(ls);
return ls;
}
这段代码其实就创建了一个LambdaObserver对象(后面用到,需要留意),然后调用了subscribe方法,好,那我们继续来看subscribe方法。
@SchedulerSupport(SchedulerSupport.NONE)
@Override
public final void subscribe(Observer super T> observer) {
ObjectHelper.requireNonNull(observer, "observer is null");
try {
observer = RxJavaPlugins.onSubscribe(this, observer);
ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");
subscribeActual(observer);
} catch (NullPointerException e) { // NOPMD
throw e;
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
// can't call onError because no way to know if a Disposable has been set or not
// can't call onSubscribe because the call might have set a Subscription already
RxJavaPlugins.onError(e);
NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
npe.initCause(e);
throw npe;
}
}
我们重点看subscribeActual这个方法,看字面意思是真正订阅的地方。但是这个方法是定义在Observable这个类里面,是一个抽象方法,所以,我们需要找到它的子类实现,那子类在那里,还记得这段代码嘛?
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
}
})
这个就是一开始create的observable,我们看这个create方法。
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
ObjectHelper.requireNonNull(source, "source is null");
return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
}
这里其实就是创建了一个ObservableCreate类,显然这个类里面实现了subscribeActual方法,继续。
@Override
protected void subscribeActual(Observer super T> observer) {
CreateEmitter parent = new CreateEmitter(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
这里我们需要两个地方,一个是CreateEmitter这个类,还有就是source.subscribe(parent)。看这个订阅方法。这个source其实就是create方法里面传过来的参数ObservableOnSubscribe,然后就可以调用subscribe方法。
可以看到这里的subscribe方法会被调用。好,我们再看CreateEmitter这个类。
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
CreateEmitter(Observer super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
return;
}
if (!isDisposed()) {
observer.onNext(t);
}
}
@Override
public void onError(Throwable t) {
if (t == null) {
t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
if (!isDisposed()) {
try {
observer.onError(t);
} finally {
dispose();
}
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}
@Override
public void setDisposable(Disposable d) {
DisposableHelper.set(this, d);
}
@Override
public void setCancellable(Cancellable c) {
setDisposable(new CancellableDisposable(c));
}
@Override
public ObservableEmitter serialize() {
return new SerializedEmitter(this);
}
@Override
public void dispose() {
DisposableHelper.dispose(this);
}
@Override
public boolean isDisposed() {
return DisposableHelper.isDisposed(get());
}
}
我们在写的时候,需要调用onNext()方法。我们先看下这个方法。主要看一行代码就行了。
observer.onNext(t);
这里的observer是哪里来的呢,还记得上面我们提到过的LambdaObserver这个类吗,这里的observer就是它,好,那我们只要直接看这个类里面的onNext方法是怎么实现的就好了。
@Override
public void onNext(T t) {
if (!isDisposed()) {
try {
onNext.accept(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
onError(e);
}
}
}
这里其实就是调用了onNext.accept(t)这个方法,那这个onNext是什么呢。我们看这个类的构造方法,这里面对这个onNext进行了赋值。
public LambdaObserver(Consumer super T> onNext, Consumer super Throwable> onError,
Action onComplete,
Consumer super Disposable> onSubscribe) {
super();
this.onNext = onNext;
this.onError = onError;
this.onComplete = onComplete;
this.onSubscribe = onSubscribe;
}
其实就是我们传进来的Consumer,我们需要知道这个Consumer是怎么传进来的就好了。
LambdaObserver<T> ls = new LambdaObserver<T>(onNext, onError, onComplete, onSubscribe);
还记得这行代码嘛?这个就是subscribe方法里面的。
subscribe(new Consumer() {
@Override
public void accept(Integer integer) throws Exception {
}
});
ok,很显然嘛,这里的accept就被调用了嘛,那这个流程是不是就是 通了呢?哈哈~