Skip to content

Commit ebe6962

Browse files
committed
added general purpose combine* extensions
1 parent 965e1a2 commit ebe6962

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.monstarlab.extensions
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.lifecycle.LifecycleCoroutineScope
5+
import androidx.lifecycle.lifecycleScope
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.flow.Flow
8+
import kotlinx.coroutines.flow.collect
9+
import kotlinx.coroutines.flow.combine
10+
import kotlinx.coroutines.launch
11+
12+
fun <T1, T2> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, collectBlock: (suspend (T1, T2) -> Unit)) {
13+
launch {
14+
flow1.combine(flow2) { v1, v2 ->
15+
collectBlock.invoke(v1, v2)
16+
}.collect {
17+
// Empty collect block to trigger ^
18+
}
19+
}
20+
}
21+
22+
fun <T1, T2, T3> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, flow3: Flow<T3>, collectBlock: (suspend (T1, T2, T3) -> Unit)) {
23+
launch {
24+
combine(flow1, flow2, flow3) { v1, v2, v3 ->
25+
collectBlock.invoke(v1, v2, v3)
26+
}.collect {
27+
// Empty collect block to trigger ^
28+
}
29+
}
30+
}
31+
32+
fun <T1, T2, T3, T4> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, flow3: Flow<T3>, flow4: Flow<T4>, collectBlock: (suspend (T1, T2, T3, T4) -> Unit)) {
33+
launch {
34+
combine(flow1, flow2, flow3, flow4) { v1, v2, v3, v4 ->
35+
collectBlock.invoke(v1, v2, v3, v4)
36+
}.collect {
37+
// Empty collect block to trigger ^
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)