Flowable concatMapSingle 没有预取以忽略点击直到处理完成 [英] Flowable concatMapSingle without prefetch to ignore clicks until processing finishes
问题描述
我希望以这样一种方式处理点击,只要我正在处理发生的某些点击,它们就会被忽略.
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
可以解决问题,如 akarnokd 在 GitHub 上的建议:
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
s的最大活动订阅数:
The last parameter is maxConcurrency
, specifying the maximum number of active subscriptions to the SingleSource
s:
clicks
.onBackpressureDrop()
.flatMapSingle(::handleClick, false, 1)
在这种情况下,flatMapSingle
按顺序订阅那些 Single
,因此它不会改变我从 concatMapSingle
获得的语义.
In this instance, flatMapSingle
subscribes to those Single
s sequentially, so it doesn't change the semantics I got from concatMapSingle
.
这篇关于Flowable concatMapSingle 没有预取以忽略点击直到处理完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!