Browse Source

Fastest.kt 增加一个函数

drake 4 years ago
parent
commit
2423c7eb08

+ 1 - 1
README.md

@@ -107,7 +107,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.9'
 
-implementation 'com.github.liangjingkanji:Net:2.2.17'
+implementation 'com.github.liangjingkanji:Net:2.2.18'
 ```
 
 <br>

+ 2 - 1
docs/api/net/com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/fastest.md

@@ -3,6 +3,7 @@
 # fastest
 
 `suspend fun <T> CoroutineScope.fastest(vararg deferredArray: Deferred<T>): T`
+`suspend fun <T> CoroutineScope.fastest(deferredArray: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<Deferred<T>>): T`
 
 该函数将选择[deferredArray](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.Array((kotlinx.coroutines.Deferred((com.drake.net.utils.fastest.T)))))/deferredArray)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果
 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
@@ -22,7 +23,7 @@
 
 [DeferredTransform](../../com.drake.net.transform/-deferred-transform/index.md)
 
-`suspend fun <T, R> CoroutineScope.fastest(deferredList: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>>): R`
+`@JvmName("fastestTransform") suspend fun <T, R> CoroutineScope.fastest(deferredList: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>>): R`
 
 该函数将选择[deferredList](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.collections.List((com.drake.net.transform.DeferredTransform((com.drake.net.utils.fastest.T, com.drake.net.utils.fastest.R)))))/deferredList)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果
 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常

+ 1 - 1
docs/api/net/com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/index.md

@@ -4,4 +4,4 @@
 
 | Name | Summary |
 |---|---|
-| [fastest](fastest.md) | 该函数将选择[deferredArray](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.Array((kotlinx.coroutines.Deferred((com.drake.net.utils.fastest.T)))))/deferredArray)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常`suspend fun <T> CoroutineScope.fastest(vararg deferredArray: Deferred<T>): T`<br>`suspend fun <T, R> CoroutineScope.fastest(vararg deferredArray: `[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>): R`<br>该函数将选择[deferredList](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.collections.List((com.drake.net.transform.DeferredTransform((com.drake.net.utils.fastest.T, com.drake.net.utils.fastest.R)))))/deferredList)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常`suspend fun <T, R> CoroutineScope.fastest(deferredList: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>>): R` |
+| [fastest](fastest.md) | 该函数将选择[deferredArray](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.Array((kotlinx.coroutines.Deferred((com.drake.net.utils.fastest.T)))))/deferredArray)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常`suspend fun <T> CoroutineScope.fastest(vararg deferredArray: Deferred<T>): T`<br>`suspend fun <T> CoroutineScope.fastest(deferredArray: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<Deferred<T>>): T`<br>`suspend fun <T, R> CoroutineScope.fastest(vararg deferredArray: `[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>): R`<br>该函数将选择[deferredList](fastest.md#com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.collections.List((com.drake.net.transform.DeferredTransform((com.drake.net.utils.fastest.T, com.drake.net.utils.fastest.R)))))/deferredList)中的Deferred执行[Deferred.await](#), 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常`suspend fun <T, R> CoroutineScope.fastest(deferredList: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`DeferredTransform`](../../com.drake.net.transform/-deferred-transform/index.md)`<T, R>>): R` |

+ 1 - 0
docs/api/net/package-list

@@ -22,6 +22,7 @@ $dokka.location:com.drake.net.transform$transform(kotlinx.coroutines.Deferred((c
 $dokka.location:com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.Array((com.drake.net.transform.DeferredTransform((com.drake.net.utils.fastest.T, com.drake.net.utils.fastest.R)))))com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/fastest.md
 $dokka.location:com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.Array((kotlinx.coroutines.Deferred((com.drake.net.utils.fastest.T)))))com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/fastest.md
 $dokka.location:com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.collections.List((com.drake.net.transform.DeferredTransform((com.drake.net.utils.fastest.T, com.drake.net.utils.fastest.R)))))com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/fastest.md
+$dokka.location:com.drake.net.utils$fastest(kotlinx.coroutines.CoroutineScope, kotlin.collections.List((kotlinx.coroutines.Deferred((com.drake.net.utils.fastest.T)))))com.drake.net.utils/kotlinx.coroutines.-coroutine-scope/fastest.md
 $dokka.location:com.drake.net.utils$foreach(android.database.Cursor, kotlin.Function1((android.database.Cursor, kotlin.Unit)))com.drake.net.utils/android.database.-cursor/foreach.md
 $dokka.location:com.drake.net.utils$getSavedModel(androidx.fragment.app.Fragment)com.drake.net.utils/androidx.fragment.app.-fragment/get-saved-model.md
 $dokka.location:com.drake.net.utils$getSavedModel(androidx.fragment.app.FragmentActivity)com.drake.net.utils/androidx.fragment.app.-fragment-activity/get-saved-model.md

