使用 ThreatLatest、distinctUntilChanged 到 Kotlin 协程的 RxJava 运算符

vov*_*ost 5 android kotlin rx-java2 kotlin-coroutines

将此 RxJava 链转换为 Kotlin 协程的最佳方法是什么?

RxTextView
    .textChanges(inputEditText)
    .map { it.toString() }
    .throttleLatest(500, TimeUnit.MILLISECONDS, true)
    .distinctUntilChanged()
    .subscribe({ text ->
        // More logic here
    }, { error ->
        // Error handling
    })
Run Code Online (Sandbox Code Playgroud)

vov*_*ost 0

GitHub 上有一个相关的未解决问题,其中提出了使用现有运算符的解决方案:

fun <T> Flow<T>.throttleLatest(delayMillis: Long): Flow<T> = this
    .conflate()
    .transform {
        emit(it)
        delay(delayMillis)
    }
Run Code Online (Sandbox Code Playgroud)

https://github.com/Kotlin/kotlinx.coroutines/issues/1446#issuecomment-1198103541

还有一个与 RxJavathrottleLatest功能类似的库。这是函数文档https://hoc081098.github.io/FlowExt/docs/latest/-flow-ext/com.hoc081098.flowext/throttle-time.html

提供不同 RxJava 运算符的库 https://github.com/hoc081098/FlowExt