drake 3 سال پیش
والد
کامیت
188f4e2b31

+ 3 - 1
docs/debounce.md

@@ -17,7 +17,7 @@
 ```kotlin
 var scope: CoroutineScope? = null
 
-et_input.debounce().listen(this) {
+et_input.debounce().distinctUntilChanged().listen(this) {
     scope?.cancel() // 发起新的请求前取消旧的请求, 避免旧数据覆盖新数据
     scope = scopeNetLife { // 保存旧的请求到一个变量中, scopeNetLife其函数决定网络请求生命周期
         tvFragment.text = "请求中"
@@ -33,6 +33,8 @@ et_input.debounce().listen(this) {
 fun EditText.debounce(timeoutMillis: Long = 800)
 ```
 
+过滤掉重复结果使用函数`distinctUntilChanged`
+
 ## 生命周期
 其生命周期依然遵守[网络请求作用域函数scope*](scope.md#_2)
 

+ 2 - 1
net/src/main/java/com/drake/net/utils/FlowUtils.kt

@@ -32,6 +32,7 @@ import kotlinx.coroutines.flow.debounce
 /**
  * 收集Flow结果并过滤重复结果
  */
+@Deprecated("规范命名", ReplaceWith("launchIn"), DeprecationLevel.WARNING)
 @OptIn(InternalCoroutinesApi::class)
 inline fun <T> Flow<T>.listen(
     owner: LifecycleOwner? = null,
@@ -50,7 +51,7 @@ inline fun <T> Flow<T>.listen(
  * @param event 销毁时机
  * @param dispatcher 指定调度器
  */
-@Deprecated("规范命名", ReplaceWith("launchIn"), DeprecationLevel.ERROR)
+@Deprecated("规范命名", ReplaceWith("launchIn"), DeprecationLevel.WARNING)
 @OptIn(InternalCoroutinesApi::class)
 inline fun <T> Flow<T>.scope(
     owner: LifecycleOwner? = null,

+ 4 - 2
sample/src/main/java/com/drake/net/sample/ui/fragment/EditDebounceFragment.kt

@@ -5,10 +5,11 @@ import com.drake.net.Get
 import com.drake.net.sample.R
 import com.drake.net.sample.databinding.FragmentEditDebounceBinding
 import com.drake.net.utils.debounce
-import com.drake.net.utils.listen
+import com.drake.net.utils.launchIn
 import com.drake.net.utils.scopeNetLife
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.cancel
+import kotlinx.coroutines.flow.distinctUntilChanged
 import org.json.JSONObject
 
 class EditDebounceFragment :
@@ -20,7 +21,8 @@ class EditDebounceFragment :
     override fun initView() {
         var scope: CoroutineScope? = null
 
-        binding.etInput.debounce().listen(this) {
+        // distinctUntilChanged 表示过滤掉重复结果
+        binding.etInput.debounce().distinctUntilChanged().launchIn(this) {
             scope?.cancel() // 发起新的请求前取消旧的请求, 避免旧数据覆盖新数据
             scope = scopeNetLife { // 保存旧的请求到一个变量中
                 binding.tvFragment.text = "请求中"