可流动的concatMapSingle,无需预取即可忽略点击,直到处理完成 [英] Flowable concatMapSingle without prefetch to ignore clicks until processing finishes

查看:193
本文介绍了可流动的concatMapSingle,无需预取即可忽略点击,直到处理完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以这样一种方式来处理点击,只要我正在处理发生的某些点击,就可以忽略它们.

I want to handle clicks in such a way that they are ignored as long as I'm doing processing of some click that occurred.

我以为我可以通过利用背压来做到这一点,就像这样:

I thought I could do it by utilizing the backpressure, like this:

private val clicks = PublishProcessor.create<Unit>()

// ...

clicks
    .onBackpressureDrop()
    .concatMapSingle(::handleClick, 0)

但是这会引发错误,因为有一个要求concatMapSingle需要预取至少一个项目,这使得它在处理完成后立即将点击​​排队并立即处理它,这不是我想要的.我只想在当前没有任何处理的情况下处理点击.

But this throws an error, because there's a requirement that concatMapSingle needs to prefetch at least one item, which makes it queue the click and process it immediately after I'm done processing, which is not what I want. I want to process the click only if there is no processing happening at the moment.

还有其他一些运算符可以用来达到预期效果吗?

Is there some other operator I could use to achieve the desired effect?

推荐答案

使用flatMapSingle代替concatMapSingle可以解决问题,

Using flatMapSingle instead of concatMapSingle does the trick, as suggested by akarnokd on GitHub:

flatMap仅在当前项实际完成时才获取下一个上游项

flatMap will only fetch the next upstream item if the current one actually completed

最后一个参数是maxConcurrency,用于指定SingleSource的最大活动订阅数:

The last parameter is maxConcurrency, specifying the maximum number of active subscriptions to the SingleSources:

clicks
    .onBackpressureDrop()
    .flatMapSingle(::handleClick, false, 1)

在这种情况下,flatMapSingle按顺序订阅了这些Single,因此它不会改变我从concatMapSingle获得的语义.

In this instance, flatMapSingle subscribes to those Singles sequentially, so it doesn't change the semantics I got from concatMapSingle.

这篇关于可流动的concatMapSingle,无需预取即可忽略点击,直到处理完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