RxJava 复刻简版之一,调用流程分析之案例一

调用示例(io.reactivex.rxjava3:rxjava:3.1.5)

                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、解析部分1

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

  1. 和 create 一样,RxJavaPlugins.onSubscribe(this, observer) 又是直接返回结果
  2. 执行 subscribeActual(observer) 方法
  3. 先回调 onSubscribe(@NonNull Disposable d) ///1,再回调subscribe(@NonNull ObservableEmitter emitter) ///2
    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;
        }
    }

2、解析部分2

创建的 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);
            }
        }

3、仿写框架

根据以上流程,模仿一个

你可能感兴趣的:(RxJava,分析,rxjava,android)