简体   繁体   English

为什么RxJava2不支持“ Maybe.doOnDispose”?

[英]Why “Maybe.doOnDispose” is not supported in RxJava2?

I'm using Maybe class in RxJava2. 我在RxJava2中使用Maybe类。

I registered the doOnDispose callback to detect the Dispose event, but it is not fired. 我注册了doOnDispose回调以检测Dispose事件,但未触发。

Maybe.just("aaa")
    .doOnDispose({ /* do something */ })
    .subscribe( ... )

I looked at the RxJava 2 code, but Maybe seemed not to support doOnDispose . 我查看了RxJava 2代码,但Maybe似乎不支持doOnDispose

Maybe is create MaybePeek (not DoOnDisposeObserver ) object in doOnDispose ,,, Maybe是在doOnDispose创建MaybePeek (不是DoOnDisposeObserver )对象,

@CheckReturnValue
@SchedulerSupport("none")
public final Maybe<T> doOnDispose(Action onDispose) {
    return RxJavaPlugins.onAssembly(new MaybePeek(this, Functions.emptyConsumer(), Functions.emptyConsumer(), Functions.emptyConsumer(), Functions.EMPTY_ACTION, Functions.EMPTY_ACTION, (Action)ObjectHelper.requireNonNull(onDispose, "onDispose is null")));
}

protected void subscribeActual(MaybeObserver<? super T> observer) {
    this.source.subscribe(new MaybePeek.MaybePeekObserver(observer, this));
}

But, Single is create DoOnDisposeObserver , and it is worked fine. 但是, Single是create DoOnDisposeObserver ,并且运行良好。

@CheckReturnValue
@SchedulerSupport("none")
public final Single<T> doOnDispose(Action onDispose) {
    ObjectHelper.requireNonNull(onDispose, "onDispose is null");
    return RxJavaPlugins.onAssembly(new SingleDoOnDispose(this, onDispose));
}

protected void subscribeActual(SingleObserver<? super T> s) {
    this.source.subscribe(new SingleDoOnDispose.DoOnDisposeObserver(s, this.onDispose));
}

Why Maybe.doOnDispose is not supported? 为什么不支持Maybe.doOnDispose

As the documentation says about doOnDispose(Action onDispose) 如文档所述,关于doOnDispose(Action onDispose)

Calls the dispose Action if the downstream disposes the sequence. 如果下游处理序列,则调用处理操作。

Since your downstream never dispose it, it will never call. 由于您的下游永远不会处理它,因此它永远不会调用。

Disposable disposable = Maybe.just("aaa")
    .doOnDispose({ /* do something */ })
    .subscribe( ... )

disposable.dispose();

Now the action in doOnDispose should be called. 现在应该调用doOnDispose的操作。

Note that if the completion of the stream takes less time than the going to the next operation ( disposable.dispose() ), then the onDispose action should not be called. 请注意,如果流的完成所花的时间少于进行下一个操作( onDispose disposable.dispose() )的时间,则不应调用onDispose操作。 So, in order to verify it you can use a delay: 因此,为了验证它,您可以使用延迟:

Disposable disposable = Maybe.just("aaa")
    .delay(2000, TimeUnit.MILLISECONDS)
    .doOnDispose({ /* do something */ })
    .subscribe( ... )

disposable.dispose();

Now the action should be fired. 现在该动作应该被解雇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM