Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test {
- private val refreshTrigger: MutableSharedFlow<Unit> = MutableSharedFlow(extraBufferCapacity = 1)
- // This is my entry point called from a ViewModel with the `viewModelScope`
- fun launchIn(scope: CoroutineScope) {
- val test = Test()
- test()
- .onEach { log("Got: $it") }
- .launchIn(scope)
- scope.launch {
- delay(2000L)
- test.refresh()
- }
- }
- @OptIn(ExperimentalCoroutinesApi::class)
- operator fun invoke(): Flow<Any> = refreshTrigger
- .onStart { emit(Unit) }
- .onEach { log("1 refreshing flow!") }
- .flatMapLatest {
- log("2 remitting flow!")
- randomFlow()
- }
- private fun randomFlow(): Flow<Any> = flow { emit(Any()) }
- fun refresh() {
- log("Emit refresh")
- refreshTrigger.tryEmit(Unit)
- }
- private fun log(message: String) {
- AppLogger.i(message, "kotlinlang")
- }
- }
- /*
- * Android output:
- 16:16:02.812 kotlinlang I 1 refreshing flow!
- 16:16:02.813 kotlinlang I 2 remitting flow!
- 16:16:02.814 kotlinlang I Got: java.lang.Object@3df904f
- 16:16:04.816 kotlinlang I Emit refresh
- 16:16:04.816 kotlinlang I 1 refreshing flow!
- 16:16:04.817 kotlinlang I 2 remitting flow!
- 16:16:04.817 kotlinlang I Got: java.lang.Object@b915e29
- */
- /*
- * iOS output:
- 02-14 16:17:20.205 💙 INFO kotlinlang - 1 refreshing flow!
- 02-14 16:17:20.206 💙 INFO kotlinlang - 2 remitting flow!
- 02-14 16:17:20.206 💙 INFO kotlinlang - Got: kotlin.Any@2ea16c8
- 02-14 16:17:22.207 💙 INFO kotlinlang - Emit refresh
- 02-14 16:17:22.207 💙 INFO kotlinlang - 1 refreshing flow!
- 02-14 16:17:22.208 💙 INFO kotlinlang - 2 remitting flow!
- 02-14 16:17:22.208 💙 INFO kotlinlang - Got: kotlin.Any@2ea1f98
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement