Skip to content

Commit

Permalink
[UI] Update Coroutine zip
Browse files Browse the repository at this point in the history
  • Loading branch information
ajou4095 committed Nov 21, 2024
1 parent 9d507c2 commit f8638e3
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.ray.template.android.common.util.coroutine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import kotlinx.coroutines.coroutineScope

data class Zip2Result<T1, T2>(
val result1: T1,
Expand All @@ -12,11 +11,11 @@ data class Zip2Result<T1, T2>(
suspend fun <T1, T2> zip(
block1: suspend () -> Result<T1>,
block2: suspend () -> Result<T2>
): Result<Zip2Result<T1, T2>> = withContext(Dispatchers.IO) {
): Result<Zip2Result<T1, T2>> = coroutineScope {
val deferred1 = async { block1() }
val deferred2 = async { block2() }

return@withContext runCatching {
return@coroutineScope runCatching {
Zip2Result(
result1 = deferred1.await().getOrThrow(),
result2 = deferred2.await().getOrThrow()
Expand All @@ -34,12 +33,12 @@ suspend fun <T1, T2, T3> zip(
block1: suspend () -> Result<T1>,
block2: suspend () -> Result<T2>,
block3: suspend () -> Result<T3>
): Result<Zip3Result<T1, T2, T3>> = withContext(Dispatchers.IO) {
): Result<Zip3Result<T1, T2, T3>> = coroutineScope {
val deferred1 = async { block1() }
val deferred2 = async { block2() }
val deferred3 = async { block3() }

return@withContext runCatching {
return@coroutineScope runCatching {
Zip3Result(
result1 = deferred1.await().getOrThrow(),
result2 = deferred2.await().getOrThrow(),
Expand All @@ -60,13 +59,13 @@ suspend fun <T1, T2, T3, T4> zip(
block2: suspend () -> Result<T2>,
block3: suspend () -> Result<T3>,
block4: suspend () -> Result<T4>
): Result<Zip4Result<T1, T2, T3, T4>> = withContext(Dispatchers.IO) {
): Result<Zip4Result<T1, T2, T3, T4>> = coroutineScope {
val deferred1 = async { block1() }
val deferred2 = async { block2() }
val deferred3 = async { block3() }
val deferred4 = async { block4() }

return@withContext runCatching {
return@coroutineScope runCatching {
Zip4Result(
result1 = deferred1.await().getOrThrow(),
result2 = deferred2.await().getOrThrow(),
Expand All @@ -90,14 +89,14 @@ suspend fun <T1, T2, T3, T4, T5> zip(
block3: suspend () -> Result<T3>,
block4: suspend () -> Result<T4>,
block5: suspend () -> Result<T5>
): Result<Zip5Result<T1, T2, T3, T4, T5>> = withContext(Dispatchers.IO) {
): Result<Zip5Result<T1, T2, T3, T4, T5>> = coroutineScope {
val deferred1 = async { block1() }
val deferred2 = async { block2() }
val deferred3 = async { block3() }
val deferred4 = async { block4() }
val deferred5 = async { block5() }

return@withContext runCatching {
return@coroutineScope runCatching {
Zip5Result(
result1 = deferred1.await().getOrThrow(),
result2 = deferred2.await().getOrThrow(),
Expand All @@ -124,15 +123,15 @@ suspend fun <T1, T2, T3, T4, T5, T6> zip(
block4: suspend () -> Result<T4>,
block5: suspend () -> Result<T5>,
block6: suspend () -> Result<T6>
): Result<Zip6Result<T1, T2, T3, T4, T5, T6>> = withContext(Dispatchers.IO) {
): Result<Zip6Result<T1, T2, T3, T4, T5, T6>> = coroutineScope {
val deferred1 = async { block1() }
val deferred2 = async { block2() }
val deferred3 = async { block3() }
val deferred4 = async { block4() }
val deferred5 = async { block5() }
val deferred6 = async { block6() }

return@withContext runCatching {
return@coroutineScope runCatching {
Zip6Result(
result1 = deferred1.await().getOrThrow(),
result2 = deferred2.await().getOrThrow(),
Expand Down

0 comments on commit f8638e3

Please sign in to comment.