導入された変換演算子ソースのRxJava flatMapシリーズ

転載、リンクなどのソースを明記してください:
:この記事はから来て103styleさんのブログ

変換関連の事業者との公式紹介

RxJavaflatMapシリーズの変換演算子公式の説明:変革観測

ここでは、単に特定の実装、中間流を参照してください解決ソース演算子を作成RxJava

flatMap

  • 公式例:
    Observable.just("A", "B", "C")
            .flatMap(a -> {
                return Observable.intervalRange(1, 3, 0, 1, TimeUnit.SECONDS)
                        .map(b -> '(' + a + ", " + b + ')');
            })
            .blockingSubscribe(System.out::println);
    
    出力:
    (A, 1)
    (C, 1)
    (B, 1)
    (A, 2)
    (C, 2)
    (B, 2)
    (A, 3)
    (C, 3)
    (B, 3)
    
  • オブジェクトを返しますObservableFlatMapsubscribeActual方法を:
    public void subscribeActual(Observer<? super U> t) {
        if (ObservableScalarXMap.tryScalarXMapSubscribe(source, t, mapper)) {
            return;
        }
        source.subscribe(new MergeObserver<T, U>(t, mapper, delayErrors, maxConcurrency, bufferSize));
    }
    
  • MergeObserveronNext方法:
    public void onNext(T t) {
        // safeguard against misbehaving sources
        ...
        subscribeInner(p);
    }
    
    void subscribeInner(ObservableSource<? extends U> p) {
        for (;;) {
            if (p instanceof Callable) {
               ...
                        drain();
               ...
            } else {
                InnerObserver<T, U> inner = new InnerObserver<T, U>(this, uniqueId++);
                if (addInner(inner)) {
                    p.subscribe(inner);
                }
                break;
            }
        }
    }
    
  • InnerObserveronNext方法:
    public void onNext(U t) {
        if (fusionMode == QueueDisposable.NONE) {
            parent.tryEmit(t, this);
        } else {
            parent.drain();
        }
    }
    
  • MergeObserverdrain()方法:
    void drain() {
        if (getAndIncrement() == 0) {
            drainLoop();
        }
    }
    
    void drainLoop() {
        final Observer<? super U> child = this.downstream;//1.0
        int missed = 1;
        for (;;) {
            ...
            SimplePlainQueue<U> svq = queue;
            if (svq != null) {
                for (;;) {
                    ...
                    U o = svq.poll();
                    if (o == null) {
                        break;
                    }
                    child.onNext(o);//1.1
                }
            }
            ...
            if (d && (svq == null || svq.isEmpty()) && n == 0 && nSources == 0) {
                Throwable ex = errors.terminate();
                if (ex != ExceptionHelper.TERMINATED) {
                    if (ex == null) {
                        child.onComplete();//1.2
                    } else {
                        child.onError(ex);//1.3
                    }
                }
                return;
            }
            ...
        }
    }
    
    最終的に呼び出して、入力されたobserver対応方法を。

ます。https://www.jianshu.com/p/191a8680b8ccで再現

おすすめ

転載: blog.csdn.net/weixin_34148508/article/details/91052816