Flowable subscribe process description

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

Flowable method of subscribe

public final Disposable subscribe() {
    return subscribe(Functions.emptyConsumer(), Functions.ON_ERROR_MISSING,
            Functions.EMPTY_ACTION, FlowableInternalHelper.RequestMax.INSTANCE);
}

public final Disposable subscribe(Consumer<? super T> onNext) {
    return subscribe(onNext, Functions.ON_ERROR_MISSING,
            Functions.EMPTY_ACTION, FlowableInternalHelper.RequestMax.INSTANCE);
}

public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) {
    return subscribe(onNext, onError, Functions.EMPTY_ACTION,
            FlowableInternalHelper.RequestMax.INSTANCE);
}

public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError,
                                  Action onComplete) {
    return subscribe(onNext, onError, onComplete,
            FlowableInternalHelper.RequestMax.INSTANCE);
}

public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError,
                                  Action onComplete, Consumer<? super Subscription> onSubscribe) {
    LambdaSubscriber<T> ls = new LambdaSubscriber<T>(onNext, onError, onComplete, onSubscribe);
    subscribe(ls);
    return ls;
}

public final void subscribe(Subscriber<? super T> s) {
    if (s instanceof FlowableSubscriber) {
        subscribe((FlowableSubscriber<? super T>)s);
    } else {
        ObjectHelper.requireNonNull(s, "s is null");
        subscribe(new StrictSubscriber<T>(s));
    }
}

public final void subscribe(FlowableSubscriber<? super T> s) {
    ObjectHelper.requireNonNull(s, "s is null");
    try {
        Subscriber<? super T> z = RxJavaPlugins.onSubscribe(this, s);
        subscribeActual(z);
    } catch (...) {
       ...
    }
}

protected abstract void subscribeActual(Subscriber<? super T> s);

The first four methods are invoked by default:

  • Functions.emptyConsumer() :
    static final class EmptyConsumer implements Consumer<Object> {
        @Override
        public void accept(Object v) { }
    
        @Override  
        public String toString() {
            return "EmptyConsumer";
        }
    }
    
  • Functions.ON_ERROR_MISSING :
    static final class OnErrorMissingConsumer implements Consumer<Throwable> {
        @Override
        public void accept(Throwable error) {
            RxJavaPlugins.onError(new OnErrorNotImplementedException(error));
        }
    }
    
  • Functions.EMPTY_ACTION :
    static final class EmptyAction implements Action {
        @Override
        public void run() { }
    
        @Override
        public String toString() {
            return "EmptyAction";
        }
    }
    
  • FlowableInternalHelper.RequestMax.INSTANCE :
    public enum RequestMax implements Consumer<Subscription> {
        INSTANCE;
        @Override
        public void accept(Subscription t) throws Exception {
            t.request(Long.MAX_VALUE);
        }
    }
    

Call subscribe(onNext, onError, onComplete, onSubscribe), and then packaged into a four parameter LambdaSubscriberobject to the child class overrides a subscribeActualmethod.

And subscribe(Subscriber<? super T> s)is achieved by transmitting its own FlowableSubscriberinterface or a transfer Subscriberconfigured to StrictSubscribertransfer subclasses overridden subscribeActualmethods.

And then the next process to create the source operator of analytical Rxjava described similarly.

the above.

Reproduced in: https: //www.jianshu.com/p/0aa24c3ccb4f

Guess you like

Origin blog.csdn.net/weixin_34026484/article/details/91052813