+ 1 - 4
net/src/main/java/com/drake/net/transform/DeferredTransform.kt

@@ -23,7 +23,4 @@ fun <T, R> Deferred<T>.transform(block: (T) -> R): DeferredTransform<T, R> {
     return DeferredTransform(this, block)
 }
 
-data class DeferredTransform<T, R>(
-        val deferred: Deferred<T>,
-        val block: (T) -> R
-                                  )
+data class DeferredTransform<T, R>(val deferred: Deferred<T>, val block: (T) -> R)

+ 158 - 0
net/src/main/java/com/drake/net/utils/Fastest.kt

@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2018 Drake, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.drake.net.utils
+
+import com.drake.net.transform.DeferredTransform
+import com.yanzhenjie.kalle.NetCancel
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.sync.Mutex
+import kotlinx.coroutines.sync.withLock
+
+
+/**
+ * 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
+ * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
+ *
+ * @param deferredArray 一系列并发任务
+ */
+@OptIn(ExperimentalCoroutinesApi::class)
+@Suppress("SuspendFunctionOnCoroutineScope")
+suspend fun <T> CoroutineScope.fastest(vararg deferredArray: Deferred<T>): T {
+    val chan = Channel<T>()
+    val mutex = Mutex()
+    deferredArray.forEach {
+        launch(Dispatchers.IO) {
+            try {
+                val result = it.await()
+                mutex.withLock {
+                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
+                    chan.send(result)
+                }
+            } catch (e: Exception) {
+                it.cancel()
+                val allFail = deferredArray.all { it.isCancelled }
+                if (allFail) throw e else {
+                    if (e !is CancellationException) e.printStackTrace()
+                }
+            }
+        }
+    }
+    return chan.receive()
+}
+
+/**
+ * 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
+ * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
+ *
+ * @param deferredArray 一系列并发任务
+ */
+@OptIn(ExperimentalCoroutinesApi::class)
+@Suppress("SuspendFunctionOnCoroutineScope")
+suspend fun <T> CoroutineScope.fastest(deferredArray: List<Deferred<T>>): T {
+    val chan = Channel<T>()
+    val mutex = Mutex()
+    deferredArray.forEach {
+        launch(Dispatchers.IO) {
+            try {
+                val result = it.await()
+                mutex.withLock {
+                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
+                    chan.send(result)
+                }
+            } catch (e: Exception) {
+                it.cancel()
+                val allFail = deferredArray.all { it.isCancelled }
+                if (allFail) throw e else {
+                    if (e !is CancellationException) e.printStackTrace()
+                }
+            }
+        }
+    }
+    return chan.receive()
+}
+
+/**
+ * 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
+ * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
+ *
+ * @see DeferredTransform 允许监听[Deferred]返回数据回调
+ * @param deferredArray 一系列并发任务
+ */
+@OptIn(ExperimentalCoroutinesApi::class)
+@Suppress("SuspendFunctionOnCoroutineScope")
+suspend fun <T, R> CoroutineScope.fastest(vararg deferredArray: DeferredTransform<T, R>): R {
+    val chan = Channel<R>()
+    val mutex = Mutex()
+    deferredArray.forEach {
+        launch(Dispatchers.IO) {
+            try {
+                val result = it.deferred.await()
+                mutex.withLock {
+                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
+                    if (!chan.isClosedForSend) {
+                        val transformResult = it.block(result)
+                        chan.send(transformResult)
+                    }
+                }
+            } catch (e: Exception) {
+                it.deferred.cancel()
+                val allFail = deferredArray.all { it.deferred.isCancelled }
+                if (allFail) throw e else {
+                    if (e !is CancellationException) e.printStackTrace()
+                }
+            }
+        }
+    }
+    return chan.receive()
+}
+
+/**
+ * 该函数将选择[deferredList]中的Deferred执行[Deferred.await], 然后将返回最快的结果
+ * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
+ *
+ * @see DeferredTransform 允许监听[Deferred]返回数据回调
+ * @param deferredList 一系列并发任务
+ */
+@JvmName("fastestTransform")
+@OptIn(ExperimentalCoroutinesApi::class)
+@Suppress("SuspendFunctionOnCoroutineScope")
+suspend fun <T, R> CoroutineScope.fastest(deferredList: List<DeferredTransform<T, R>>): R {
+    val chan = Channel<R>()
+    val mutex = Mutex()
+    deferredList.forEach {
+        launch(Dispatchers.IO) {
+            try {
+                val result = it.deferred.await()
+                mutex.withLock {
+                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
+                    if (!chan.isClosedForSend) {
+                        val transformResult = it.block(result)
+                        chan.send(transformResult)
+                    }
+                }
+            } catch (e: Exception) {
+                it.deferred.cancel()
+                val allFail = deferredList.all { it.deferred.isCancelled }
+                if (allFail) throw e else {
+                    if (e !is CancellationException) e.printStackTrace()
+                }
+            }
+        }
+    }
+    return chan.receive()
+}

