Created
March 24, 2022 18:01
-
-
Save hoc081098/f80a191c546077e567a7210d68b2bdbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
public fun <T> race(flows: Iterable<Flow<T>>): Flow<T> = TODO("Will implement") | |
fun firstSource(): Flow<String> = flow { | |
delay(400) | |
repeat(10) { | |
emit("[1] - $it") | |
delay(100) | |
} | |
} | |
fun secondSource(): Flow<String> = flow { | |
delay(100) | |
repeat(10) { | |
emit("[2] - $it") | |
delay(100) | |
} | |
} | |
fun thirdSource(): Flow<String> = flow { | |
delay(500) | |
repeat(10) { | |
emit("[3] - $it") | |
delay(100) | |
} | |
} | |
suspend fun main() { | |
race( | |
firstSource(), | |
secondSource(), | |
thirdSource() | |
).collect { println(it) } | |
// CONSOLE | |
// [2] - 0 | |
// [2] - 1 | |
// [2] - 2 | |
// [2] - 3 | |
// [2] - 4 | |
// [2] - 5 | |
// [2] - 6 | |
// [2] - 7 | |
// [2] - 8 | |
// [2] - 9 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment