Skip to content

Instantly share code, notes, and snippets.

@hrach
Created July 30, 2021 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrach/94ba0dc427ad2f08a73467d8972516a2 to your computer and use it in GitHub Desktop.
Save hrach/94ba0dc427ad2f08a73467d8972516a2 to your computer and use it in GitHub Desktop.
Difference between MutableSharedFlow() & Channel()
class Foo1 {
private val unpause = MutableSharedFlow<Unit>()
suspend fun await() {
delay(1000)
unpause.first()
}
fun unpause() {
unpause.tryEmit(Unit)
}
}
class Foo2 {
private val unpause = Channel<Unit>()
suspend fun await() {
delay(1000)
unpause.receiveCatching()
}
fun unpause() {
unpause.trySend(Unit)
}
}
class ExampleUnitTest {
@Test
fun testUnpause() = runBlockingTest {
var afterPause = false
val foo = Foo1()
launch {
foo.await()
afterPause = true
}
advanceTimeBy(1000)
assertEquals(false, afterPause)
foo.unpause()
assertEquals(true, afterPause)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment