作用: 将Observable所发送送的信息进行格式转换或者处理,然后转变为另外一个类型,发送给Observer
思路:
一、ObservableCreate
二、ObservableMap
三、调用思路和源码逻辑
四、生产环境代码
五、画图总结
ps:
subscribe() 是Observable中的的方法,因为ObservableCreate和ObservableMap都继承了Observable所以把该方法放到了这两个类中
一、ObservableCreate
public final class ObservableCreate<T> extends Observable<T> {
final ObservableOnSubscribe<T> source;
public ObservableCreate(ObservableOnSubscribe<T> source) {
this.source = source;
}
@Override
void subscribe(Observer<? super T> observer) {
subscribeActual(observer);
}
//
@Override
protected void subscribeActual(Observer<? super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<T>(observer);
observer.onSubscribe(parent);
source.subscribe(parent);
}
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 (!isDisposed()) {
observer.onNext(t);
}
}
@Override
public void onComplete() {
observer.onComplete();
}
}
}
二、ObservableMap
ObservableMap extends Observable{
ObservableSource<T> source; //指ObservableCreate
public ObservableMap(ObservableSource<T> source, Function<? super T, ? extends U> function) {
super(source);
this.function = function;
}
@Override
void subscribe(Observer<? super T> observer) {
subscribeActual(observer);
}
//
@Override
public void subscribeActual(Observer<? super U> t) {
source.subscribe(new MapObserver<T, U>(t, function));
}
static final class MapObserver<T, U> extends BasicFuseableObserver<T, U> {
final Function<? super T, ? extends U> mapper;
Observer<? super R> actual;
MapObserver(Observer<? super U> actual, Function<? super T, ? extends U> mapper) {
super(actual);
this.mapper = mapper;
}
@Override
public void onNext(T t) {
U v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
actual.onNext(v);
}
}
三、分析调用流程
1.第一块
发起点:订阅与map操作符操作
源码中可以看到create() 创建了ObservableCreate
map() 创建了ObservableMap
map().subscribe(observer1) 其实是调用的 ObserbableMap的 subscribe() ——>subscribeActual()
ObserbableMap中的subscribeActual (Observer t ){
source.subscribe(new MapObserver<T, U>(t, function));
}
发现:observer1已经被包装成了MapObserver
source是谁?从哪里来??? : source.subscribe(new MapObserver<T, U>(t, function));
2.第二块
其实在ObserbableMap是调用的ObservableCreate的 map()方法创建的,代码如下:
public final <R> Observable<R> map(Function<? super T, ? extends R> mapper) {
return RxJavaPlugins.onAssembly(new ObservableMap<T, R>(this, mapper));
}
this : 指的是当前对象,当前对象就是ObservableCreate
ok那下一步就是调用ObservableCreate的subscribe()方法
3.第三块:
ObservableCreate{
@Override
void subscribe(Observer<? super T> observer) {
subscribeActual(observer);
}@Override
protected void subscribeActual(Observer<? super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<T>(observer);
observer.onSubscribe(parent);source.subscribe(parent);
}}
source 是谁? 是observableOnSubscribe1
parent 是谁? parent是CreateEmitter的一个实例,包裹了MapObserver的对象
observableOnSubscribe1代码:{
ObservableOnSubscribe observableOnSubscribe1 = new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onComplete();
}
};emitter.onNext(1);代码如下:{
public void onNext(T t) {
observer.onNext(t);
}
observer是MapObserver的一个实例被包装在了CreateEmitter中
observer.onNext(t); 代码如下{
Observer<? super R> actual;
public void onNext(T t) {
U v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
actual.onNext(v);
}actual是实例observer1
}
}
四、生产环境代码
//创建一个下游 Observer
Observer<Integer> observer1 = new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("donghe", "subscribe");
}
@Override
public void onNext(Integer value) {
Log.d("donghe", "" + value);
}
@Override
public void onError(Throwable e) {
Log.d("donghe", "error");
}
@Override
public void onComplete() {
Log.d("donghe", "complete");
}
};
Function function1 = new Function<Integer, Object>() {
@Override
public Object apply(Integer integer) throws Exception {
return null;
}
};
ObservableOnSubscribe observableOnSubscribe1 = new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onComplete();
}
};
//创建一个上游 Observable:
Observable.create(observableOnSubscribe1).map(function1).subscribe(observer1);
五、调用逻辑顺序