RxJava2实例源码浅析(1)-just

从最简单的例子着手,一步步看RxJava内部是如何封装调用的

Flowable.just(1)
        .subscribe(new FlowableSubscriber<Integer>() {
            @Override
            public void onSubscribe(@NonNull Subscription subscription) {
                subscription.request(Long.MAX_VALUE);
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext"+integer);
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onComplete() {
            }
        });


1.第一句.just(1)


经过转换又返回一个Flowable对象,实现链式调用

@CheckReturnValue
@BackpressureSupport(BackpressureKind.FULL)
@SchedulerSupport("none")
public static <T> Flowable<T> just(T item) {
    ObjectHelper.requireNonNull(item, "item is null"); //非空检查,item为空就抛出空指针异常
    return RxJavaPlugins.onAssembly(new FlowableJust(item));
}


看第二句,FlowableJust是Flowable的子类,内部保存了item的值。这里将FlowableJust对象作为参数传入onAssembly方法


@NonNull
public static <T> Flowable<T> onAssembly(@NonNull Flowable<T> source) {
    Function f = onFlowableAssembly;
    return f != null?(Flowable)apply(f, source):source;
}

 

先看onFlowableAssembly这个是什么

static volatile Function<? super Flowable, ? extends Flowable> onFlowableAssembly;

public interface Function<T, R> {
    @NonNull
    R apply(@NonNull T var1) throws Exception;
}

 它是一个接口,内部一个方法apply,将入参T转换成R输出


onFlowableAssembly默认为空,通过setOnFlowableAssembly方法设置,因此这里是直接返回上面传入的FlowableJust对象。
Flowable.just(1)实际就是new FlowableJust(1)


2.FlowableSubscriber<Integer>观察者


new一个观察者,FlowableSubscriber是一个接口,它又继承Subscriber接口,实现4个方法onSubscribe、onNext、onError、onComplete


3.第三步subscribe订阅


.subscribe(subscriber) 传入观察者,开始订阅,进入该方法查看具体实现

@BackpressureSupport(BackpressureKind.SPECIAL)
@SchedulerSupport("none")
@Beta
public final void subscribe(FlowableSubscriber<? super T> s) {
    ObjectHelper.requireNonNull(s, "s is null"); //非空检查
    try {
        Subscriber e = RxJavaPlugins.onSubscribe(this, s);
        ObjectHelper.requireNonNull(e, "Plugin returned null Subscriber"); //再检查一次Plugin返回的Subscriber
        this.subscribeActual(e); //真正执行订阅
    } catch (NullPointerException var4) {
        throw var4;
    } catch (Throwable var5) {
        Exceptions.throwIfFatal(var5);
        RxJavaPlugins.onError(var5);
        NullPointerException npe = new NullPointerException("Actually not, but can\'t throw other exceptions due to RS");
        npe.initCause(var5);
        throw npe;
    }
}

看RxJavaPlugins.onSubscribe(this, s)方法

@NonNull
public static <T> Subscriber<? super T> onSubscribe(@NonNull Flowable<T> source, @NonNull Subscriber<? super T> subscriber) {
    BiFunction f = onFlowableSubscribe;
    return f != null?(Subscriber)apply(f, source, subscriber):subscriber;
}

这个方法和上面的onAssembly方法类似,这里的onFlowableSubscribe是一个BiFunction接口,与Function的区别是有两个入参。

public interface BiFunction<T1, T2, R> {
    @NonNull
    R apply(@NonNull T1 var1, @NonNull T2 var2) throws Exception;
}

onFlowableSubscribe默认null,因此onSubscribe也是原样返回传入的FlowableSubscriber

现在看subscribeActual方法,现在才真正开始订阅,Flowable中这个方法是抽象方法,需要找到它的子类即上面发现的FlowableJust

protected void subscribeActual(Subscriber<? super T> s) {
     s.onSubscribe(new ScalarSubscription(s, this.value));
 }
这个方法先创建了ScalarSubscription对象将subscriber本身和之前FlowableJust创建时保存的值都包装进去,接着subscriber调用onSubscribe方法,将scalarSubscription传入,即到了开头例子中第四行那个回调方法:

@Override
public void onSubscribe(@NonNull Subscription subscription) {
    subscription.request(Long.MAX_VALUE);
}

这个方法中我们执行了传入的scalarSubscription的request方法,进入这个方法:

public void request(long n) {
    if(SubscriptionHelper.validate(n)) { 
        if(this.compareAndSet(0, 1)) { 
            Subscriber s = this.subscriber;
            s.onNext(this.value);
            if(this.get() != 2) {
                s.onComplete();
            }
        }
    }
}

1)第一句validate检查n必须大于0
2) 第二句compareAndSet方法是原子操作,ScalarSubscription继承自AtomicInteger,通过这个方法保障volatile修饰的变量数据在多核情况下依然能够线程安全的操作。
来到AtomicInteger类中看,compareAndSet是AtomicInteger中的方法,第一个参数表示期望值,如果期望值与当前值(AtomicInteger中的value成员变量)一致,就更新当前值为第二个参数
这里就是作了一个自增1操作,成功返回true
3)接下来就是调用subscriber的onNext,将数据传入,例子中我将这个值打印输出日志
3)接下来this.get()获取到AtomicInteger中的value,此时经过前面的自增操作应该是1,执行subscriber.onComplete方法。

到这里就明白了RxJava2最简单的例子是如何一步一步调用下来的。


猜你喜欢

转载自blog.csdn.net/dehang0/article/details/79121240