Browse Source

Flow的函数scope重命名为launchIn

drake 3 years ago
parent
commit
7ff67d0170
1 changed files with 16 additions and 2 deletions
  1. 16 2
      net/src/main/java/com/drake/net/utils/FlowUtils.kt

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

@@ -47,19 +47,33 @@ inline fun <T> Flow<T>.listen(
  * @param event 销毁时机
  * @param event 销毁时机
  * @param dispatcher 指定调度器
  * @param dispatcher 指定调度器
  */
  */
+@Deprecated("规范命名", ReplaceWith("launchIn"))
 @OptIn(InternalCoroutinesApi::class)
 @OptIn(InternalCoroutinesApi::class)
 inline fun <T> Flow<T>.scope(
 inline fun <T> Flow<T>.scope(
     owner: LifecycleOwner? = null,
     owner: LifecycleOwner? = null,
     event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     dispatcher: CoroutineDispatcher = Dispatchers.Main,
     dispatcher: CoroutineDispatcher = Dispatchers.Main,
     crossinline action: suspend CoroutineScope.(value: T) -> Unit
     crossinline action: suspend CoroutineScope.(value: T) -> Unit
+): AndroidScope = launchIn(owner, event, dispatcher, action)
+
+/**
+ * Flow直接创建作用域
+ * @param owner 跟随的生命周期组件
+ * @param event 销毁时机
+ * @param dispatcher 指定调度器
+ */
+@OptIn(InternalCoroutinesApi::class)
+inline fun <T> Flow<T>.launchIn(
+    owner: LifecycleOwner? = null,
+    event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
+    dispatcher: CoroutineDispatcher = Dispatchers.Main,
+    crossinline action: suspend CoroutineScope.(value: T) -> Unit
 ): AndroidScope = AndroidScope(owner, event, dispatcher).launch {
 ): AndroidScope = AndroidScope(owner, event, dispatcher).launch {
-    this@scope.collect(object : FlowCollector<T> {
+    this@launchIn.collect(object : FlowCollector<T> {
         override suspend fun emit(value: T) = action(this@launch, value)
         override suspend fun emit(value: T) = action(this@launch, value)
     })
     })
 }
 }
 
 
-
 /**
 /**
  * 为EditText的输入框文本变化启用节流阀, 即超过指定时间后(默认800毫秒)的输入框文本变化事件[TextWatcher.onTextChanged]会被下游收集到
  * 为EditText的输入框文本变化启用节流阀, 即超过指定时间后(默认800毫秒)的输入框文本变化事件[TextWatcher.onTextChanged]会被下游收集到
  *
  *