RxJava源码分析-数据流向

2018-05-21  本文已影响0人  一只胖Wa牛

RxJava源码分析-数据流向

Observable.create(ObservableOnSubscribe<String> {
            it.onNext("hello,world")
            it.onComplete()
        }).subscribe(object : Observer<String> {

            var mDisposable: Disposable? = null
            override fun onComplete() {
                Log.d("Observer", "onComplete")
            }

            override fun onSubscribe(d: Disposable) {
                mDisposable = d
                Log.d("Observer", "onSubscribe")
            }

            override fun onNext(t: String) {
                Log.d("Observer", "onNext:-- $t")
            }

            override fun onError(e: Throwable) {
                Log.d("Observer", "onError:--")
            }

        })

我们来看logcat的输出

05-21 09:40:09.230 25346-25346/com.example.pandaguo.rxdemo D/Observer: onSubscribe
    onNext:-- hello,world
    onComplete

先后打印了,也就说下游的Observer代码执行顺序是先执行了onSubscribe,然后才执行了onNextonComplete
接下来我们就来跟进代码看下整个流程是如何执行的

Observable.create

我们的示例代码中,创建了一个ObservableOnSubscribe对象并将其作为参数传入到了Observable的静态方法create中,我们就看下整个方法做了什么

public abstract class Observable<T> implements ObservableSource<T> {
    ...
    public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
        //null校验
        ObjectHelper.requireNonNull(source, "source is null");
        return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
    }
    ...
}

可以看到这里就是简单的做了一个非空检验,然后就执行了RxJavaPlugins的onAssembly方法,并new了一个ObservableCreate对象作为参数传入,继续跟进

RxJavaPlugins.onAssembly

public final class RxJavaPlugins {
    ...
    static volatile Function<? super Observable, ? extends Observable> onObservableAssembly;
    ...
    public static <T> Observable<T> onAssembly(@NonNull Observable<T> source) {
        Function<? super Observable, ? extends Observable> f = onObservableAssembly;
        if (f != null) {
            return apply(f, source);
        }
        return source;
    }
    ...
}

首次执行RxJavaPlugins的onAssembly方法时,此时onObservableAssembly还未被初始化,所以就直接返回了传入的source,也就是前一步我们直接创建的ObservableCreate对象,所以可以这么说Observable.create()最终我们得到的就是一个ObservableCreate对象,我们来看下ObservableCreate的实现

ObservableCreate

public final class ObservableCreate<T> extends Observable<T> {
    final ObservableOnSubscribe<T> source;

    public ObservableCreate(ObservableOnSubscribe<T> source) {
        this.source = source;
    }
    ...
}

Observable.subscrib

public abstract class Observable<T> implements ObservableSource<T> {
    ...
    try {
        ...
        subscribeActual(observer);
    } catch (NullPointerException e) {
        ...
    } catch (Throwable e) {
        ...
    }
    ...
}

略去不必要的代码,实际上这个方法最终调用到了subscribeActual方法,在Observable中subscribeActual方法是个抽象方法,因此它的具体实现就是在ObservableCreate对象中,我们来看下ObservableCreate中都做了什么

ObservableCreate.subscribeActual

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);
        try {
            source.subscribe(parent);
        } catch (Throwable ex) {
            ...
        }
    }
    ...
}
public interface ObservableOnSubscribe<T> {
    void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception;
}
    it.onNext("hello,world")
    it.onComplete()

这里的it实际上就是传入的CreateEmitter,那么CreateEmitter是怎么把数据传给Observer呢?

public final class ObservableCreate<T> extends Observable<T> {
    ...
    static final class CreateEmitter<T>
    extends AtomicReference<Disposable>
    implements ObservableEmitter<T>, 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 (!tryOnError(t)) {
                RxJavaPlugins.onError(t);
            }
        }

        @Override
        public boolean tryOnError(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();
                }
                return true;
            }
            return false;
        }

        @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<T> serialize() {
            return new SerializedEmitter<T>(this);
        }

        @Override
        public void dispose() {
            DisposableHelper.dispose(this);
        }

        @Override
        public boolean isDisposed() {
            return DisposableHelper.isDisposed(get());
        }
    }
    ...
}
上一篇下一篇

猜你喜欢

热点阅读