Skip to content

Commit

Permalink
Update flatMapCombined function to ensure safe type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tungnk123 committed Nov 17, 2024
1 parent ca92e34 commit e8fe89a
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions core/src/main/java/com/m3u/core/util/coroutine/Flows.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,70 @@ fun <T> Flow<T>.timeout(duration: Duration, block: FlowCollector<T>.() -> Unit)
}

fun <R> flatmapCombined(
flows: Iterable<Flow<Any>>,
transform: (keys: Array<Any>) -> Flow<R>
): Flow<R> = combine(flows) { it }.flatMapLatest { keys -> transform(keys) }
vararg flows: Flow<*>,
transform: (values: List<Any?>) -> Flow<R>,
): Flow<R> =
combine(flows.asList()) { it.toList() }
.flatMapLatest { values -> transform(values) }

@Suppress("UNCHECKED_CAST")
fun <T1, T2, R> flatmapCombined(
flow1: Flow<T1>,
flow2: Flow<T2>,
transform: (t1: T1, t2: T2) -> Flow<R>
): Flow<R> where T1 : Any, T2 : Any, R : Any = flatmapCombined(listOf(flow1, flow2)) { keys ->
transform(keys[0] as T1, keys[1] as T2)
}
transform: (t1: T1, t2: T2) -> Flow<R>,
): Flow<R> =
combine(
flow1,
flow2
) { t1, t2 -> t1 to t2 }
.flatMapLatest { (t1, t2) ->
transform(
t1,
t2
)
}

@Suppress("UNCHECKED_CAST")
fun <T1, T2, T3, R> flatmapCombined(
flow1: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
transform: (t1: T1, t2: T2, t3: T3) -> Flow<R>
): Flow<R> where T1 : Any, T2 : Any, T3 : Any, R : Any =
flatmapCombined(listOf(flow1, flow2, flow3)) { keys ->
transform(keys[0] as T1, keys[1] as T2, keys[2] as T3)
transform: (t1: T1, t2: T2, t3: T3) -> Flow<R>,
): Flow<R> =
combine(
flow1,
flow2,
flow3
) { t1, t2, t3 ->
Triple(
t1,
t2,
t3
)
}
.flatMapLatest { (t1, t2, t3) ->
transform(
t1,
t2,
t3
)
}

@Suppress("UNCHECKED_CAST")
fun <T1, T2, T3, T4, R> flatmapCombined(
flow1: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
transform: (t1: T1, t2: T2, t3: T3, t4: T4) -> Flow<R>
): Flow<R> where T1 : Any, T2 : Any, T3 : Any, T4 : Any, R : Any =
flatmapCombined(listOf(flow1, flow2, flow3, flow4)) { keys ->
transform(keys[0] as T1, keys[1] as T2, keys[2] as T3, keys[3] as T4)
}
transform: (t1: T1, t2: T2, t3: T3, t4: T4) -> Flow<R>,
): Flow<R> =
combine(
flow1,
flow2,
flow3,
flow4
) { t1, t2, t3, t4 ->
transform(
t1,
t2,
t3,
t4
)
}.flatMapLatest { it }

0 comments on commit e8fe89a

Please sign in to comment.