使用的版本:
implementation 'io.reactivex:rxjava:1.3.8'
implementation 'io.reactivex:rxandroid:1.2.1'
just操作符简单示例先看一个简单的例子如下:
Observable.just(1, 2, 3, 4, 5)
.subscribe(new Observer() {
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
@Override
public void onError(Throwable e) {
System.out.println("onError");
}
@Override
public void onNext(Integer i) {
System.out.println("onNext:" + i);
}
});
我们发现just重载了好多just方法,最多有十个参数。然后在just方法中调用了from方法,这里只看5个参数just方法的源码
public static Observable just(T t1, T t2, T t3, T t4, T t5) {
return from((T[])new Object[] { t1, t2, t3, t4, t5 });
}
public static Observable from(T[] array) {
int n = array.length;
if (n == 0) {
return empty();
} else
if (n == 1) {
return just(array[0]);
}
return unsafeCreate(new OnSubscribeFromArray(array));
}
好了,到了这就可以知道just就是壳啊,都是交给from大哥干活的,from方法中根据传入数组的长度来分别处理,我们再来跟进分析一下
1. 0长度,就是just没有传入参数,跟进一下empty方法
public static Observable empty() {
return EmptyObservableHolder.instance();
}
public enum EmptyObservableHolder implements OnSubscribe
EmptyObservableHolder是个枚举类,实现了OnSubscribe,它的静态参数EMPTY是使用INSTANCE创建了一个Observable,所以最终是调用的当前的call方法。就是啥也不干,直接onCompleted。
2. 1长度,就是just传入一个参数,跟进一下一个参数的just方法
public static Observable just(final T value) {
return ScalarSynchronousObservable.create(value);
}
public static ScalarSynchronousObservable create(T t) {
return new ScalarSynchronousObservable(t);
}
protected ScalarSynchronousObservable(final T t) {
super(RxJavaHooks.onCreate(new JustOnSubscribe(t)));
this.t = t;
}
static final class JustOnSubscribe implements OnSubscribe {
final T value;
JustOnSubscribe(T value) {
this.value = value;
}
@Override
public void call(Subscriber super T> s) {
s.setProducer(createProducer(s, value));
}
}
static Producer createProducer(Subscriber super T> s, T v) {
if (STRONG_MODE) {
return new SingleProducer(s, v);
}
return new WeakSingleProducer(s, v);
}
static final class WeakSingleProducer implements Producer {
final Subscriber super T> actual;
final T value;
boolean once;
public WeakSingleProducer(Subscriber super T> actual, T value) {
this.actual = actual;
this.value = value;
}
@Override
public void request(long n) {
if (once) {
return;
}
if (n < 0L) {
throw new IllegalStateException("n >= required but it was " + n);
}
if (n == 0L) {
return;
}
once = true;
Subscriber super T> a = actual;
if (a.isUnsubscribed()) {
return;
}
T v = value;
try {
a.onNext(v);
} catch (Throwable e) {
Exceptions.throwOrReport(e, a, v);
return;
}
if (a.isUnsubscribed()) {
return;
}
a.onCompleted();
}
}
看到这我们就发现其实最后是调用了producer.requst的方法,然后回头看一下WeakSingleProducer类,就可以发现在他的request方法中可以看到熟悉的onNext和onCompleted方法。
3. 其他长度,跟进一下OnSubscribeFromArray的源码
public final class OnSubscribeFromArray implements OnSubscribe {
final T[] array;
public OnSubscribeFromArray(T[] array) {
this.array = array;
}
@Override
public void call(Subscriber super T> child) {
child.setProducer(new FromArrayProducer(child, array));
}
static final class FromArrayProducer
extends AtomicLong
implements Producer {
/** */
private static final long serialVersionUID = 3534218984725836979L;
final Subscriber super T> child;
final T[] array;
int index;
public FromArrayProducer(Subscriber super T> child, T[] array) {
this.child = child;
this.array = array;
}
@Override
public void request(long n) {
if (n < 0) {
throw new IllegalArgumentException("n >= 0 required but it was " + n);
}
if (n == Long.MAX_VALUE) {
if (BackpressureUtils.getAndAddRequest(this, n) == 0) {
fastPath();
}
} else
if (n != 0) {
if (BackpressureUtils.getAndAddRequest(this, n) == 0) {
slowPath(n);
}
}
}
void fastPath() {
final Subscriber super T> child = this.child;
for (T t : array) {
if (child.isUnsubscribed()) {
return;
}
child.onNext(t);
}
if (child.isUnsubscribed()) {
return;
}
child.onCompleted();
}
void slowPath(long r) {
final Subscriber super T> child = this.child;
final T[] array = this.array;
final int n = array.length;
long e = 0L;
int i = index;
for (;;) {
while (r != 0L && i != n) {
if (child.isUnsubscribed()) {
return;
}
child.onNext(array[i]);
i++;
if (i == n) {
if (!child.isUnsubscribed()) {
child.onCompleted();
}
return;
}
r--;
e--;
}
r = get() + e;
if (r == 0L) {
index = i;
r = addAndGet(e);
if (r == 0L) {
return;
}
e = 0L;
}
}
}
}
}
从OnSubscribeFromArray的call方法我们就明白了,关键就在FromArrayProducer的request方法中。这里面有个背压处理,不过这不是这节的重点不看了,直接看里面的fastPath和slowPath都是遍历数组然后onNext。到这我们就明白了from操作符的原理,当然just一样明白了。