+ 0 - 0
net/src/main/java/com/drake/net/utils/JetPackUtils.kt → net/src/main/java/com/drake/net/utils/JetPack.kt


+ 4 - 111
net/src/main/java/com/drake/net/utils/ScopeUtils.kt → net/src/main/java/com/drake/net/utils/Scope.kt

@@ -23,15 +23,13 @@ import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleOwner
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.scope.*
-import com.drake.net.transform.DeferredTransform
 import com.drake.statelayout.StateLayout
-import com.yanzhenjie.kalle.NetCancel
-import kotlinx.coroutines.*
-import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.InternalCoroutinesApi
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.FlowCollector
-import kotlinx.coroutines.sync.Mutex
-import kotlinx.coroutines.sync.withLock
 
 /**
  * 作用域内部全在主线程
@@ -168,108 +166,3 @@ inline fun <T> Flow<T>.scope(owner: LifecycleOwner? = null,
 }
 
 
-//<editor-fold desc="并发返回最快">
-/**
- * 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
- * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
- *
- * @param deferredArray 一系列并发任务
- */
-@OptIn(ExperimentalCoroutinesApi::class)
-@Suppress("SuspendFunctionOnCoroutineScope")
-suspend fun <T> CoroutineScope.fastest(vararg deferredArray: Deferred<T>): T {
-    val chan = Channel<T>()
-    val mutex = Mutex()
-    deferredArray.forEach {
-        launch(Dispatchers.IO) {
-            try {
-                val result = it.await()
-                mutex.withLock {
-                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
-                    chan.send(result)
-                }
-            } catch (e: Exception) {
-                it.cancel()
-                val allFail = deferredArray.all { it.isCancelled }
-                if (allFail) throw e else {
-                    if (e !is CancellationException) e.printStackTrace()
-                }
-            }
-        }
-    }
-    return chan.receive()
-}
-
-/**
- * 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
- * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
- *
- * @see DeferredTransform 允许监听[Deferred]返回数据回调
- * @param deferredArray 一系列并发任务
- */
-@OptIn(ExperimentalCoroutinesApi::class)
-@Suppress("SuspendFunctionOnCoroutineScope")
-suspend fun <T, R> CoroutineScope.fastest(vararg deferredArray: DeferredTransform<T, R>): R {
-    val chan = Channel<R>()
-    val mutex = Mutex()
-    deferredArray.forEach {
-        launch(Dispatchers.IO) {
-            try {
-                val result = it.deferred.await()
-                mutex.withLock {
-                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
-                    if (!chan.isClosedForSend) {
-                        val transformResult = it.block(result)
-                        chan.send(transformResult)
-                    }
-                }
-            } catch (e: Exception) {
-                it.deferred.cancel()
-                val allFail = deferredArray.all { it.deferred.isCancelled }
-                if (allFail) throw e else {
-                    if (e !is CancellationException) e.printStackTrace()
-                }
-            }
-        }
-    }
-    return chan.receive()
-}
-
-/**
- * 该函数将选择[deferredList]中的Deferred执行[Deferred.await], 然后将返回最快的结果
- * 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
- *
- * @see DeferredTransform 允许监听[Deferred]返回数据回调
- * @param deferredList 一系列并发任务
- */
-@OptIn(ExperimentalCoroutinesApi::class)
-@Suppress("SuspendFunctionOnCoroutineScope")
-suspend fun <T, R> CoroutineScope.fastest(deferredList: List<DeferredTransform<T, R>>): R {
-    val chan = Channel<R>()
-    val mutex = Mutex()
-    deferredList.forEach {
-        launch(Dispatchers.IO) {
-            try {
-                val result = it.deferred.await()
-                mutex.withLock {
-                    NetCancel.cancel(coroutineContext[CoroutineExceptionHandler])
-                    if (!chan.isClosedForSend) {
-                        val transformResult = it.block(result)
-                        chan.send(transformResult)
-                    }
-                }
-            } catch (e: Exception) {
-                it.deferred.cancel()
-                val allFail = deferredList.all { it.deferred.isCancelled }
-                if (allFail) throw e else {
-                    if (e !is CancellationException) e.printStackTrace()
-                }
-            }
-        }
-    }
-    return chan.receive()
-}
-//</editor-fold>
-
-
-

+ 0 - 0
net/src/main/java/com/drake/net/utils/SuspendUtils.kt → net/src/main/java/com/drake/net/utils/Suspend.kt