Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Object> emitter) throws Throwable {
/// 2
ConstKt.myLog(TAG +"subscribe");
emitter.onNext(1);
emitter.onNext(2);
emitter.onComplete();
}
}).subscribe(new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
/// 1
ConstKt.myLog(TAG +"onSubscribe");
}
@Override
public void onNext(@NonNull Object o) {
/// 4
ConstKt.myLog(TAG +"onNext");
}
@Override
public void onError(@NonNull Throwable e) {
/// 4
ConstKt.myLog(TAG +"onError");
}
@Override
public void onComplete() {
/// 3
ConstKt.myLog(TAG +"onComplete");
}
});
输出结果:
rxJava1 onSubscribe
rxJava1 subscribe
rxJava1 onNext 1
rxJava1 onNext 2
rxJava1 onComplete
1、调用 Observable.create
public static <@NonNull T> Observable<T> create(@NonNull ObservableOnSubscribe<T> source) {
Objects.requireNonNull(source, "source is null");
return RxJavaPlugins.onAssembly(new ObservableCreate<>(source));
}
创建 ObservableCreate,方法 subscribeActual 中实现 ObservableOnSubscribe 接口的 subscribe
public final class ObservableCreate<T> extends Observable<T> {
final ObservableOnSubscribe<T> source;
public ObservableCreate(ObservableOnSubscribe<T> source) {
this.source = source;
}
@Override
protected void subscribeActual(Observer<? super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<>(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
执行 RxJavaPlugins.onAssembly,直接返回 source
public static <@NonNull T> Observable<T> onAssembly(@NonNull Observable<T> source) {
Function<? super Observable, ? extends Observable> f = onObservableAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
2、调用 subscribe
public final void subscribe(@NonNull Observer<? super T> observer) {
Objects.requireNonNull(observer, "observer is null");
try {
observer = RxJavaPlugins.onSubscribe(this, observer);
Objects.requireNonNull(observer, "The RxJavaPlugins.onSubscribe hook returned a null Observer. Please change the handler provided to RxJavaPlugins.setOnObservableSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins");
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;
}
}
创建的 CreateEmitter 回调给 subscribe,也就是 emitter.onNext/onComplete/onError
@Override
protected void subscribeActual(Observer<? super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<>(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
CreateEmitter 继承 ObservableEmitter ,ObservableEmitter 继承 Emitter
public interface Emitter<@NonNull T> {
/**
* Signal a normal value.
* @param value the value to signal, not {@code null}
*/
void onNext(@NonNull T value);
/**
* Signal a {@link Throwable} exception.
* @param error the {@code Throwable} to signal, not {@code null}
*/
void onError(@NonNull Throwable error);
/**
* Signal a completion.
*/
void onComplete();
}
因此,代码 emitter.onNext(1);emitter.onNext(2);emitter.onComplete(); 执行回调到 CreateEmitter,又回调返回给代码调用 public void onNext(@NonNull Object o) {/// 4 ConstKt.myLog(TAG +“onNext”); }
CreateEmitter(Observer<? super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
if (t == null) {
onError(ExceptionHelper.createNullPointerException("onNext called with a null value."));
return;
}
if (!isDisposed()) {
observer.onNext(t);
}
}
根据以上流程,模仿一个