RxJava flatMap series of conversion operator Source introduced

Reproduced, please indicate the source as a link:
This article comes from: 103style's blog

Conversion-related operator and the official introduction

RxJavaThe flatMapseries conversion operator official description: Transforming observables

Here we just a specific implementation, please refer to the intermediate flow RxJava create the source operator resolved .

flatMap

  • Official Example:
    Observable.just("A", "B", "C")
            .flatMap(a -> {
                return Observable.intervalRange(1, 3, 0, 1, TimeUnit.SECONDS)
                        .map(b -> '(' + a + ", " + b + ')');
            })
            .blockingSubscribe(System.out::println);
    
    Output:
    (A, 1)
    (C, 1)
    (B, 1)
    (A, 2)
    (C, 2)
    (B, 2)
    (A, 3)
    (C, 3)
    (B, 3)
    
  • Returns the object ObservableFlatMap's subscribeActualmethods:
    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));
    }
    
  • MergeObserverThe onNextmethod:
    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;
            }
        }
    }
    
  • InnerObserverThe onNextmethod:
    public void onNext(U t) {
        if (fusionMode == QueueDisposable.NONE) {
            parent.tryEmit(t, this);
        } else {
            parent.drain();
        }
    }
    
  • MergeObserverThe drain()method:
    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;
            }
            ...
        }
    }
    
    Eventually call-entered the observercorresponding method.

Reproduced in: https: //www.jianshu.com/p/191a8680b8cc

Guess you like

Origin blog.csdn.net/weixin_34148508/article/details/91052816