RxJava1执行分析2 - map

书接上文《RxJava1执行分析1》,这里主要分析加入操作符map的执行流程:

RxJava1执行分析2 - map_第1张图片
image.png

示例2

Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(Subscriber subscriber) {
                subscriber.onNext("goods");
                subscriber.onCompleted();
            }
        })
                .map(new Func1() {
                    @Override
                    public String call(String s) {
                        return s+" map";
                    }
                })
                .subscribe(new Action1() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });

map的源码:

    public final  Observable map(Func1 func) {
        return unsafeCreate(new OnSubscribeMap(this, func));
    }

    public static  Observable unsafeCreate(OnSubscribe f) {
        return new Observable(RxJavaHooks.onCreate(f));
    }

这里可以发现 map 操作符会对应的生成Observable(rxjava中的操作符都会至少生成一个)与OnSubscribeMap( onSubscribe的实现) 。同时它也会持有链路中上一个节点的observable的引用

OnSubscribeMap源码:

public final class OnSubscribeMap implements OnSubscribe {

    final Observable source;

    final Func1 transformer;

    public OnSubscribeMap(Observable source, Func1 transformer) {
        this.source = source;
        this.transformer = transformer;
    }

    @Override
    public void call(final Subscriber o) {
        MapSubscriber parent = new MapSubscriber(o, transformer);
        o.add(parent);
        source.unsafeSubscribe(parent);
    }

    static final class MapSubscriber extends Subscriber {

        final Subscriber actual;

        final Func1 mapper;

        boolean done;

        public MapSubscriber(Subscriber actual, Func1 mapper) {
            this.actual = actual;
            this.mapper = mapper;
        }

        @Override
        public void onNext(T t) {
            R result;

            try {
                result = mapper.call(t);
            } catch (Throwable ex) {
                Exceptions.throwIfFatal(ex);
                unsubscribe();
                onError(OnErrorThrowable.addValueAsLastCause(ex, t));
                return;
            }

            actual.onNext(result);
        }

        @Override
        public void onError(Throwable e) {
            if (done) {
                RxJavaHooks.onError(e);
                return;
            }
            done = true;

            actual.onError(e);
        }


        @Override
        public void onCompleted() {
            if (done) {
                return;
            }
            actual.onCompleted();
        }

        @Override
        public void setProducer(Producer p) {
            actual.setProducer(p);
        }
    }

}

以下的关键代码中可以发现 map节点对上一个节点进行了订阅,call中传入的subcriber是下一个节点的订阅者

   public void call(final Subscriber o) {
        MapSubscriber parent = new MapSubscriber(o, transformer);
        o.add(parent);
        source.unsafeSubscribe(parent);
    }

source.unsafeSubscribe源码:

    public final Subscription unsafeSubscribe(Subscriber subscriber) {
             ...
            subscriber.onStart();
            RxJavaHooks.onObservableStart(this, onSubscribe).call(subscriber);
            return RxJavaHooks.onObservableReturn(subscriber);
             ...
            return Subscriptions.unsubscribed();
        }
    }
RxJava1执行分析2 - map_第2张图片
动图

你可能感兴趣的:(RxJava1执行分析2 - map)