RxJava2 源码分析笔记(二)
RxJava2 源码分析笔记(三) 线程切换
先看一个最简单的调用
Observable.create(new ObservableOnSubscribe
打印日志 (可以看到日志中onError执行以后,onComplete并没有输出日志)
上面就是简单创建一个被观察者然后注册订阅者就可以收到回调。
Observable.create(被观察者).subscribe(观察者);
1 先看 Observable.create(被观察者)
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static Observable create(ObservableOnSubscribe source) {
ObjectHelper.requireNonNull(source, "source is null");
//省略其他代码,只留主要代码
return new ObservableCreate(source);
}
↓
↓
↓ 接着看 ObservableCreate
↓
↓
public final class ObservableCreate extends Observable {
final ObservableOnSubscribe source;
public ObservableCreate(ObservableOnSubscribe source) {
this.source = source;
}
@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);
}
}
//...后面先省略
//...
Observable.create到这里就结束了,我们得到了一个继承了了Observable的ObservableCreate 对象。实现了Observable中的抽象方法 subscribeActual 。
2 再来看.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");
//观察者对象被subscribe进来以后执行了这个方法
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(observer);发现
protected abstract void subscribeActual(Observer super T> observer);
咋是个抽象方法呢,因为具体实现是在Observable.create(被观察者)时实现的;那我们就回到上面Observable.create(被观察者)所得到的ObservableCreate对象中看看subscribeActual方法是如何实现的。
public final class ObservableCreate extends Observable {
final ObservableOnSubscribe source;
public ObservableCreate(ObservableOnSubscribe source) {
this.source = source;
}
@Override
protected void subscribeActual(Observer super T> observer) {
// 1 subscribe方法传进的观察者被传到了这里
// 2 Emitter是发射,发射器的意思,就是发射数据用的;并将subscribe方法传进的观察者
// 传到这个发射器里面
CreateEmitter parent = new CreateEmitter(observer);
// 3 此处就是执行subscribe方法中观察者的onSubscribe方法,所以会先打印这个日志
observer.onSubscribe(parent);
try {
// 4 这个source就是create的时候传进的被观察者,执行subscribe方法
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
上图中 4 source.subscribe(parent); 执行的其实就是
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(@NonNull ObservableEmitter emitter) throws
Exception {
System.out.println(" 被观察者 subscribe ");
emitter.onNext("onNext");
// onError 和 onComple 其中一个被调用就代表结束
// 另一个不会被调用,下面源码中会分析
emitter.onError(new NullPointerException());
emitter.onComplete();
}
}
而emitter接着就会执行 onNext....及后面的方法。emitter其实就是上上图中2
CreateEmitter parent = new CreateEmitter(observer);
点进去看一下
public final class ObservableCreate extends Observable {
final ObservableOnSubscribe source;
public ObservableCreate(ObservableOnSubscribe source) {
this.source = source;
}
@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 是ObservableCreate中的一个内部类
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
//接收到subscribe传进来的观察者赋值给全局observer
//后面就是使用它调用onNext,onError,onComplete方法
CreateEmitter(Observer super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
if (!isDisposed()) {
//这里执行其实就是调用subscribe绑定的观察者的onNext
observer.onNext(t);
}
}
@Override
public void onError(Throwable t) {
if (!tryOnError(t)) {
RxJavaPlugins.onError(t);
}
}
@Override
public boolean tryOnError(Throwable t) {
if (!isDisposed()) {
try {
//这里执行其实就是调用subscribe绑定的观察者的onError
observer.onError(t);
} finally {
dispose();
}
return true;
}
return false;
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
//这里执行其实就是调用subscribe绑定的观察者的onComplete
observer.onComplete();
} finally {
dispose();
}
}
}
//其他省略
}
这样一个完整回调就通了。
另外:
其中 onComplete和onError的执行以后都会执行一个finally里面的dispose()方法,而他们再执行之前进行判断if (!isDisposed()),
这样其中一个执行了以后就会将dispose的状态变掉,所以另外一个就不会执行。
这样onComplete和oneError只会执行其中一个,而不会都执行
@Override
public void onError(Throwable t) {
if (!tryOnError(t)) {
RxJavaPlugins.onError(t);
}
}
@Override
public boolean tryOnError(Throwable t) {
if (!isDisposed()) {
try {
observer.onError(t);
} finally {
dispose();
}
return true;
}
return false;
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}