Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

kotlin - 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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...