从反应流处理中删除变量突变



以下工作,但我认为它可以更好,即更惯用的 rx/功能/更少可变。

var total = 0
var published = 0
service
.getThings()
.doOnNext { total++ }
.flatMap { shouldPublish(it) }  // Returns empty() or just(it)
.doOnNext { published++ }
.subscribe { publishToSqs(it) }
println("Published $publish out of $total")

有什么想法吗?

我通常在".merge((">d事件流上使用".scan((">运算符。这样,始终存在最新状态的有效快照。

更多关于它的信息在这里 - 反应聚合状态管道

最新更新