package com.suno.android.utils.mvi import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.rememberCoroutineScope import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.onSubscription import kotlinx.coroutines.launch @Composable fun MviEffectHandler( effects: SharedFlow, onBound: (suspend () -> Unit)? = null, handler: suspend (T) -> Unit, ) { // set up a scope independent of LaunchedEffect so that effect handling isn't cancelled on recomposition val coroutineScope = rememberCoroutineScope() LaunchedEffect(effects) { effects .onSubscription { onBound?.invoke() } .collect { effect -> // launch on each effect so effects are handled in parallel coroutineScope.launch { handler(effect) } } } }